Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用ggplot2更改我的多区域图的图例、国家标签和大小?_R_Ggplot2_Size_Legend_Patchwork - Fatal编程技术网

R 如何使用ggplot2更改我的多区域图的图例、国家标签和大小?

R 如何使用ggplot2更改我的多区域图的图例、国家标签和大小?,r,ggplot2,size,legend,patchwork,R,Ggplot2,Size,Legend,Patchwork,我一直在试图改变我的传说和在其中一个地区多次出现的国家的名字 一,。如您所见,图例出现了好几次。我想让这个传说成为最后一张照片中的地方。对于这两种疾病:症状和共病: 如果这可以出现在整个情节之上,表现为:症状:胸痛、寒战等,以及共病:哮喘、1型糖尿病等 二,。每个区域图的大小不同 它们的大小应该相等 iii.还有国家标签。很好,它们只出现一次。希望它们看起来像最后一张图片: A-印度, B-巴基斯坦, C-英国,位于每套区域地块上方的左侧 下面是一个我希望我的情节看起来像的例子 以下是代码和伪数

我一直在试图改变我的传说和在其中一个地区多次出现的国家的名字

一,。如您所见,图例出现了好几次。我想让这个传说成为最后一张照片中的地方。对于这两种疾病:症状和共病:

  • 如果这可以出现在整个情节之上,表现为:症状:胸痛、寒战等,以及共病:哮喘、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 
    

    sympt\u count\u plot您可以使用
    cowplot::get\u legend
    获取图例,然后按照自己的喜好进行排列。下面是一个完整的reprex:

    #加载包和数据

    库(ggplot2)
    图书馆(拼凑)
    
    谢谢你,艾伦!有办法控制国家标签吗?@GaB有。你想用它们做什么?如果你在实际的帖子中看到,第二张图片在每套面积图的顶部都有大陆。我希望国家也一样。还有图例,希望它位于绘图顶部。共四行。@GaB相当费劲-但完整的reprex请参见我的更新
    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)
    
    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)
    
    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)
    
    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))