添加多个图例到组合图 ggplot2

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

Adding multiple legend to combined graph ggplot2

问题

我有这样的数据:

head(data,10)
   Ratio Wealthness Cash Quality Variants
1      A          A    E       G        r
2      D          B    A       C        u
3      C          B    C       E        s
4      A          C    E       A        s
5      D          B    A       D        y
6      C          B    D       D        x
7      B          C    D       D        v
8      D          C    C       D        y
9      B          C    B       A        x
10     A          A    D       G        u

并根据Rui Barradas的解决方案创建了一个组合图:

library(ggplot2)
library(tidyr)

color_clrs <- c(
    A = "white",
    B = "black",
    C = "black",
    D = "white",
    E = "black",
    F = "white",
    G = "white"
)

fill_clrs <- c(
    A = "#1f3560",
    B = "#B0C4DE",
    C = "#f2f3f3",
    D = "#ff0000",
    E = "#A9A9A9",
    F = "#B22222",
    G = "#1E90FF"
)

ggplot(data %>% pivot_longer(-Variants), aes(Variants, fill = value)) +
    geom_bar(position = "fill") +
    geom_text(stat = "count", aes(label = after_stat(count), color = value), 
              position = position_fill(vjust = 0.5), show.legend = FALSE) +
    facet_wrap(~name) +
    scale_x_discrete(limit = rev) +
    scale_y_continuous(trans = "reverse") +
    scale_fill_manual(values = fill_clrs) +
    scale_color_manual(values = color_clrs) +
    coord_flip() +
    theme_classic() +
    theme(legend.position = "top",
          axis.ticks.x = element_blank(),
          axis.text.x = element_blank())

输出图像:

添加多个图例到组合图 ggplot2

我的问题是如何为每一列创建一个具有颜色和组(A-E)的单独图例? 在这个问题中,这不是真实的数据示例,在实际情况下,一些组可以是1到1000,甚至在这种情况下很难识别颜色。或者可能只有在变量下的所有变体中有一次出现某个字母,因此如果能够看到该变量的组是什么将更容易理解。

英文:

I have such type of the data:

head(data,10)
   Ratio Wealthness Cash Quality Variants
1      A          A    E       G        r
2      D          B    A       C        u
3      C          B    C       E        s
4      A          C    E       A        s
5      D          B    A       D        y
6      C          B    D       D        x
7      B          C    D       D        v
8      D          C    C       D        y
9      B          C    B       A        x
10     A          A    D       G        u

And created a combined plot adapting the solution by Rui Barradas provided to my previous question :

library(ggplot2)
library(tidyr)

color_clrs &lt;- c(
    A = &quot;white&quot;,
    B = &quot;black&quot;,
    C = &quot;black&quot;,
    D = &quot;white&quot;,
    E = &quot;black&quot;,
    F = &quot;white&quot;,
    G = &quot;white&quot;
)

fill_clrs &lt;- c(
    A = &quot;#1f3560&quot;,
    B = &quot;#B0C4DE&quot;,
    C = &quot;#f2f3f3&quot;,
    D = &quot;#ff0000&quot;,
    E = &quot;#A9A9A9&quot;,
    F = &quot;#B22222&quot;,
    G = &quot;#1E90FF&quot;
)

ggplot(data %&gt;% pivot_longer(-Variants), aes(Variants, fill = value)) +
    geom_bar(position = &quot;fill&quot;) +
    geom_text(stat = &quot;count&quot;, aes(label = after_stat(count), color = value), 
              position = position_fill(vjust = 0.5), show.legend = FALSE) +
    facet_wrap(~name) +
    scale_x_discrete(limit = rev) +
    scale_y_continuous(trans = &quot;reverse&quot;) +
    scale_fill_manual(values = fill_clrs) +
    scale_color_manual(values = color_clrs) +
    coord_flip() +
    theme_classic() +
    theme(legend.position = &quot;top&quot;,
          axis.ticks.x = element_blank(),
          axis.text.x = element_blank())

The output:

添加多个图例到组合图 ggplot2

My question how to make separate legend with colors and groups (A-E) to each column? It is not real data example in this question, in reality some groups can be 1 to 1000 and it's even difficult to spot color in such cases. Or it can be that only to one time some letter appeared for all variants under the variable, so it will be more understandable if there would be seen what groups can be for that variable.

答案1

得分: 1

你可以将你的图表转换成一个函数(代码与你写的相同,我只是删除了 facet_wrappivot_longer 部分),在每个子集中应用它,然后使用 purrr::map2 组合列表中的所有图表,最后使用 patchwork::wrap_plots


library(patchwork)

ind_plot <- function(data, plot_titles)
  data %>%
  ggplot(aes(Variants, fill = value)) +
  geom_bar(position = "fill") +
  geom_text(stat = "count", aes(label = after_stat(count), color = value), 
            position = position_fill(vjust = 0.5), show.legend = FALSE) +
  scale_x_discrete(limit = rev) +
  scale_y_continuous(trans = "reverse") +
  scale_fill_manual(values = fill_clrs) +
  scale_color_manual(values = color_clrs) +
  coord_flip() +
  theme_classic() +
  theme(legend.position = "top",
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank()) + 
  labs(title = plot_titles)

  data %>%
  pivot_longer(-Variants) %>%
  split(.$name) %>%
  purrr::map2(names(.), ind_plot) %>%
  patchwork::wrap_plots()  

添加多个图例到组合图 ggplot2

此代码于 2023-06-19 使用 reprex v2.0.2 创建

英文:

You can transform your plot in a function (is the same code that You wrote, I just removed facet_wrap and pivot_longer parts), apply it in every subset with purrr::map2 and then combine all the plots from the list with patchwork::wrap_plots


library(patchwork)

ind_plot &lt;- function(data, plot_titles)
  data %&gt;% 
  ggplot(aes(Variants, fill = value)) +
  geom_bar(position = &quot;fill&quot;) +
  geom_text(stat = &quot;count&quot;, aes(label = after_stat(count), color = value), 
            position = position_fill(vjust = 0.5), show.legend = FALSE) +
  scale_x_discrete(limit = rev) +
  scale_y_continuous(trans = &quot;reverse&quot;) +
  scale_fill_manual(values = fill_clrs) +
  scale_color_manual(values = color_clrs) +
  coord_flip() +
  theme_classic() +
  theme(legend.position = &quot;top&quot;,
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank()) + 
  labs(title = plot_titles)

  data %&gt;% 
  pivot_longer(-Variants) %&gt;% 
  split(.$name) %&gt;% 
  purrr::map2(names(.), ind_plot) %&gt;% 
  patchwork::wrap_plots()  

添加多个图例到组合图 ggplot2<!-- -->

<sup>Created on 2023-06-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月19日 16:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504698.html
匿名

发表评论

匿名网友

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

确定