nhern121
nhern121

Reputation: 3921

Color outliers multiple factors in 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.

Upvotes: 5

Views: 3308

Answers (2)

cuttlefish44
cuttlefish44

Reputation: 6786

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)

Upvotes: 4

Haboryme
Haboryme

Reputation: 4761

Maybe this:

ggplot(data = df, aes(x = as.factor(delta), y = value,fill=as.factor(metric))) + 
  geom_boxplot(outlier.size = 1)+ geom_point(pch = 21,position=position_jitterdodge(jitter.width=0))

enter image description here

Upvotes: 0

Related Questions