Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confidence interval disappears in ggplot2 using coord_cartesian

Tags:

Limiting the x range by coord_cartesian makes the confidence interval created by stat_smooth disappear.

I noticed it happens with R 4.1.0 but not with R 4.0.2. The ggplot2 version I used was 3.3.5.

The first plot shows the confidence interval as a shade. The second plot should show the same confidence interval in the specified range. But with R 4.1.0, the shade disappear. Any one experiencing it?

library(ggplot2)
mtcars %>%
  ggplot(aes(x = wt, y = mpg)) +
  geom_point() +
  stat_smooth()

mtcars %>%
  ggplot(aes(x = wt, y = mpg)) +
  geom_point() +
  stat_smooth() +
  coord_cartesian(xlim = c(3, 4))
like image 206
microbe Avatar asked Jul 30 '21 14:07

microbe


1 Answers

The comments solved the problem. I summarize them as an answer. Thanks.

The issue is the R 4.1.0 problem on Windows. See the ggplot2 issue Problem with geom_ribbon and alpha combined with coord-cartesian in R 4.1.0 on Windows.

The remedy is using ragg as a graphic backend. If you are using RStudio >= 1.4, change the graphic backend to AGG by going to General option -> General -> Graphics.

like image 83
microbe Avatar answered Oct 16 '22 15:10

microbe