Color rejects several 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 make boxplot a few factors:

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

Output:

enter image description here

So far so good, but if I do this:

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

I get:

enter image description here

I do not know what he is doing. My goal is to colorize the emissions, as is done here . Note that this solution changes the inner color of the boxes to white and sets the border to different colors. I want to keep the same color in the boxes, while having outliers that inherit these colors. I want to know how to make outliers with the same colors from the respective boxes.

+5
source share
2 answers

Do you want to just change the color of outliers? If so, you can do it easily by using the square 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) 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) 
+3
source

Perhaps it:

 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

0
source

Source: https://habr.com/ru/post/1258120/


All Articles