user248237
user248237

Reputation:

controlling point colors with geom_jitter and geom_boxplot in ggplot2 in R

I have the following boxplot in ggplot2 to which I add the points plotted with geom_jitter:

p <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(aes(colour=factor(cyl))) + geom_jitter(aes(color=factor(cyl)))

I colored the individual points according to factor(cyl) which works great. However, some points still appear as black. What are these? are these the outlier to the boxplots? If so, it's strange since some of them are just as far from median as the colored points (which are not outliers), but perhaps that is explained by the randomness of geom_jitter?

can someone please explain if that's the correct explanation, and also, how can I make the outliers go away if I use geom_jitter? thanks.

Upvotes: 2

Views: 5441

Answers (1)

marbel
marbel

Reputation: 7714

The black point is the outlier of the boxplot.

Plotting just the box plot you can see that.

ggplot(mtcars, aes(cyl, mpg)) + 
   geom_boxplot(aes(fill=as.factor(cyl)), outlier.size = 0) 

Setting outlier.size = 0 does the job of getting rid of the outlier dot. You can change colours also. Check out ?geom_boxplot for more details.

ggplot(mtcars, aes(cyl, mpg)) + 
   geom_boxplot(aes(fill=as.factor(cyl)), outlier.size = 0) +
   geom_jitter(color=factor(cyl))

Upvotes: 3

Related Questions