Maybe one offset of jitter points in ggplot boxplot

In ggplot, boxplotit is easy to use jitter to add source 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")

displays the graph below.

Is it possible to use zero jitter, but add an offset so that the points are in the line, but are shifted to the left by 25% of the window width? I tried geom_pointwith help dodge, but it caused a jitter.enter image description here

+5
source share
1 answer

If we convert the group to numeric and then add an offset, you seem to get the desired result. There is probably a more efficient / efficient way, but give this a whirlwind:

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

enter image description here

+7
source

All Articles