简体   繁体   English

在ggplot2中均匀地水平分布点

[英]Spread points evenly horizontally in ggplot2

I want to draw vertical boxplots of counts, and show the counts as points, overlaid over the boxplots. 我想绘制计数的垂直箱图,并将计数显示为点,覆盖在箱线图上。 Because they are discrete values, there are going to be multiple points with the same value. 因为它们是离散值,所以会有多个具有相同值的点。 To show the data in ggplot2, I could use geom_jitter() to spread the data and get a slightly better impression, but jitter screws up the values (the vertical component), and the randomness of the horizontal spread means that if the jitter height is set to 0, there is still a high chance of overlapping points. 为了在ggplot2中显示数据,我可以使用geom_jitter()来传播数据并获得稍微好一些的印象,但是抖动会使值(垂直分量)上升,而水平扩展的随机性意味着如果抖动高度是设置为0,重叠点的可能性很高。

Is there a way to spread all points which have the same value, evenly and horizontally? 有没有办法均匀地和水平地传播具有相同值的所有点? Something along the lines of this: 有点像这样:

在此输入图像描述

Here is some example data: 以下是一些示例数据:

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))

在此输入图像描述

While this isn't exactly what the image looks like it can be adjusted to get there. 虽然这不完全是图像的样子,但可以调整它以达到目的。 geom_dotplot (added in version 0.9.0-ish I think) does this: 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))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM