2

I have a data matrix in the form of:

x   y   z
1   A   T
1.5 B   T
2   A   T
5   B   T
7   A   T

... and so on. So my x is a continuous variable, and y a factor (A or B), and z a factor (of only 1 level T). Now I want to draw a boxplot of variable x, and lay on top each data point coloured by y.

ggplot(data, aes(x=z, y=x)) + geom_boxplot() + geom_point(aes(colour = data$y))

Since my dataset is bigger than the example above, I have a lot of overlaying dots that are coloured. Therefore I would like to position the data points for each factor (y) next to each other, so that the data points for the factors don't overlap (the data points within a factor can of course). How do I do that?

rororo
  • 815
  • 16
  • 31

1 Answers1

0

I think what you need is adding some jitter : https://ggplot2.tidyverse.org/reference/position_jitter.html

This way the points won't overlap anymore.

Yvonnig
  • 69
  • 7
  • jitter positions the points randomly. What I need is a jitter per factor – rororo Nov 28 '18 at 14:23
  • Then I think the answer is here : https://stackoverflow.com/questions/10493084/ggplot2-jitter-and-position-dodge-together – Yvonnig Nov 28 '18 at 15:23