ggplot2中的交互图

时间:2011-09-06 16:37:10

标签: r ggplot2

我正在尝试与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)

我如何绘制剂量水平组合手段而不仅仅是剂量水平意味着我来到这里?在此先感谢您的帮助。

enter image description here

4 个答案:

答案 0 :(得分:31)

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

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()

enter image description here

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

答案 1 :(得分:9)

您可以按相应的组(supp)计算摘要:

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  + 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)

或转换为ggplot语法(并合并为一个表达式)

ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
  geom_boxplot() +
  stat_summary(aes(group=supp), fun.y = mean, geom="point", colour="blue") +
  stat_summary(aes(group=supp), fun.y = mean, geom="line") +
  scale_x_discrete("Dose") +
  scale_y_continuous("Response") +
  theme_bw() +
  opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0),
    axis.title.y = theme_text(size = 12, angle = 90,  vjust = 0.25))

编辑:

为了使这项工作与0.9.3一致,它实际上变为Joran's answer

library("plyr")
summ <- ddply(ToothGrowth, .(supp, dose), summarise, len = mean(len))

ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
  geom_boxplot() +
  geom_point(data = summ, aes(group=supp), colour="blue", 
             position = position_dodge(width=0.75)) +
  geom_line(data = summ, aes(group=supp), 
            position = position_dodge(width=0.75)) +
  scale_x_discrete("Dose") +
  scale_y_continuous("Response") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0),
        axis.title.y = element_text(size = 12, angle = 90,  vjust = 0.25))

enter image description here

答案 2 :(得分:7)

如果您认为可能需要更通用的方法,可以在HandyStuff包中尝试函数rxnNorm(github.com/bryanhanson/HandyStuff)。免责声明:我是作者。免责声明#2:盒子图选项不能正常工作,但所有其他选项都没问题。

以下是使用ToothGrowth数据的示例:

p <- rxnNorm(data = ToothGrowth, res = "len", fac1 = "dose", fac2 = "supp", freckles = TRUE, method = "iqr", fac2cols = c("red", "green"))
print(p)

rxnNorm Demo

答案 3 :(得分:1)

更简单的方法。没有ddply。直接用ggplot2。

ggplot(ToothGrowth, aes(x = factor(dose) , y=len , group = supp, color = supp)) + 
  geom_boxplot() +
  geom_smooth(method = lm, se=F) +
  xlab("dose") +
  ylab("len")