Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does geom_jitter replicates black points when we use aes colour?

I came across this issue while analyzing my data and I was able to replicate it with the example of the official ggplot reference.

This code creates black points that seem to be the original points before jitter was applied with collors:

ggplot(mpg, aes(cyl, hwy)) + 
  geom_point() +
  geom_jitter(aes(colour = class))

However, this code works fine, it doesn’t show the black points:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

p <- ggplot(mpg, aes(cyl, hwy))
p + geom_point()
p + geom_jitter(aes(colour = class))

I was thinking it may be related to geom_point printing the black dots before geom_jitter, but if this is the case, why does it work fine in the second example, which follows the same order?

This is the image of the black points

>Solution :

geom_jitter is merely a convenience function, it is calling geom_point under the hood. Because of this, your use of p + geom_point() + geom_jitter(aes(color=class)) is actually the same as

ggplot(ggplot2::mpg, aes(cyl, hwy)) + 
  geom_point() +
  geom_point(aes(color = class), position = "jitter")

which is plotting the same points twice. You can clarify this a little by changing the color of the original points:

ggplot(ggplot2::mpg, aes(cyl, hwy)) + 
  geom_point(color = "red") +
  geom_jitter(aes(color = class))

ggplot of mpg with red points

If you want jittered points, use either geom_point(position = "jitter") or geom_jitter(), not both.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading