Search code examples
rggplot2smoothingconfidence-interval

R - geom_smooth, add se for only one line


Is there a way, in geom_smooth() (from libray ggplot2), to have the confidence interval (parameters se = T) for a line but not for the other ?

mpg %>% 
   filter(class %in% c('compact', 'midsize')) %>% 
   ggplot(aes(x = displ, y = as.numeric(hwy), color = class)) + 
      geom_smooth(se = T)

In the graph below, I would like to keep the confidence interval for the blue line, but to remove the one of the red line. As se parameter is not in the aes() function, I don't manage to pass different values in it. Moreover, there is no function like scale_fill_manual(), to specify different values.

enter image description here


Solution

  • This should work:

    mpg %>% 
      filter(class %in% c('compact', 'midsize')) %>% 
      ggplot(aes(x = displ, y = as.numeric(hwy), color = class)) + 
      geom_smooth(data = . %>% filter(class == "compact"), method = "loess", se = F) +
      geom_smooth(data = . %>% filter(class == "midsize"), method = "loess", se = T)