3

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?

Community
  • 1
  • 1
OFish
  • 474
  • 1
  • 9
  • 19
  • 3
    have you tried `geom_point(aes(col=manufacturer), position = "jitter")` ? – David Arenburg Apr 02 '14 at 10:22
  • Heena: Thanks for adding the bold writing. Didn't know I had to do that. @David: Hadn't tried that, but it works a charm. THANKS! But is there an explanation as to why jitter adds black dots? – OFish Apr 02 '14 at 10:57
  • 2
    Because black color is its default. You've added another object without specifying how to color it. When you do that in the same object (`geom_point`), ggplot knows how to color it – David Arenburg Apr 02 '14 at 11:01

2 Answers2

8

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()
Midnighter
  • 3,771
  • 2
  • 29
  • 43
  • Thanks for that. Didn't actually think of defining the color in the gpplot() function itself. DUH! Personally, I prefer however just to define the "simple" x and y's in the ggplot() and then start building the plot with additional aesthetics thereafter.But its good to know that multiple roads lead to rome! Also thanks for the explanation David Arenburg. Makes total sense. – OFish Apr 04 '14 at 00:40
2

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

ggplot(mpg,aes(cty,hwy,color=manufacturer))+geom_jitter();
Naveen G
  • 25
  • 2