我正在尝试使用ggplot2进行交互绘制。我的代码如下:

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")
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = 1))
p <- p  + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p  + opts(axis.title.y = theme_text(size = 12, angle = 90,  vjust = 0.25))
print(p)

如何绘制剂量-补充水平的组合平均值,而不是仅绘制我要到达的剂量水平的平均值?在此先感谢您的帮助。

最佳答案

您可以预先计算自己数据框中的值:

toothInt <- ddply(ToothGrowth,.(dose,supp),summarise, val = mean(len))

ggplot(ToothGrowth, aes(x = factor(dose), y = len, colour = supp)) +
    geom_boxplot() +
    geom_point(data = toothInt, aes(y = val)) +
    geom_line(data = toothInt, aes(y = val, group = supp)) +
    theme_bw()

请注意,使用ggplot而不是qplot可以使图形构造对于此类更复杂的图(IMHO)更加清晰。

关于r - ggplot2中的交互图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7323246/

10-12 19:58