0

我试图更改我的传说和在其中一个区域图中多次出现的国家/地区的名称。在此处输入图像描述

一世。如您所见,图例出现了好几次。我希望这个传说出现在最后一张照片中。对于两者: 症状和合并症:

  1. 如果这可以在整个图上方显示为:症状:胸痛、发冷等和合并症:哮喘、1 型糖尿病等。

ii. 每个区域地块的大小不同

  1. 它们的大小应该相等。

iii. 还有国家标签。很好,这些只出现过一次。希望它们看起来像最后一张图片:

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 

有没有办法我可以按照我想要的方式得到它们。非常感谢您的帮助。

4

1 回答 1

1

cowplot::get_legend您可以使用然后按您喜欢的方式排列它们来收获传说。这是一个完整的代表:

# 加载包和数据

library(ggplot2)
library(patchwork)

git <- "https://github.com/gabrielburcea/stackoverflow_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 包(v0.3.0)于 2020 年 11 月 14 日创建

于 2020-11-14T19:14:17.550 回答