ggplot2: How do you control point color with geom_

2019-07-07 12:59发布

问题:

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:

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

gmpg+geom_point(aes(col=manufacturer))+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?

回答1:

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()


回答2:

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

ggplot(mpg,aes(cty,hwy,color=manufacturer))+geom_jitter();


标签: r ggplot2