I am having a problem with loess lines displaying properly in ggplot. I have several variables, none of which can go below zero e.g. heights, weights and abundances. I am trying to plot this data in ggplot with a loess line. Using some made up data:
library(ggplot2)
df <- as.data.frame(rep(1:7, each = 5))
df[,2] <- c(0,1,5,0,6,0,7,2,9,1,1,18,4,2,34,8,18,24,56,12,12,18,24,63,48,
40,70,53,75,98,145,176,59,98,165)
names(df) <- c("x", "y")
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_smooth() +
scale_y_continuous(limits = c(-20,200))
This would be fine except that the shaded region showing the confidence intervals around the smoothed line goes below zero and a reviewer has pointed out that this is not possible and asked that I change it. I thought this could be easily done by setting the lower limit on the y axis to zero:
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_smooth() +
scale_y_continuous(limits = c(0,200))
But this makes part of the shaded area around the loess line dissapear. Is there a way to either make the plot work with the y axis limited to zero so that part of the shaded region is cut off, or to set a limit on the loess line in the first place so that it won't create a shaded region which goes below zero?
In this article, we will see how to generate Shading confidence intervals manually with ggplot2 in R Programming language. Let us first draw a regular curve and then add confidence intervals to it. To add shading confidence intervals, geom_ribbon () function is used.
Next, let’s plot this data as a line, and add a ribbon (using geom_ribbon) that represents the confidence interval. By adding an alpha (opacity) you can give it a nice shaded effect. You should know that you can plot multiple lines and multiple confidence intervals, simply by setting multiple groups.
Often you may want to set the axis limits on a plot using ggplot2. You can easily do this using the following functions: xlim (): specifies the lower and upper limit of the x-axis. ylim (): specifies the lower and upper limit of the y-axis.
Figure 2: ggplot2 Density Plot with Broader x-Axis due to scale_x_continuous Function. As you can see based on the previous R syntax, we specified the axis limits within the scale_x_continuous command to be within the range of -10 and 10.
We can calculate override the ymin
aes of a stat_smooth
(note the difference with geom_smooth
):
ggplot(df, aes(x=x, y=y)) +
geom_point() +
stat_smooth(geom='ribbon', aes(ymin = ifelse(..ymin.. < 0, 0, ..ymin..)),
alpha = .3) +
geom_smooth(se = FALSE) +
scale_y_continuous(limits = c(-20,200))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Created on 2018-05-22 by the reprex package (v0.2.0).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With