2013-12-12 63 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