3

I am making a boxplot conditioned by a factor similar to this example:

p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(aes(fill = factor(am)))

There are few points in the data set, and I'd like to express this visually by overlaying the data points. I want to overlay the points colored by the same factor "am" which I try to do like this:

p + geom_boxplot(aes(fill = factor(am))) + geom_jitter(aes(colour = factor(am)))

The points are colored by the factor "am" but not spaced to lay only over the box plots they are associated with. Rather they mix and cover both. Does anyone know how the condition the geom_jitter so the points associate with the factor "am"?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
bump2pass
  • 55
  • 1
  • 5

2 Answers2

3

Welcome to SO! Here's my attempt. It's a bit clumsy, but does the job. The trick is to map x to a dummy variable with manually constructed offset. I'm adding a fill scale to highlight point positioning.

mtcars$cylpt <- as.numeric(factor(mtcars$cyl)) + ifelse(mtcars$am == 0, -0.2, 0.2)
ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot(aes(fill = factor(am))) + 
  geom_point(aes(x = cylpt, colour = factor(am)), position = "jitter") +
  scale_fill_manual(values = c("white", "gray"))

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Thanks Tony. That worked great. I was able to apply this to my problem. but There is something I don't understand. When using geom_point(), how does ggplot2 know to map the points to mpg on the y axis? The aes mapping has an x (cylpt), but how does it know to carry over the y (mpg) from the previous ggplot command? – bump2pass Jun 05 '15 at 16:37
  • Quite simple: if you define some mapping in `ggplot` call (as you do for `x=factor(cyl)`, `y=mpg`, though `x` and `y` are omitted), then it becomes the default for any subsequent geom call, in your example for both `geom_boxplot` and `geom_point`. If that helped, feel free to mark the answer as accepted by hitting the green tick mark next to it. – tonytonov Jun 06 '15 at 15:30
-1

I have found this link that solves your problem:

https://datavizpyr.com/how-to-make-grouped-boxplot-with-jittered-data-points-in-ggplot2/

geom_jitter(position = position_jitterdodge())

jianwu
  • 1