简体   繁体   中英

ggplots2: Colour and position of geom_point() per factor

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 ya factor (A or B), and za 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?

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.

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