r - 在您的条形图中制作具有多个堆叠部分的黑白 ggplot 条形图的最佳方法?
问题描述
我正在尝试使用 ggplot 为仅以黑白打印的出版物制作带有堆叠条形的条形图。每个条形图有 6 个堆栈,因此使用scale_fill_grey()
会使它看起来很奇怪且难以阅读。有没有更好、更清洁的方法来做到这一点?
这是我的数据:
dput(head(Russets))
structure(list(Variety = structure(c(4L, 4L, 4L, 4L, 4L, 5L), .Label = c("Burbank 2018",
"Norkotah 2018", "Silverton 2018", "Burbank 2019", "Norkotah 2019",
"Silverton 2019"), class = "factor"), Rate = c("1", "2", "3",
"4", "5", "1"), Hollow = c("67", "65", "64", "63", "67", "73"
), Double = c("42", "43", "42", "43", "48", "56"), Knob = c("351",
"391", "362", "348", "357", "371"), AllCWT.AC = c("AVGCWT.AC.0.4",
"AVGCWT.AC.0.4", "AVGCWT.AC.0.4", "AVGCWT.AC.0.4", "AVGCWT.AC.0.4",
"AVGCWT.AC.0.4"), ValueCWT.AC = c(224.92342125, 226.91992125,
226.967565, 237.4482825, 227.80791, 168.32446125), TuberSize = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0-4 oz", "4-6 oz", "6-10 oz",
"10-13 oz", "13+ oz", "Culls"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
这是我目前的ggplot:
RussetPlot <-ggplot(data=Russets,aes(x=Rate,y=ValueCWT.AC,group=interaction(TuberSize,Variety),fill=(TuberSize))) +
geom_col(position = "stack",inherit.aes = TRUE) +
facet_wrap(~Variety,nrow=2) +
scale_color_discrete(name = "Tuber Sizes", labels = c("0-4 oz", "4-6 oz", "6-10 oz", "10-13 oz", "13+ oz", "Culls"))
print(RussetPlot)
在此先感谢您的任何建议!
解决方案
一个更手动的选项(以@dc37 的答案为基础):
使用scale_fill_manual()
ggplot(df, aes(x = variable, y = number, fill = fact))+
geom_col(color = "black)+
theme_bw()+
scale_fill_manual(values = c("grey10","grey30","grey50","grey70","grey80","grey90"))
数据
fact = rep(LETTERS[1:6],9)
number = rnorm(54)
variable = c(rep("a",18),rep("b",18),rep("c",18))
df = data.frame(fact,number, variable)
推荐阅读
- reactjs - .env 文件和 create-react-app 的问题 - 返回未定义
- byte - 来自字节串的 Scapy 以太网数据包丢失了顶层信息
- android - 无法修复错误:找不到符号类 ActivityMainBindingImpl
- android - 在android中使用网络提供的时间(不是默认系统时间)
- symfony - Symfony 5 security.interactive_login 事件未调用
- reactjs - 如何强制 Gatsby 将特定 URL 路径重定向到外部站点?
- python - TypeError:无法解压不可迭代的 NoneType 对象:找不到我的数据在哪里变成“无类型”
- html - Angular 从 innerHTML 中去除数据属性
- javascript - 遍历 Javascript 对象并检查属性是否有子数据
- json - 使用 jq 从多个文件聚合 json 数组,按键分组