Nestorghh - 10 months ago 81

R Question

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:

So far so good, but if I do:

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

I get:

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.

Answer Source

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
```

[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)
```