Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color outliers multiple factors in boxplot

Tags:

r

ggplot2

boxplot

Let's say I have the following data frame:

library(ggplot2)

set.seed(101)
n=10
df<- data.frame(delta=rep(rep(c(0.1,0.2,0.3),each=3),n), metric=rep(rep(c('P','R','C'),3),n),value=rnorm(9*n, 0.0, 1.0))

My goal is to do a boxplot by multiple factors:

p<- ggplot(data = df, aes(x = factor(delta), y = value)) + 
geom_boxplot(aes(fill=factor(metric)))

The output is:

enter image description here

So far so good, but if I do:

p+ geom_point(aes(color = factor(metric))) 

I get:

enter image description here

I do not know what it is doing. My goal is to color the outliers as it is done here. Note that this solution changes the inside color of the boxes to white and set the border to different colors. I want to keep the same color of the boxes while having the outliers inherit those colors. I want to know how to make the outliers get the same colors from their respective boxplots.

like image 648
nhern121 Avatar asked Oct 19 '22 00:10

nhern121


1 Answers

Do you want just to change the outliers' colour ? If so, you can do it easily by drawing boxplot twice.

p <- ggplot(data = df, aes(x = factor(delta), y = value)) + 
  geom_boxplot(aes(colour=factor(metric))) +
  geom_boxplot(aes(fill=factor(metric)),  outlier.colour = NA)
                                        # outlier.shape = 21  # if you want a boarder

enter image description here

[EDITED]
colss <- c(P="firebrick3",R="skyblue", C="mediumseagreen")
p + scale_colour_manual(values = colss) +   # outliers colours
    scale_fill_manual(values = colss)       # boxes colours

 # the development version (2.1.0.9001)'s geom_boxplot() has an argument outlier.fill,
 # so I guess under code would return the similar output in the near future.
p2 <- ggplot(data = df, aes(x = factor(delta), y = value)) + 
  geom_boxplot(aes(fill=factor(metric)),  outlier.shape = 21, outlier.colour = NA)
like image 105
cuttlefish44 Avatar answered Oct 27 '22 08:10

cuttlefish44