英文:
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())
输出图像:
我的问题是如何为每一列创建一个具有颜色和组(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 <- 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())
The output:
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_wrap
和 pivot_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()
此代码于 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 <- 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()
<!-- -->
<sup>Created on 2023-06-19 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论