crossquare.blogg.se

Scatter plot ggplot2
Scatter plot ggplot2





In the plots above you can see that the slopes vary by grp category. 15, aes(fill = grp))Įxtracting predicted values with predict() ggplot(dat, aes(x = x1, y = resp, color = grp) ) + I increased the transparency of the ribbons by decreasing alpha, as well, since adding confidence ribbons for many fitted lines in one plot can end up looking pretty messy. I used fill to make the ribbons the same color as the lines. Here is the same plot with a 95% confidence envelope (the default interval size) as a ribbon around the fitted lines.

scatter plot ggplot2

This is a linear model fit, so I use method = "lm". Confidence intervals can be suppressed using se = FALSE, which I use below. This can be great if you are plotting the results after you’ve checked all assumptions but is not-so-great if you are exploring the data. I’m going to plot fitted regression lines of resp vs x1 for each grp category.īy default you will get confidence intervals plotted in geom_smooth(). Supported model types include models fit with lm(), glm(), nls(), and mgcv::gam().įitted lines can vary by groups if a factor variable is mapped to an aesthetic like color or group. The geom_smooth() function in ggplot2 can plot fitted lines from models with a simple structure.

scatter plot ggplot2

Plotting separate slopes with geom_smooth() These data are from a blocked design, and the block variable is available to be used as a random effect. This dataset has one response variable, resp, along with two continuous ( x1, x2) and one categorical ( grp) explanatory variables. I created a dataset to use for fitting models and used dput() to copy and paste it here. I’m going to set the ggplot2 theme to theme_bw(). First I’ll load the packages I’m using today.







Scatter plot ggplot2