fede_luppi
fede_luppi

Reputation: 1171

Overlap measurement points on a grouped geom_boxplot() using ggplot2

my data looks like this

d1 <- data.frame(BAI2013 = rnorm(30),
                 class = rep(letters[1:3], 10),
                 treatment = rep(c("elevated","ambient"),15)) 

I plot the following boxplot, including the points of the measurements and removing the outliers:

p<- (ggplot(d1, aes(x = class, y = BAI2013)))

p + geom_boxplot(outlier.size = 0, aes(fill=factor(treatment))) + 
  geom_point(aes(color = factor(treatment)))

enter image description here

The problem is that, as you can see, the points drawn in the x axis corresponding to class, whereas I want the points overlapping each box instead of the center of each group. Thanks

Upvotes: 0

Views: 1728

Answers (2)

alexwhan
alexwhan

Reputation: 16026

If you are happy for them to be centred, you can use position_dodge():

p + geom_boxplot(outlier.size = 0, aes(fill=factor(treatment))) + 
  geom_point(aes(color = factor(treatment)), position = position_dodge(width = 0.75))

enter image description here

If you want them jittered, it gets trickier, but it's possible

Upvotes: 1

colcarroll
colcarroll

Reputation: 3682

You might try starting with

p<- (ggplot(d1, aes(x = interaction(class, treatment), y = BAI2013)))

You'll then have to think of what you want the labels/ordering to actually look like, but everything will be lined up.

Upvotes: 1

Related Questions