我有以下代码。我想更改箱线图的颜色,以便它们都具有相同的填充颜色(灰色)。另外,我想让 stat_summary 文本粘贴到每个条形图的底部,但 vjust 似乎只提供相对位置?
谢谢
boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
geom_bar(position = "dodge", stat = "summary", fun.y = "median") +
geom_boxplot(outlier.shape = NA, width=0.2, color = "black", position = position_dodge(0.9)) +
stat_summary(aes(label=round(..y..,2)), fun.y=median, geom="text", size=8, col = "white", vjust=8, position = position_dodge(0.9)) +
stat_summary(fun.y=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
boxp
Run Code Online (Sandbox Code Playgroud)
这是一个可能的解决方案,但它需要 ggplot v3.3.0 的功能stage()。
指出主要变化:
fill = 'gray'stage()计算统计数据,但随后用作0实际放置。library(ggplot2)
library(scales)
ggplot(mtcars, aes(as.factor(cyl), wt,
group = interaction(as.factor(cyl), as.factor(am)))) +
geom_bar(aes(fill=as.factor(am)), position = "dodge", stat = "summary", fun = "median") +
geom_boxplot(outlier.shape = NA, width=0.2,
color = "black", fill = 'gray',
position = position_dodge(0.9)) +
stat_summary(aes(label=round(after_stat(y), 2), y = stage(wt, after_stat = 0)),
fun=median, geom="text", size=8, col = "white", vjust=-0.5,
position = position_dodge(0.9)) +
stat_summary(fun=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-05-06 创建
| 归档时间: |
|
| 查看次数: |
3396 次 |
| 最近记录: |