0

Edited with sample data:

When I try to plot a grouped boxplot together with jittered points using position=position_jitterdodge(), and add an additional group indicated by e.g. shape, I end up with a graph where the jittered points are misaligned within the individual groups:

enter image description here

n <- 16
data <- data.frame(
 age = factor(rep(c('young', 'old'), each=8)),
 group=rep(LETTERS[1:2], n/2),
 yval=rnorm(n)
 )

 ggplot(data, aes(x=group, y=yval))+
   geom_boxplot(aes(color=group), outlier.shape = NA)+
   geom_point(aes(color=group, shape=age, fill=group),size = 1.5, position=position_jitterdodge())+
   scale_shape_manual(values = c(21,24))+
   scale_color_manual(values=c("black", "#015393"))+
   scale_fill_manual(values=c("white", "#015393"))+
   theme_classic()

Is there a way to suppress that additional separation?

Thank you!

ic23oluk
  • 125
  • 1
  • 9
  • 1
    How does this code make a graph? I don't see any data being supplied. – Ian Campbell Jun 10 '21 at 15:46
  • 1
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. I can't tell what you mean by them being in the wrong groups, and can't test the code without the data – camille Jun 10 '21 at 16:59
  • Thank you camille for the instructions. I edited the code and provided data. Hope you see my point here. Thanks in advance for any constructive tips on how to fix this. – ic23oluk Jun 11 '21 at 10:25

1 Answers1

1

OP, I think I get what you are trying to explain. It seems the points are grouped according to age, rather than treated as the same for each group. The reason for this is that you have not specified what to group together. In order to jitter the points, they are first grouped together according to some aesthetic, then the jitter is applied. If you don't specify the grouping, then ggplot2 gives it a guess as to how you want to group the points.

In this case, it is grouping according to age and group, since both are defined to be used in the aesthetics (x=, fill=, and color= are assigned to group and shape= is assigned to age).

To define that you only want to group the points by the column group, you can use the group= aesthetic modifier. (reposting your data with a seed so you see the same thing)

set.seed(8675309)
n <- 16
data <- data.frame(
  age = factor(rep(c('young', 'old'), each=8)),
  group=rep(LETTERS[1:2], n/2),
  yval=rnorm(n)
)

ggplot(data, aes(x=group, y=yval))+
  geom_boxplot(aes(color=group), outlier.shape = NA)+
  geom_point(aes(color=group, shape=age, fill=group, group=group),size = 1.5, position=position_jitterdodge())+
  scale_shape_manual(values = c(21,24))+
  scale_color_manual(values=c("black", "#015393"))+
  scale_fill_manual(values=c("white", "#015393"))+
  theme_classic()

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32