简体   繁体   中英

How to plot regression line for a 2x2x2 design in ggplot2 in R?

I have data (dtf.long) that looks as follows:

nutrition fertilizer season seedlingdensity plandensity fitted
  nitrogen  none       wet        5             19       6
  nitrogen  none       dry        5             19       8
  nitrogen  phos       wet        4             23       16
  nitrogen  phos       dry        5             19       10
  iron      none       wet        5             29       21
  iron      none       dry        5             19       14
  iron      phos       wet        4             23       12
  iron      phos       dry        5             20       14
  ....
  ...

There are total of 16 replicates. I want to plot a regression with log(seedlingdensity) on y axis and log(plandensity) on x-axis, faceted by food. The two types of fertilizer can go in one colour and the season in different pch.

I have tried to write out a code, but I still don't know how to code the pch for season

The model fit from the regression is stored in the column fitted

summary_dat = dtf.long %>%
              group_by(nutrition, fertilizer, season) %>%
              summarise(mean_predict=mean(fitted),
                        sd_predict = sd(fitted),
                        n_predict = n()) %>%

  mutate(se_predict = sd_predict / sqrt(n_predict),
         lower_ci = mean_predict - qt(1 - (0.05 / 2), n_predict - 1) * se_predict,
         upper_ci = mean_predict + qt(1 - (0.05 / 2), n_predict - 1) * se_predict)

ggplot() + 
  geom_point(data=dtf.long, aes(x=log(plantdensity), y=log(seedlingdensity), group=fertilizer, color = fertilizer), position=position_dodge(width=0.5)) + 
  geom_errorbar(data=summary_dat, aes(x=log(plantdensity), ymax=upper_ci, ymin=lower_ci, group=fertilizer, color=fertilizer), position=position_dodge(width=0.5), width=0.2) + 
  geom_point(data=summary_dat, aes(log(plantdensity), y=mean_predict, group=fertilizer, color=fertilizer), size=3, position=position_dodge(width=0.5)) + 
  facet_grid(nutrition ~ .) + xlab("log(Plant Density") + ylab("log(Seedling Density)")

You can add the argument shape to the call to geom_point , and map it to the value of the variable season in your data.frame.

ggplot() + 
 geom_point(data=dtf.long, 
    aes(x=log(plantdensity), 
        y=log(seedlingdensity), 
        group=fertilizer, 
        color = fertilizer,
        shape = season), 
    position=position_dodge(width=0.5)) + 
 geom_errorbar(data=summary_dat, 
    aes(x=log(plantdensity), 
        ymax=upper_ci, 
        ymin=lower_ci), 
    position=position_dodge(width=0.5), width=0.2) + 
 facet_grid(nutrition ~ .) + 
 xlab("log(Plant Density") + 
 ylab("log(Seedling Density)")

You can find further examples at this page on the ggplot site

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