【问题标题】:how to change legend, country labels and size of my multiple area plots with ggplot2?如何使用 ggplot2 更改我的多个区域图的图例、国家标签和大小?
【发布时间】:2021-02-26 10:29:42
【问题描述】:

我试图更改我的图例和在其中一个区域图中多次出现的国家/地区的名称。

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

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

二。每个区域地块的大小不同

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

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

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 

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

【问题讨论】:

    标签: r ggplot2 size legend patchwork


    【解决方案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 package (v0.3.0) 于 2020 年 11 月 14 日创建

    【讨论】:

    • 谢谢Allan!有没有办法控制国家标签?
    • @GaB 是的。你想用它们做什么?
    • 如果您在实际帖子中看到,第二张图片在每组区域图的顶部都有大陆。我希望国家也一样。也是传奇,希望它在情节的顶部。四行。
    • @GaB 相当多的努力 - 但请参阅我的更新以获得完整的代表
    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2016-03-20
    • 2012-10-14
    • 2021-01-02
    相关资源
    最近更新 更多