散点图和Boxplot叠加 [英] Scatter plot and boxplot overlay

查看:760
本文介绍了散点图和Boxplot叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于以前的帖子 ggplot与散点图叠加(相同变量)的箱形图

我想每周有一个boxplot ,而不是两个boxlot 有不同颜色的分散点。



代码如下:

<$ p $
plot1< -ggplot(data = dodgers,aes(x = ordered_day_of_week,y = Attend))+ geom_boxplot()
$
plot2< -ggplot(dodgers,aes(x = ordered_month,y = Attend,color = Bobblehead,size = 1.5))+ geom_point()

#Box-ploy with Scatter plot overlay
plot3< -ggplot(data = dodgers,aes(x = ordered_day_of_week,y = Attend,color = Bobblehead))+ geom_boxplot()+ geom_point()

结果是:

1,scat ter plot





2,boxplot plot


<3>,合并后的图
color = 在<$ c $里面放置 c> aes() of geom_point()并从 ggplot() AES()。如果您在 ggplot()中放入 color = ,那么它会影响所有的几何。



mtcars 数据作为OP的例子没有提供数据。

  ggplot(mtcars,aes(factor(cyl),mpg))+ geom_boxplot()+ 
geom_point (aes(color = factor(am)),position = position_dodge(width = 0.5))

< img src =https://i.stack.imgur.com/0JXRj.pngalt =在这里输入图片描述>


Based on the previous post ggplot boxplots with scatterplot overlay (same variables),

I would like to have one boxplot for each day of week instead of two boxplots while have scatter points on it with different colour.

The code will be like:

#Box-plot for day of week effect
plot1<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend)) + geom_boxplot()

#Scatter with specific colors for day of week
plot2<-ggplot(dodgers, aes(x=ordered_month, y=Attend, colour=Bobblehead, size=1.5)) + geom_point()

#Box-ploy with Scatter plot overlay
plot3<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend, colour=Bobblehead)) + geom_boxplot() + geom_point()

And the result would be:
1, scatter plot

2, boxplot plot

3, combined plot

解决方案

Put color= inside the aes() of geom_point() and remove it from ggplot() aes(). If you put color= inside the ggplot() then it affects all geoms. Also you could consider to use position dodge to separate points.

Example with mtcars data as OP didn't provide data.

ggplot(mtcars,aes(factor(cyl),mpg))+geom_boxplot()+
  geom_point(aes(color=factor(am)),position=position_dodge(width=0.5))

这篇关于散点图和Boxplot叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆