ggplot2:boxplot,所有点均匀分布在一行中

Dan*_*nny 6 r ggplot2 boxplot

我有一个使用以下代码生成的boxplot:

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))
Run Code Online (Sandbox Code Playgroud)

我想做的是,对于任何给定的y轴点,显示连续的所有单个点.例如,对于b.males,我想在8处看到3个点,中间点恰好在中心,另外两个点在它旁边.

我试过:

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

但这并没有让这些观点紧密相连.此外,在某些情况下,它会在盒子中间的右侧或左侧放置多个点,而不是按照我的意愿均匀分布它们.

Did*_*rts 9

您可以使用geom_dotplot()添加点 - 使用参数binaxis="y"根据y值(counts)和参数stackdir="center"确定值,这可确保点居中.要改变点的大小,请使用参数dotsize=

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

在此输入图像描述