Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: How do you control point color with geom_jitter and geom_point?

Tags:

r

ggplot2

This is related to the question here. However, I'm not trying to make a boxplot, but a scatterplot in ggplot2, but adding the argument geom_jitter() adds black dots which seem to be non-related to my dataset.

Here's an example using the mpg data pack:

This is a simple scatterplot, that looks a bit "too clean"

gmpg<-ggplot(data=mpg, aes(x=hwy, y=cty))
gmpg+geom_point(aes(col=manufacturer))

Which produces this: Regular scatterplot, with no jitter

Now, if I add the argument jitter, this is what happens

gmpg+geom_point(aes(col=manufacturer))+geom_jitter()

plot with geom_jitter

I've tried reducing the alpha etc., but the black dots remain. What on earth are these and how do I remove them?

like image 965
OFish Avatar asked Dec 04 '25 10:12

OFish


2 Answers

There is no need to assign a new aesthetic mapping in the geom_* functions. This should work:

gmpg <- ggplot(data=mpg, aes(x=hwy, y=cty, col=manufacturer))
gmpg + geom_point() + geom_jitter()
like image 73
Midnighter Avatar answered Dec 06 '25 00:12

Midnighter


There is no need to add geom_point() in it,geom_jitter() is enough.

ggplot(mpg,aes(cty,hwy,color=manufacturer))+geom_jitter();
like image 37
Naveen G Avatar answered Dec 06 '25 00:12

Naveen G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!