此代码与ggplot2 0.9.3
无法正常工作(与早期版本的ggplot2一起使用,请参阅here)。如果你建议解决这个问题我会非常感激。感谢
library(ggplot2)
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue", aes(group=supp))
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp))
p <- p + theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + theme(axis.title.y = element_text(size = 12, angle = 90, vjust = 0.25))
print(p)
修改 这一行
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp))
产生以下警告
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
答案 0 :(得分:2)
此行为是ggplot2 0.9.3中的错误:https://github.com/hadley/ggplot2/issues/739
您可以使用ddply计算摘要来解决这个问题:
library(plyr)
tg <- ddply(ToothGrowth, c("dose", "supp"), summarise, len = mean(len))
library(ggplot2)
ggplot(ToothGrowth, aes(x=as.factor(dose), y=len, colour=supp)) +
geom_boxplot() +
geom_line(data=tg, aes(group=supp))