2013-12-12 64 views
6

我有下面的代码生成一个箱线均匀分布的所有点:GGPLOT2:箱线与连续

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个点,中点正好在中心,其他两个点在右边。

我尝试:

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

但这并没有保持点并拢。此外,在某些情况下,它会将多个点放在框中间的右侧或左侧,而不是按照我的意愿平均分配它们。

回答

8

您可以使用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) 

enter image description here

+0

优秀的,正是我想要的。谢谢。 – Danny