应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

huangapple go评论104阅读模式
英文:

How to apply separate colors for multiple geom_bars that is based on three different x-axis values and filled with two different factors

问题

以下是您要翻译的内容:

这是使用下面的数据生成的绘图(为了复制,抱歉提供了较长的数据示例)

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

它是用以下代码创建的

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

有12个单独的geom_bar(),它们当前由包含两种可能因子的填充颜色着色(我的意思是,6个geom_bar()与另外6个geom_bar()堆叠在一起)。

问题:
我能手动更改每个12个geom_bar()的颜色吗?

类似于

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

数据

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

Let's say I have this plot generated with the data below (apologies for the long data sample, but it was needed to reproduce)

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

It has been made with this code

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

There are 12 individual geom_bar(), which are currently colored by a fill that contains two possible factors (I mean, 6 geom_bar() stacked with 6 other geom_bar().

Question:
Can I manually change the color of each 12 geom_bar()?

Something like

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

Data

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

答案1

得分: 4

手动为柱形图上色或将每个条形图映射到特定值,例如 paste(speclib, digest_method, frak, sep = ".") 或对 fill 和/或 color 上的三个变量进行交互映射,并额外映射 forcats::fct_rev(speclib)group aes。随后您可以定义自己的调色板。在下面的代码中,我使用了六个基本颜色的不同色调来创建12种颜色。此外,我添加了一个虚拟的 geom_col 来获得一个 alpha 图例,以显示哪个 speclib 类别对应较浅/较深的色调。

英文:

To color your bars manually or individually map e.g. paste(speclib, digest_method, frak, sep = ".") or the interaction of the three variables on fill and/or color and additionally map forcats::fct_rev(speclib) on the group aes.

Afterwards it's up to you to define your color palette. In the code below I use shades of six base colors to create 12 colors. Additionally I added a fake geom_col to get an alpha legend to show which speclib category refers to the lighter/darker shade.

  1. library(ggplot2)
  2. pal <- scales::brewer_pal(type = "qual")(6)
  3. pal <- c(colorspace::darken(pal, .4), colorspace::lighten(pal, .4))
  4. ggplot(q, aes(
  5. x = digest_method, y = diff,
  6. color = paste(speclib, digest_method, frak, sep = "."),
  7. group = forcats::fct_rev(speclib),
  8. fill = paste(speclib, digest_method, frak, sep = "."),
  9. )) +
  10. geom_col(
  11. aes(alpha = forcats::fct_rev(speclib)),
  12. fill = "transparent"
  13. ) +
  14. geom_col() +
  15. geom_text(
  16. aes(
  17. y = diff,
  18. label = protein_total
  19. ),
  20. color = "black",
  21. vjust = 1.5,
  22. position = position_stack(),
  23. show.legend = FALSE
  24. ) +
  25. scale_fill_manual(
  26. values = pal, aesthetics = c("color", "fill"),
  27. guide = "none"
  28. ) +
  29. facet_wrap(~frak,
  30. nrow = 1
  31. ) +
  32. guides(alpha = guide_legend(
  33. override.aes = list(
  34. alpha = 1,
  35. fill = c(
  36. colorspace::lighten("grey", .4),
  37. colorspace::darken("grey", .4)
  38. )
  39. )
  40. ))

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

答案2

得分: 2

以下是代码中的翻译部分:

另一种选项是:

在这里,我们创建了一个带有12个值的辅助列,并将随机生成的颜色分配给填充美学:

  1. library(ggplot2)
  2. random_colors <- sample(colors(), 12)
  3. q %>%
  4. ungroup() %>%
  5. mutate(helper = row_number()) %>%
  6. ggplot(aes(x = digest_method, y = diff,
  7. fill = factor(helper))) +
  8. geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
  9. geom_text(aes(y = protein_total,
  10. label = protein_total),
  11. vjust = rep(c(1.5, -0.5), 6),
  12. show.legend = FALSE) +
  13. facet_wrap(~ frak,
  14. nrow = 1) +
  15. scale_fill_manual(values = random_colors) +
  16. theme(legend.position = "none")

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

英文:

Another option is:

Here we create a helper column with 12 values and assign then randomly generated colors to the fill aesthetics:

  1. library(ggplot2)
  2. random_colors &lt;- sample(colors(), 12)
  3. q %&gt;%
  4. ungroup() %&gt;%
  5. mutate(helper = row_number()) %&gt;%
  6. ggplot(aes(x = digest_method, y = diff,
  7. fill = factor(helper))) +
  8. geom_bar(stat = &quot;identity&quot;, position = position_stack(reverse = TRUE)) +
  9. geom_text(aes(y = protein_total,
  10. label = protein_total),
  11. vjust = rep(c(1.5, -0.5), 6),
  12. show.legend = FALSE) +
  13. facet_wrap(~ frak,
  14. nrow = 1) +
  15. scale_fill_manual(values = random_colors) +
  16. theme(legend.position = &quot;none&quot;)

应用不同颜色于基于三个不同X轴值且填充有两种不同因素的多个geom_bar。

huangapple
  • 本文由 发表于 2023年5月18日 04:43:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定