使用 ggplot2 将颜色列添加到 geom_bar 图中

Add color column to geom_bar plot with ggplot2

我需要帮助才能将吓人的颜色添加到 ggplot2 图的 geom_bar 图中。

这是我目前能做到的:

    head(data)
  x  y group Axis_color
1 A 25     F         G1
2 A 88     G         G1
3 A 88     H         G1
4 A 22     I         G1
5 A 18     J         G1
6 B 54     F         G3

color_list<- c("F"="orange", "G"="darkgreen", "H"="grey", "I"="pink", "J"="purple", "G1"="blue","G2"="red","G3"="green")
ggplot(data, aes(x = x, y = y, fill = group, label = y)) + 
  coord_flip()+
  geom_bar(stat = "identity")

给出:

但我想添加列“Axis_color”以添加存储在 color_list 中的颜色,例如:

如您所见,我也得到了两个不同的图例轴。

如果有帮助的话,这里是数据:

structure(list(x = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "E", 
"E", "E", "E", "E"), y = c(25, 88, 88, 22, 18, 54, 25, 37, 68, 
72, 36, 84, 17, 64, 48, 15, 17, 72, 61, 25, 66, 10, 18, 99, 63
), group = c("F", "G", "H", "I", "J", "F", "G", "H", "I", "J", 
"F", "G", "H", "I", "J", "F", "G", "H", "I", "J", "F", "G", "H", 
"I", "J"), Axis_color = c("G1", "G1", "G1", "G1", "G1", "G3", 
"G3", "G3", "G3", "G3", "G1", "G1", "G1", "G1", "G1", "G2", "G2", 
"G2", "G2", "G2", "G3", "G3", "G3", "G3", "G3")), row.names = c(NA, 
-25L), class = "data.frame")
library(ggnewscale)
library(tidyverse)

data <- structure(list(x = c(
  "A", "A", "A", "A", "A", "B", "B", "B",
  "B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "E",
  "E", "E", "E", "E"
), y = c(
  25, 88, 88, 22, 18, 54, 25, 37, 68,
  72, 36, 84, 17, 64, 48, 15, 17, 72, 61, 25, 66, 10, 18, 99, 63
), group = c(
  "F", "G", "H", "I", "J", "F", "G", "H", "I", "J",
  "F", "G", "H", "I", "J", "F", "G", "H", "I", "J", "F", "G", "H",
  "I", "J"
), Axis_color = c(
  "G1", "G1", "G1", "G1", "G1", "G3",
  "G3", "G3", "G3", "G3", "G1", "G1", "G1", "G1", "G1", "G2", "G2",
  "G2", "G2", "G2", "G3", "G3", "G3", "G3", "G3"
)), row.names = c(
  NA,
  -25L
), class = "data.frame")

color_list <- c("F" = "orange", "G" = "darkgreen", "H" = "grey", "I" = "pink", "J" = "purple", "G1" = "blue", "G2" = "red", "G3" = "green")


data %>%
  as_tibble() %>%
  ggplot(aes(x, y)) +
  geom_bar(aes(fill = group), stat = "identity") +
  scale_fill_manual(
    values = color_list[names(color_list) %>% discard(~ .x %>% str_starts("G[0-9]"))]
  ) +
  new_scale_fill() +
  geom_bar(aes(fill = Axis_color, y = -10), stat = "identity") +
  scale_fill_manual(
    values = color_list[names(color_list) %>% keep(~ .x %>% str_starts("G[0-9]"))]
  ) +
  coord_flip()

reprex package (v2.0.1)

于 2021-09-25 创建

实现您想要的结果的一个选择是像这样使用 geom_tileggnewscale 包。

  1. 对于 geom_tile,我使用了一个简化的数据集,其中仅包含唯一或不同的类别和轴颜色值。根据您想要的结果,您可以通过 width 参数调整图块的宽度。
  2. 因为 geom_tile 默认情况下会导致图例键的外观略有不同,我使用 guide_legendoverride.aes 参数,以便图例键与对于 geom_col

请注意,我切换到 geom_col 并切换了 xy 美学的角色,从而摆脱了 coord_flip

library(ggplot2)
library(ggnewscale)

color_list<- c("F"="orange", "G"="darkgreen", "H"="grey", "I"="pink", "J"="purple", "G1"="blue","G2"="red","G3"="green")
ggplot(data, aes(x = y, y = x, fill = group, label = y)) + 
  geom_col() +
  scale_x_continuous(expand = c(.025, 0)) +
  scale_fill_manual(values = color_list[unique(data$group)], guide = guide_legend(order = 1)) +
  new_scale_fill() +
  geom_tile(data = dplyr::distinct(data, x, Axis_color), aes(x = -15, y = x, fill = Axis_color), 
            height = .9, width = 15, inherit.aes = FALSE) +
  scale_fill_manual(values = color_list[sort(unique(data$Axis_color))], 
                    guide = guide_legend(order = 2, override.aes = list(lwd = .5, color = NA)))