2014-03-06 65 views
3

我想绘制垂直方块图的计数,并将计数显示为点,叠加在箱形图上。因为它们是离散值,所以会有多个具有相同值的点。为了在ggplot2中显示数据,我可以使用geom_jitter()来传播数据并获得稍微好的印象,但抖动将数值(垂直分量)拧紧,并且水平扩展的随机性意味着如果抖动高度是设置为0,重叠点的可能性很高。在ggplot2中水平均匀分布点

有没有办法将所有具有相同值的点均匀分布并水平分布?沿此线的东西:

enter image description here

下面是一些示例数据:

wine_votes <- melt(list(a=c(7,7,7,8,8,7,7,8,4,7,7,6,8,6), 
         b=c(5,8,6,4,3,4,4,9,5,8,4,5,4), 
         c=c(7.5,8,5,8,6,8,5,6,6.5,7,5,5,6), 
         d=c(4,4,5,5,6,8,5,8,5,6,3,6,5), 
         e=c(7,4,6,7,4,6,7,5,6.5,8.5,8,5) 
         )) 
names(wine_votes) <- c('vote', 'option') 

# Example plot with jitter: 
ggplot(wine_votes, aes(x=blend, y=vote)) + 
    geom_boxplot() + geom_jitter(position=position_jitter(height=0, width=0.2)) + 
    scale_y_continuous(breaks=seq(0,10,2)) 

enter image description here

+1

这个问题没有一个可重复的数据集,所以它真的只对你有用,而不是未来的搜索。你在['geom_dotplot']之后(http://docs.ggplot2.org/current/geom_dotplot.html)。 –

+0

@TylerRinker:见编辑 – naught101

+0

你可以摆脱'geom_jitter'并添加如下内容:'geom_dotplot(binaxis =“y”,stackdir =“center”,aes(fill = option))'? –

回答

7

虽然这是不完全的形象是什么样子,可以调整到到达那里。 geom_dotplot(版本0.9.0上下的,我认为加)做到这一点:

ggplot(wine_votes, aes(x=option, y=vote)) + 
    geom_boxplot() + 
    scale_y_continuous(breaks=seq(0,10,2)) + 
    geom_dotplot(binaxis = "y", stackdir = "center", aes(fill=option)) 

enter image description here

+0

这是一个很酷的工具。我发现默认尺寸太大了。传递给'geom_dotplot()'的'dotsize = 0.5'参数修复了这个问题。谢谢! – naught101