手动更改ggbarplot的条形图颜色,与填充变量无关

ryoqjall  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(87)

我得到以下数据集:
| 瓦尔|类型|年|perc| perc_SE|
| --------------|--------------|--------------|--------------|--------------|
| 米格|格萨姆特|2016年|零点三五|零点零二|
| 米格|格萨姆特|二〇二一|0.42|零点零三|
| 米格|克拉塞|2016年|0.06|零点|
| 米格|克拉塞|二〇二一|零点一七|零点|
| cbooksl|格萨姆特|2016年|零点六七|零点零一|
| cbooksl|格萨姆特|二〇二一|0.68|零点零二|
| cbooksl|克拉塞|2016年|0.79|零点|
| cbooksl|克拉塞|二〇二一|0.65|零点|
到目前为止,我使用了以下代码:

cbbPalette <- c("#00a0a0", "#a3dee1")

data$var <- factor(data$var, levels = c("mig","cbooksl")
data$type <- factor(data$type, levels = c("Klasse", "Gesamt"))

ggplot(data, aes(x=type, width = 0.8, y=perc, fill=factor(year, c("2021", "2016")))) + 
  geom_bar(position=position_dodge(width=0.9), stat="identity", color = "black") + 
  geom_errorbar(data=subset(data, perc_SE > 0), aes(ymin=perc - 2*perc_SE, ymax=perc + 2*perc_SE), width=.3,
                position=position_dodge(.9)) +
  geom_text(aes(label =paste(as.character(round(perc * 100)), "%"), y = 0.03), colour = "black",
            position=position_dodge(width=0.8), fontface="bold", size=3.25) + 
  theme_bw() + 
  geom_text(aes(y=-0.039, label=year, fontface="bold"), position=position_dodge(width=1), color="black", size=3) + 
  theme(legend.position = "none", title = element_blank(),
        axis.title.y=element_blank(), axis.text.y = element_text(angle = 90, vjust = -27, hjust=0.5),
        axis.ticks.y=element_blank(), axis.line.x = element_line(colour = "black", 
        size = 0.1, linetype = "solid"),   
        strip.text.y = element_text(angle=0), panel.border = element_blank(), strip.background = element_blank(),
        panel.grid.major.y = element_blank(), panel.spacing = unit(0, "lines")) +
  coord_flip() +
  scale_fill_manual(values=cbbPalette)  +
  facet_grid(var~., switch = "y", margins = F) + 
  scale_y_continuous(breaks=c(0,0.2,0.4,0.6,0.8,1), minor_breaks = c(0,0.2,0.4,0.6,0.8,1),
                     sec.axis = sec_axis(~. *1, breaks = c(0,0.2,0.4,0.6,0.8,1),
          labels = c("0%","20%","40%","60%","80%","100%")), position = "right",
        labels=c("0%","20%", "40%", "60%", "80%", "100%"), limits = c(-0.039,1))

这给了我以下情节:

我如何调整条形图的颜色,使它们的颜色取决于类型向量的值,而不改变其他任何东西?或者,我如何手动调整颜色,使2个连续的酒吧交替具有相同的颜色?
我尝试使用这个调色板矢量:

cbbPalette <- c("#00a0a0", "#00a0a0", "#a3dee1", "#a3dee1")

这会使所有的条形图都显示为“#00a0a0”。
我试图在ggplot中定义变量“type”作为填充参数,这破坏了整个图。

dhxwm5r4

dhxwm5r41#

如果我理解正确的话,你想在fill上Maptype,但在group美学上Mapyear

library(ggplot2)

cbbPalette <- c("#00a0a0", "#a3dee1")

ggplot(data, aes(
  x = type, width = 0.8, y = perc,
  group = factor(year, c("2021", "2016")),
  fill = type
)) +
  geom_bar(
    position = position_dodge(width = 0.9),
    stat = "identity", color = "black"
  ) +
  geom_errorbar(
    data = subset(data, perc_SE > 0), aes(
      ymin = perc - 2 * perc_SE,
      ymax = perc + 2 * perc_SE
    ), width = .3,
    position = position_dodge(.9)
  ) +
  geom_text(
    aes(label = paste(as.character(round(perc * 100)), "%"), y = 0.03),
    colour = "black",
    position = position_dodge(width = 0.8), fontface = "bold", size = 3.25
  ) +
  theme_bw() +
  geom_text(
    aes(y = -0.039, label = year, fontface = "bold"),
    position = position_dodge(width = 1), color = "black", size = 3
  ) +
  theme(
    legend.position = "none", title = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y = element_text(angle = 90, vjust = -27, hjust = 0.5),
    axis.ticks.y = element_blank(),
    axis.line.x = element_line(
      colour = "black",
      size = 0.1, linetype = "solid"
    ),
    strip.text.y = element_text(angle = 0),
    panel.border = element_blank(), strip.background = element_blank(),
    panel.grid.major.y = element_blank(), panel.spacing = unit(0, "lines")
  ) +
  coord_flip() +
  scale_fill_manual(values = cbbPalette) +
  facet_grid(var ~ ., switch = "y", margins = F) +
  scale_y_continuous(
    breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1),
    minor_breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1),
    sec.axis = sec_axis(~ . * 1,
      breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1),
      labels = c("0%", "20%", "40%", "60%", "80%", "100%")
    ), position = "right",
    labels = c("0%", "20%", "40%", "60%", "80%", "100%"),
    limits = c(-0.039, 1)
  )

资料

data <- data.frame(
  stringsAsFactors = FALSE,
  var = c(
    "mig", "mig", "mig", "mig",
    "cbooksl", "cbooksl", "cbooksl", "cbooksl"
  ),
  type = c(
    "Gesamt", "Gesamt", "Klasse",
    "Klasse", "Gesamt", "Gesamt", "Klasse", "Klasse"
  ),
  year = c(2016L, 2021L, 2016L, 2021L, 2016L, 2021L, 2016L, 2021L),
  perc = c(0.35, 0.42, 0.06, 0.17, 0.67, 0.68, 0.79, 0.65),
  perc_SE = c(0.02, 0.03, 0, 0, 0.01, 0.02, 0, 0)
)

相关问题