如何为基于三个不同x轴值并使用两个不同因子填充的多个geom_bars应用单独的颜色

i34xakig  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(165)

假设我用下面的数据生成了这个图(抱歉数据样本太长了,但是需要重新生成)

已经用这个代码做出来了

ggplot(q, aes(x = digest_method, y = diff,
                   color = forcats::fct_rev(speclib), fill = forcats::fct_rev(speclib))) +
  geom_bar(stat = "identity", position = "stack") +
  geom_text(aes(y = protein_total,
                label = protein_total),
            vjust = rep(c(1.5, -0.5), 6),
            show.legend = FALSE) +
  facet_wrap(~ frak,
             nrow = 1)

有12个单独的geom_bar(),它们当前由包含两个可能因子的填充着色(我的意思是,6个geom_bar()与其他6个geom_bar()堆叠。

**问题:*是否可以手动更改 * 每12geom_bar()的颜色?

就像

数据

q <- structure(list(digest_method = c("Direct", "Direct", "Direct", 
                                      "Direct", "In solution", "In solution", "In solution", "In solution", 
                                      "PAC", "PAC", "PAC", "PAC"), protein_total = c(780L, 1173L, 948L, 
                                                                                     1526L, 891L, 1444L, 1442L, 1880L, 947L, 1704L, 1598L, 2433L), 
                    speclib = c("No", "SpecLib", "No", "SpecLib", "No", "SpecLib", 
                                "No", "SpecLib", "No", "SpecLib", "No", "SpecLib"), frak = structure(c(1L, 
                                                                                                       1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), levels = c("Raw", 
                                                                                                                                                               "20K Vesicles", "120K Vesicles", "Vesicles depleted"), class = "factor"), 
                    diff = c(780L, 393L, 948L, 578L, 891L, 553L, 1442L, 438L, 
                             947L, 757L, 1598L, 835L)), class = c("grouped_df", "tbl_df", 
                                                                  "tbl", "data.frame"), row.names = c(NA, -12L), groups = structure(list(
                                                                    frak = structure(c(1L, 1L, 1L, 2L, 2L, 2L), levels = c("Raw", 
                                                                                                                           "20K Vesicles", "120K Vesicles", "Vesicles depleted"), class = "factor"), 
                                                                    digest_method = c("Direct", "In solution", "PAC", "Direct", 
                                                                                      "In solution", "PAC"), .rows = structure(list(1:2, 5:6, 9:10, 
                                                                                                                                    3:4, 7:8, 11:12), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                    "vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
                                                                                                                                                                                                                                               "tbl", "data.frame"), .drop = TRUE))
3phpmpom

3phpmpom1#

要手动或单独为条形图着色,例如paste(speclib, digest_method, frak, sep = ".")fill和/或color上的三个变量的相互作用,并且另外将forcats::fct_rev(speclib)Map到group AES上。
之后,它是由你来定义你的调色板。在下面的代码中,我使用六种基本颜色的阴影来创建12种颜色。此外,我添加了一个假的geom_col来获得一个alpha图例,以显示哪个speclib类别指的是较亮/较暗的阴影。

library(ggplot2)

pal <- scales::brewer_pal(type = "qual")(6)
pal <- c(colorspace::darken(pal, .4), colorspace::lighten(pal, .4))

ggplot(q, aes(
  x = digest_method, y = diff,
  color = paste(speclib, digest_method, frak, sep = "."),
  group = forcats::fct_rev(speclib),
  fill = paste(speclib, digest_method, frak, sep = "."),
)) +
  geom_col(
    aes(alpha = forcats::fct_rev(speclib)),
    fill = "transparent"
  ) +
  geom_col() +
  geom_text(
    aes(
      y = diff,
      label = protein_total
    ),
    color = "black",
    vjust = 1.5,
    position = position_stack(),
    show.legend = FALSE
  ) +
  scale_fill_manual(
    values = pal, aesthetics = c("color", "fill"),
    guide = "none"
  ) +
  facet_wrap(~frak,
    nrow = 1
  ) +
  guides(alpha = guide_legend(
    override.aes = list(
      alpha = 1,
      fill = c(
        colorspace::lighten("grey", .4),
        colorspace::darken("grey", .4)
      )
    )
  ))

zsbz8rwp

zsbz8rwp2#

另一种选择是:
在这里,我们创建一个包含12个值的helper列,然后将随机生成的颜色分配给填充美学:

library(ggplot2)

random_colors <- sample(colors(), 12)

q %>% 
  ungroup() %>% 
  mutate(helper = row_number()) %>% 
  ggplot(aes(x = digest_method, y = diff,
                fill = factor(helper))) +
  geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
  geom_text(aes(y = protein_total,
                label = protein_total),
            vjust = rep(c(1.5, -0.5), 6),
            show.legend = FALSE) +
  facet_wrap(~ frak,
             nrow = 1) +
  scale_fill_manual(values = random_colors) +
  theme(legend.position = "none")

相关问题