假设我有以下数据框:
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))
Run Code Online (Sandbox Code Playgroud)
我的目标是通过多种因素做一个箱形图:
p<- ggplot(data = df, aes(x = factor(delta), y = value)) +
geom_boxplot(aes(fill=factor(metric)))
Run Code Online (Sandbox Code Playgroud)
输出是:
到目前为止一切都很好,但如果我这样做:
p+ geom_point(aes(color = factor(metric)))
Run Code Online (Sandbox Code Playgroud)
我明白了:
我不知道它在做什么.我的目标是在这里完成异常值的着色.请注意,此解决方案将框的内部颜色更改为白色,并将边框设置为不同的颜色.我希望保持相同颜色的盒子,同时让异常值继承这些颜色.我想知道如何使异常值从各自的箱形图中获得相同的颜色.
您只想更改异常值的颜色吗?如果是这样,您可以通过绘制两次箱线图来轻松完成。
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
Run Code Online (Sandbox Code Playgroud)
[已编辑]
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)
Run Code Online (Sandbox Code Playgroud)