5

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.

Community
  • 1
  • 1
nhern121
  • 3,831
  • 6
  • 27
  • 40

2 Answers2

4

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)
cuttlefish44
  • 6,586
  • 2
  • 17
  • 34
  • This is what I was looking for. But what if I want to set my own colors for each one. I tried `colss <- c(P="firebrick3",R="skyblue", C="mediumseagreen") ` and then do `p+scale_fill_manual(values = colss)` and it is not working. – nhern121 Oct 13 '16 at 12:55
  • @Nestorghh; 1st `geom_boxplot()` draws the outliers with `aes(colour)` and 2nd one draws boxes (and they cover over first boxes) with `aes(fill)`. So this method needs both `scale_fill_manual` and `scale_colour_manual` to change colors of boxes and outliers. – cuttlefish44 Oct 13 '16 at 13:36
0

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

Haboryme
  • 4,611
  • 2
  • 18
  • 21