简体   繁体   中英

Combing facet_grid with geom_abline

I am trying to use geom_abline with facet_grid where I have 4 months in a categorical variable and 4 lakes in a categorical variable giving a 16 panel grid. I have already built the slopes and intercepts using coef(summary(lm)) from 4 different lm models. Is it possible to specify which panels in the facet_grid I want to add the geom_abline to? I have added some dummy data to get an idea of what I am trying to do, but think of all 16 panels having multiple data points rather than 4 of them having one..

df1 <- data.frame(month =c("May","June","September","October"),
              lake = c("L1","L2","L3","L4"),
              var1 = c(10,9,8,9),
              var2 = c(11,9,8,9)
              )

gridfig <- ggplot(df1, aes(var2,var1))+
  facet_grid(rows = vars(month), cols = vars(lake))+
  geom_point(size = 2)

Any advice would be much appreciated.

If you build your model with lm , you can use geom_smooth to plot the model directly. Here, I used the mpg dataset to show you how do it:

ggplot(mpg, aes(x = displ, y = cty)) + 
    geom_point() + 
    facet_grid(drv ~ cyl) +
    geom_smooth(method = "lm")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM