2

i use following code:

data(mtcars)
ggplot(mtcars, aes(x=factor(cyl), y=mpg)) +
  geom_jitter(aes(colour=factor(gear)), width = 0.1) +
  geom_boxplot(aes(fill=factor(gear)), alpha=0.6)

with following result: enter image description here

But i want the colored dots from geom_jitter directly behind the corresponding(!) boxplot. Is there a way to do it?

aosmith
  • 34,856
  • 9
  • 84
  • 118
user2083142
  • 431
  • 1
  • 6
  • 16
  • So you want both jittering and dodging? You can use `position_jitterdodge` for that. See [here](http://stackoverflow.com/questions/10493084/ggplot2-jitter-and-position-dodge-together) – aosmith Apr 19 '17 at 17:05
  • Thanks. position_jitterdodge within geom_point works well. – user2083142 Apr 19 '17 at 19:17

1 Answers1

5

Solution is position_jitterdodge as mentioned by aosmith and his link.

library(ggplot2)

data(mtcars)
ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(gear), colour=factor(gear))) +
  geom_point(position = position_jitterdodge()) +
  geom_boxplot(alpha=0.6)

The result looks like: enter image description here

user2083142
  • 431
  • 1
  • 6
  • 16