如何使用 ggplot2 更改我的多区域图的图例、国家/地区标签和大小?
how to change legend, country labels and size of my multiple area plots with ggplot2?
我试图更改我的图例和在其中一个区域图中多次出现的国家名称。
我。如您所见,图例出现了多次。我希望这个图例像上一张图片一样放置。对于两者:症状和合并症:
- 如果这可以在整个图上方显示为:症状:胸痛、发冷等以及合并症:哮喘、一型糖尿病等
二。每个区域地块的大小都不同
- 它们的大小应该相等。
三。还有国家标签。好吧,这些只出现一次。希望它们看起来像最后一张图片:
A - 印度,
B——巴基斯坦,
C - 英国,位于每组区域图上方的左侧。
这是我希望情节看起来像的示例
这是代码和假数据集:
sympt_count_plot <- ggplot2::ggplot(count_symptoms, ggplot2::aes(x = age_band, y = Count, group = symptoms, fill = symptoms)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
sympt_count_plot
sympt_percent_plot <- ggplot2::ggplot(count_symptoms, ggplot2::aes(x = age_band, y = Percent, group = symptoms, fill = symptoms)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
sympt_percent_plot
library(patchwork)
plot_sympt <- sympt_count_plot + sympt_percent_plot
plot_sympt
comorb_count_plot <- ggplot2::ggplot(count_comorbidities, ggplot2::aes(x = age_band, y = Count, group = comorbidities, fill = comorbidities)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
#viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
comorb_count_plot
comorb_percent_plot <- ggplot2::ggplot(count_comorbidities, ggplot2::aes(x = age_band, y = Percent, group = comorbidities, fill = comorbidities)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
#viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
comorb_percent_plot
plot_comorb <- comorb_count_plot + comorb_percent_plot
plot_comorb
plot_sympt + plot_comorb
有什么办法可以让我按我想要的方式获得它们。非常感谢帮助。
您可以使用cowplot::get_legend
收获传说,然后按照您喜欢的方式排列它们。这是一个完整的代表:
#加载包和数据
library(ggplot2)
library(patchwork)
git <- "https://github.com/gabrielburcea/Whosebug_fake_data/raw/master"
count_symptoms <- readr::read_csv(paste0(git, "/fake_symptoms.csv"))
count_comorbidities <- readr::read_csv(paste0(git, "/fake_comorbidities.csv"))
#情节 1
sympt_count_plot <- ggplot(count_symptoms) +
geom_area(aes(x = age_band, y = Count, group = symptoms, fill = symptoms),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"),
expand = c(0, 0)) +
scale_fill_viridis_d() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", hjust = 0)) +
facet_wrap(~Country, ncol = 1)
#情节2
sympt_percent_plot <- ggplot(count_symptoms) +
geom_area(aes(x = age_band, y = Percent, group = symptoms, fill = symptoms),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_viridis_d() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", color = "white")) +
facet_wrap(~Country, ncol = 1)
#情节3
comorb_count_plot <- ggplot(count_comorbidities) +
geom_area(aes(age_band, Count, group = comorbidities, fill = comorbidities),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"),
expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_brewer(palette = "Oranges") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", color = "white")) +
facet_wrap(~Country, ncol = 1)
#情节4
comorb_percent_plot <- ggplot(count_comorbidities) +
geom_area(aes(age_band, Percent, group = comorbidities, fill = comorbidities),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"),
expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_brewer(palette = "Oranges") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", color = "white")) +
facet_wrap(~Country, ncol = 1)
# 将图拼接在一起
plot_sympt <- sympt_count_plot + theme(legend.position = "none") +
sympt_percent_plot + theme(legend.position = "none")
plot_comorb <- comorb_count_plot + theme(legend.position = "none") +
comorb_percent_plot + theme(legend.position = "none")
plot_legend <- wrap_plots(
cowplot::get_legend(sympt_percent_plot),
cowplot::get_legend(comorb_percent_plot),
ncol = 1)
wrap_plots(plot_sympt, plot_comorb, plot_legend,
nrow = 1, widths = c(2, 2, 1))
由 reprex package (v0.3.0)
于 2020-11-14 创建
我试图更改我的图例和在其中一个区域图中多次出现的国家名称。
我。如您所见,图例出现了多次。我希望这个图例像上一张图片一样放置。对于两者:症状和合并症:
- 如果这可以在整个图上方显示为:症状:胸痛、发冷等以及合并症:哮喘、一型糖尿病等
二。每个区域地块的大小都不同
- 它们的大小应该相等。
三。还有国家标签。好吧,这些只出现一次。希望它们看起来像最后一张图片:
A - 印度, B——巴基斯坦, C - 英国,位于每组区域图上方的左侧。
这是我希望情节看起来像的示例
这是代码和假数据集:
sympt_count_plot <- ggplot2::ggplot(count_symptoms, ggplot2::aes(x = age_band, y = Count, group = symptoms, fill = symptoms)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
sympt_count_plot
sympt_percent_plot <- ggplot2::ggplot(count_symptoms, ggplot2::aes(x = age_band, y = Percent, group = symptoms, fill = symptoms)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
sympt_percent_plot
library(patchwork)
plot_sympt <- sympt_count_plot + sympt_percent_plot
plot_sympt
comorb_count_plot <- ggplot2::ggplot(count_comorbidities, ggplot2::aes(x = age_band, y = Count, group = comorbidities, fill = comorbidities)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
#viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
comorb_count_plot
comorb_percent_plot <- ggplot2::ggplot(count_comorbidities, ggplot2::aes(x = age_band, y = Percent, group = comorbidities, fill = comorbidities)) +
ggplot2::geom_area( color = "white") +
ggplot2::scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
#viridis::scale_fill_viridis(discrete = TRUE) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
ggplot2::facet_grid(Country ~.)
comorb_percent_plot
plot_comorb <- comorb_count_plot + comorb_percent_plot
plot_comorb
plot_sympt + plot_comorb
有什么办法可以让我按我想要的方式获得它们。非常感谢帮助。
您可以使用cowplot::get_legend
收获传说,然后按照您喜欢的方式排列它们。这是一个完整的代表:
#加载包和数据
library(ggplot2)
library(patchwork)
git <- "https://github.com/gabrielburcea/Whosebug_fake_data/raw/master"
count_symptoms <- readr::read_csv(paste0(git, "/fake_symptoms.csv"))
count_comorbidities <- readr::read_csv(paste0(git, "/fake_comorbidities.csv"))
#情节 1
sympt_count_plot <- ggplot(count_symptoms) +
geom_area(aes(x = age_band, y = Count, group = symptoms, fill = symptoms),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"),
expand = c(0, 0)) +
scale_fill_viridis_d() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", hjust = 0)) +
facet_wrap(~Country, ncol = 1)
#情节2
sympt_percent_plot <- ggplot(count_symptoms) +
geom_area(aes(x = age_band, y = Percent, group = symptoms, fill = symptoms),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"), expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_viridis_d() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", color = "white")) +
facet_wrap(~Country, ncol = 1)
#情节3
comorb_count_plot <- ggplot(count_comorbidities) +
geom_area(aes(age_band, Count, group = comorbidities, fill = comorbidities),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"),
expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_brewer(palette = "Oranges") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", color = "white")) +
facet_wrap(~Country, ncol = 1)
#情节4
comorb_percent_plot <- ggplot(count_comorbidities) +
geom_area(aes(age_band, Percent, group = comorbidities, fill = comorbidities),
color = "white") +
scale_x_discrete(limits = c( "0-19" ,"20-39", "40-59","60+"),
expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_brewer(palette = "Oranges") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.background = element_blank(),
strip.text = element_text(size = 14, face = "bold", color = "white")) +
facet_wrap(~Country, ncol = 1)
# 将图拼接在一起
plot_sympt <- sympt_count_plot + theme(legend.position = "none") +
sympt_percent_plot + theme(legend.position = "none")
plot_comorb <- comorb_count_plot + theme(legend.position = "none") +
comorb_percent_plot + theme(legend.position = "none")
plot_legend <- wrap_plots(
cowplot::get_legend(sympt_percent_plot),
cowplot::get_legend(comorb_percent_plot),
ncol = 1)
wrap_plots(plot_sympt, plot_comorb, plot_legend,
nrow = 1, widths = c(2, 2, 1))
由 reprex package (v0.3.0)
于 2020-11-14 创建