ggplot2中的交互图 [英] Interaction Plot in 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 = )
print(p)
我如何绘制剂量补充水平组合的方式,而不是只有剂量水平的手段,我在这里?提前感谢您的帮助。
您可以预先计算自己的数据框中的值:
(牙齿增长,(dose,supp),总结,val =平均(len))
ggplot(ToothGrowth,aes( x =因子(剂量),y = len,color = supp))+
geom_boxplot()+
geom_point(data = toothInt,aes(y = val))+
geom_line = toothInt,aes(y = val,group = supp))+
theme_bw()
请注意,使用 ggplot
而不是 qplot
使图表构造更加清晰,适用于更复杂的图表(如IMHO)。
I'm trying to make interaction plot with ggplot2
. My code is below:
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)
How can I plot dose-supp level combination means rather than only dose level means which I'm getting here? Thanks in advance for your help.
You can precalculate the values in their own data frame:
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()
Note that using ggplot
rather than qplot
makes the graph construction a lot clearer for more complex plots like these (IMHO).
这篇关于ggplot2中的交互图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!