首页 > 解决方案 > 在您的条形图中制作具有多个堆叠部分的黑白 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)

在此先感谢您的任何建议!

标签: rggplot2

解决方案


一个更手动的选项(以@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)

推荐阅读