ggplot2
自动调整数据点的 ylim
。有什么办法也可以调整 ylim
的 stat_summary
吗?
df <- structure(list(Varieties = structure(c(2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L), .Label = c("F9917", "Hegari", "JS263",
"JS2002"), class = "factor"), Priming = structure(c(2L, 2L, 2L,
2L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 4L, 4L, 4L,
4L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L), .Label = c("CaCl2", "Dry",
"Hydropriming", "KNO3", "OnFarmpriming"), class = "factor"),
PH = c(225.8, 224.26, 228.9, 215.82, 230.3, 227.7, 232.8,
221.1, 260.2, 230.8, 236.75, 230.5, 250.56, 230.74, 240.64,
226.7, 268.4, 233.4, 243.33, 232.7, 252.04, 233.1, 237.14,
220.6, 265.55, 234.93, 240.04, 218.21, 300.55, 245, 243.5,
234.65, 253.3, 233.5, 238.62, 225.93, 255.74, 233.64, 238.1,
230.93, 246, 240.33, 246.08, 221.7, 250.54, 242.87, 251,
225.32, 251.47, 245.4, 266.74, 227.73, 290.62, 246.68, 256.4,
225.83, 282.67, 240.58, 258.35, 235.87)), .Names = c("Varieties",
"Priming", "PH"), class = "data.frame", row.names = c(NA, 60L
))
p1 <- ggplot(data=df, aes(x=Varieties, y=PH, group=Priming, shape=Priming, colour=Priming))+
stat_summary(fun.y=mean, geom="point", size=2, aes(group=Priming, shape=Priming, colour=Priming))+
theme_bw()
p1 <- p1 + stat_summary(fun.y=mean, geom="line", aes(group=Priming, shape=Priming, colour=Priming))
print(p1)
有关 stat_summary 值,请参阅 ylim 中的额外空间。提前感谢您的帮助和时间。
最佳答案
这是一种方法,在绘图之前使用 plyr
准备数据
df <- ddply(df, .(Varieties, Priming), transform, meanPH = mean(PH))
ggplot(df, aes(Varieties, meanPH)) +
geom_point() +
geom_line(aes(group = Priming, color = Priming))
关于r - 使用 stat_summary 自动调整 ylim,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9146635/