r - 更改 ggboxplot 框轮廓颜色
问题描述
如何向箱线图条添加黑色轮廓?如果我指定 color="black" 文本将彼此重叠。我也想把文字的颜色改成黑色
数据:
COUNTRY ACTUAL YEAR
<chr> <<dbl> <dbl>
1 U.S.A. 9.42 1996
2 U.S.A. 208 1998
3 U.S.A. 454. 1996
4 U.S.A. 21.2 1996
5 U.S.A. 566 1997
6 Mexico 370. 1995
7 U.S.A. 845. 1996
8 Canada 492 1998
9 U.S.A. 1352. 1998
10 U.S.A. 103. 1996
...
代码:
df %>%
select(YEAR, COUNTRY, ACTUAL) %>%
mutate(YEAR = as.factor(YEAR)) %>%
group_by(COUNTRY, YEAR) %>%
ggsummarystats(
., x = c("COUNTRY"), y = c("ACTUAL"),
palette=custom_pal,
ggfunc = ggboxplot,
color="YEAR",
fill = "YEAR",
summaries = c("n", "mean", "median", "min", "max")
)
制作图片
如果我编辑
color=rep("black", 12)
否则看起来不错,但汇总统计信息堆叠在一起,R 会发出警告:
Warning messages:
1: In if (color %in% names(data) & is.null(add.params$color)) add.params$color <- color :
the condition has length > 1 and only the first element will be used
2: In if (color %in% colnames(df)) { :
the condition has length > 1 and only the first element will be used
解决方案
将输出分配给一个对象后,您可以分别更改两个图的颜色,我们称之为p
。更改p$main.plot
会更改绘图轮廓和误差线,更改p$summary.plot
会更改文本颜色。
p <- df %>%
select(YEAR, COUNTRY, ACTUAL) %>%
mutate(YEAR = as.factor(YEAR)) %>%
group_by(COUNTRY, YEAR) %>%
ggsummarystats(
., x = c("COUNTRY"), y = c("ACTUAL"),
palette=custom_pal,
ggfunc = ggboxplot,
color="YEAR",
fill = "YEAR",
summaries = c("n", "mean", "median", "min", "max")
)
n <- length(unique(df$YEAR))
p$main.plot <- p$main.plot + scale_color_manual(values=rep("black", n))
p$summary.plot <- p$summary.plot + scale_color_manual(values=rep("black", n))
p
推荐阅读
- c++ - 使用 portaudio 作为带有 cmake 的 git 子模块的问题
- python - 我的 Django 更新表单没有保存,也没有从患者注册时使用的详细信息中将实例显示为患者
- bison - 标记一系列两位数字,后跟一到三位数字,而不在词法分析器中使用其他状态?
- django - 在 Mac OS 上将 dockerized Django 与 MySQL 连接时出错
- c++ - CMake:设置(CMAKE_CXX_STANDARD_REQUIRED True)导致配置错误
- python - Python生成器表达式递归
- python - 一直运行到 url 不存在或不响应的循环
- django - 在Django模板中的if条件中使用外键值
- javascript - 从 forEach 中的 setTimout 获取值
- reactjs - ts(7016) 找不到模块“cloudinary-react”的声明文件。如果存在,请尝试 `npm i --save-dev @types/cloudinary-react` 或添加新的...?