[英]How to plot regression line for a 2x2x2 design in ggplot2 in R?
I have data (dtf.long) that looks as follows: 我有如下数据(dtf.long):
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. 总共有16个重复。 I want to plot a regression with log(seedlingdensity) on y axis and log(plandensity) on x-axis, faceted by food. 我想绘制一个回归图,y轴上的对数(seedlingdensity)和x轴上的log(plandensity)都由食物组成。 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. 您可以将参数shape
添加到对geom_point
的调用中,并将其映射到geom_point
可变season
的值。
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 您可以在ggplot网站的此页面上找到更多示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.