箱线图中的颜色异常值多个因素

Color outliers multiple factors in boxplot

假设我有以下数据框:

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

我的目标是根据多个因素绘制箱线图:

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

输出为:

到目前为止一切顺利,但如果我这样做:

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

我得到:

我不知道它在做什么。我的目标是在完成时为异常值着色 here. Note that this solution 将框的内部颜色更改为白色并将边框设置为不同的颜色。我想保持框的颜色相同,同时让异常值继承这些颜色。我想知道如何使异常值从各自的箱线图中获得相同的颜色。

也许是这样:

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

您只想更改离群值的颜色吗?如果是这样,你可以通过绘制两次箱线图轻松完成。

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

[编辑]
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)