用ggplot2重叠分组箱形图中的平均点 [英] Overdraw mean points in grouped boxplot with ggplot2
本文介绍了用ggplot2重叠分组箱形图中的平均点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像ggplot2这样的分组boxplot
pre $ p $ qplot(factor(cyl),mpg,data = mtcars,geom =boxplot,fill = factor(齿轮))
我想透支平均分。我试过这个
p + stat_summary(fun.y = mean,color =red,geom =point)
但是,我没有在每个组的框中获得平均值。
什么我应该怎么做到这一点?
解决方案
您应该设置位置为 position_dodge()
和 width
设置为0.75 - 所以点的放置方式与boxplots相同。
qplot(factor(cyl),mpg,data = mtcars,geom =boxplot,fill = factor(gear))+
stat_summary(fun.y = mean,color =red ,
geom =point,position = position_dodge(width = 0.75))
I have a grouped boxplot with ggplot2 like this
p <- qplot(factor(cyl), mpg, data=mtcars, geom="boxplot", fill=factor(gear))
and I want to overdraw the mean points. I've tried this
p+ stat_summary(fun.y=mean, colour="red", geom="point")
but, I don't get the mean points over the boxes of each group.
What should I do to get this?
解决方案
You should set position to position_dodge()
and width
to 0.75 - so points will be placed in the same way as boxplots.
qplot(factor(cyl), mpg, data=mtcars, geom="boxplot", fill=factor(gear)) +
stat_summary(fun.y=mean, colour="red",
geom="point",position=position_dodge(width=0.75))
这篇关于用ggplot2重叠分组箱形图中的平均点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文