可以抵消ggplot boxplot中的抖动点 [英] can one offset jitter points in ggplot boxplot
问题描述
在ggplot boxplot
中,使用抖动很容易添加具有不同程度抖动的原始数据点。对于零抖动,下面的代码:
In a ggplot boxplot
, it is easy to use jitter to add the raw data points with varying degrees of jitter. With zero jitter the following code
dat <- data.frame(group=c('a', 'b', 'c'), values = runif(90))
ggplot(dat, aes(group, values)) +
geom_boxplot(outlier.size = 0) +
geom_jitter(position=position_jitter(width=0), aes(colour=group), alpha=0.7) +
ylim(0, 1) + stat_summary(fun.y=mean, shape=3, col='red', geom='point') +
opts(legend.position = "right") + ylab("values") + xlab("group")
产生下面的图。
是否可以使用零抖动,但添加一个偏移量使得点排成一行,但向左移动了框宽度的25%?我用 dodge
尝试了 geom_point
,但是这产生了一个抖动。
Is it possible to use zero jitter but add an offset such that the points are in a line but shifted left by 25% of the box width? I tried geom_point
with dodge
but this generated a jitter.
推荐答案
如果我们将组转换为数字,然后添加一个偏移量,你似乎得到你想要的输出。这可能是一种更有效的方法,但可以这样做:
If we convert group to numeric and then add an offset, you seem to get your desired output. There is probably a more effective / efficient way, but give this a whirl:
ggplot(dat, aes(group, values)) +
geom_boxplot(outlier.size = 0) +
geom_point(aes(x = as.numeric(group) + .25, colour=group), alpha=0.7) +
ylim(0, 1) + stat_summary(fun.y=mean, shape=3, col='red', geom='point') +
opts(legend.position = "right") + ylab("values") + xlab("group")
这篇关于可以抵消ggplot boxplot中的抖动点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!