简体   繁体   中英

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

在此处输入图片说明

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

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

在此处输入图片说明

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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