r - 添加 scale_fill_grey 会更改条形,但不会更改 ggplot2 中的线
问题描述
我的数据如下:
counts <- structure(list(ECOST = c("0.08", "0.19", "0.07", "0.21", "0.06",
"0.20", "0.08", "0.19", "0.07", "0.21", "0.06", "0.20", "0.08",
"0.19", "0.07", "0.21", "0.06", "0.20", "0.08", "0.19", "0.07",
"0.21", "0.06", "0.20"), variable = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("treatment", "control"), class = "factor"),
percentage = c(0.714495882305909, 0.769654513612482, 0.803015075376884,
0.778858962374387, 0.824455825864277, 0.743992307222663,
0.731115523760302, 0.767143914637009, 0.775154121121069,
0.766219224551454, 0.800973964325206, 0.767750444842938,
0.281818181818182, 0.418181818181818, 0.5, 0.354545454545455,
0.5, 0.372727272727273, 0.329896907216495, 0.43298969072165,
0.536082474226804, 0.402061855670103, 0.597938144329897,
0.443298969072165), type = c("Contributions", "Contributions",
"Contributions", "Contributions", "Contributions", "Contributions",
"Contributions", "Contributions", "Contributions", "Contributions",
"Contributions", "Contributions", "Choices", "Choices", "Choices",
"Choices", "Choices", "Choices", "Choices", "Choices", "Choices",
"Choices", "Choices", "Choices"), group = c("Contributions control",
"Contributions control", "Contributions control", "Contributions control",
"Contributions control", "Contributions control", "Contributions treatment",
"Contributions treatment", "Contributions treatment", "Contributions treatment",
"Contributions treatment", "Contributions treatment", "Choices control",
"Choices control", "Choices control", "Choices control",
"Choices control", "Choices control", "Choices treatment",
"Choices treatment", "Choices treatment", "Choices treatment",
"Choices treatment", "Choices treatment")), row.names = c(NA,
-24L), class = c("data.table", "data.frame"))
library(ggplot2)
ggplot() +
geom_bar(data=filter(counts, group %in% c("Choices control", "Choices treatment")), aes(x = ECOST, y = percentage, fill=group), stat ="identity", position="dodge")+
geom_point(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")),aes(x = ECOST, y = percentage,colour=group)) +
geom_line(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")), aes(x = ECOST, y = percentage,colour=group, group=group)) +
theme_bw(base_size = 15)
创建此图:
我想制作一个使用灰色阴影的版本。但是,当我这样做时:
ggplot() +
geom_bar(data=filter(counts, group %in% c("Choices control", "Choices treatment")), aes(x = ECOST, y = percentage, fill=group), stat ="identity", position="dodge")+
geom_point(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")),aes(x = ECOST, y = percentage,colour=group)) +
geom_line(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")), aes(x = ECOST, y = percentage,colour=group, group=group)) +
theme_bw(base_size = 15) +
scale_fill_grey(start = 0.8, end = 0.5)
只有条改变,而不是线条。我如何改变线条?
解决方案
添加scale_color_grey()
将使线条颜色变为灰色阴影,例如
ggplot() +
geom_bar(data=subset(counts, group %in% c("Choices control", "Choices treatment")), aes(x = ECOST, y = percentage, fill=group), stat ="identity", position="dodge")+
geom_point(data=subset(counts, group %in% c("Contributions control", "Contributions treatment")),aes(x = ECOST, y = percentage,colour=group)) +
geom_line(data=subset(counts, group %in% c("Contributions control", "Contributions treatment")), aes(x = ECOST, y = percentage,colour=group, group=group)) +
theme_bw(base_size = 15) +
scale_fill_grey(start = 0.8, end = 0.5)+
scale_color_grey()
scale_fill_grey()
将以灰色阴影渲染箱形图、条形图、小提琴图等,同时scale_color_grey()
用于线条和点。
推荐阅读
- c - 试图理解 C 中的递归
- c++ - 使用 set-width 对齐列
- python - 通过wifi在两个系统上的两个python脚本之间进行通信
- python - 无法让我的程序返回主循环
- openocd - Openocd 错误:命令名称“dap”无效 - 无法通过 ST-Link/V2 连接 Blue Pill
- c - C中的二进制到文本文件转换
- docker - 可以在 kubernetes 中将本地图像用于 pod yaml 吗?
- c++ - C++Builder6 中的 Random_shuffle 替代方案
- java - Java LinkedList 用于存储和排序用户输入。请协助
- javascript - 使用 Node.js 将多个条目插入 SQL Server