ggplot2:箱形图,所有点均连续分布 [英] ggplot2: boxplot with all points distributed evenly in a row

查看:167
本文介绍了ggplot2:箱形图,所有点均连续分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用以下代码生成的箱线图:

I have a boxplot generated with the following code:

b.males <- c(6, 7, 8, 8, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 14, 15)
b.females <- c(14, 13, 12, 12, 11, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 7, 7, 7, 7)
b.total<-c(b.males,b.females)

b.m<-data.frame(b.males)
b.f<-data.frame(b.females)
b.t<-data.frame(b.total)

myList<-list(b.m, b.f, b.t)
df<-melt(myList)

colnames(df) <- c("class","count")
plt<-ggplot(df, aes(x=class,y=count))+geom_boxplot() 
plt + geom_point(aes(x = as.numeric(class) + 0, colour=class))

我想做的是,对于任何给定y轴点,连续显示所有单个点。例如,对于b.males,我希望在8处看到3个点,中间的点恰好在中心,而另外两个点在其左右两侧。

What I'd like to do is, for any given y-axis point, show all individual points in a row. For example, for b.males, I'd like to see 3 dots at 8, with the middle dot exactly in the center and the other two dots right next to it on either side.

我尝试过:

plt + geom_point(aes(x = as.numeric(class) + 0, colour=class)) +
      geom_jitter(position=position_jitter(width=.1, height=0))

但是,这并没有使各点靠得很近。此外,在某些情况下,它会将多个点放在框中间的右侧或左侧,而不是按照我的意愿均匀地分布它们。

But this did not keep the points close together. Additionally, in some cases it would put multiple points to the right or left of the middle of the box, not distributing them evenly as I'd like.

推荐答案

您可以使用 geom_dotplot()添加点-参数为 binaxis = y 您的值根据y值(个计数)和参数 stackdir = center 进行装箱,以确保点居中。要更改点的大小,请使用参数 dotsize =

You can use geom_dotplot() to add points - with arguments binaxis="y" your values binned according to y values (counts) and argument stackdir="center" ensures that dots are centered. To change the size of points use argument dotsize=

ggplot(df,aes(class,count))+geom_boxplot()+
  geom_dotplot(aes(fill=class),binaxis="y",stackdir="center",dotsize=0.5)

这篇关于ggplot2:箱形图,所有点均连续分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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