2011-09-06 77 views
20

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

回答

30

您可以在自己的数据帧预先计算值:

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使得图构建一个更加清晰的更复杂的情节像这些(恕我直言)。

+0

非常感谢。这是我正在寻找的。再次感谢。 – MYaseen208

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

+0

感谢Brian Diggs的回复。如何为点表示不同的颜色点? – MYaseen208

+0

对于点,颜色的默认映射被覆盖为“蓝色”,所以只需从'stat_summary'调用中移除'color =“blue”',并且它将默认回到基于'supp'的映射。变量。 –

+0

谢谢Brain Diggs。 – MYaseen208

7

如果你认为你可能需要一个更通用的方法,你可以尝试在包HandyStuff(github.com/bryanhanson/HandyStuff)功能rxnNorm。免责声明:我是作者。免责声明#2:box plot选项不太适合,但所有其他选项都可以。

下面是一个使用ToothGrowth数据为例:

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

rxnNorm Demo

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

这正确地绘制线条而不是箱形图。 – hpesoj626