can one offset jitter points in ggplot boxplot

笑着哭i 提交于 2019-12-19 08:13:37

问题


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

produces the plot below.

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.


回答1:


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



来源:https://stackoverflow.com/questions/8510003/can-one-offset-jitter-points-in-ggplot-boxplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!