0
votes

I have trying to change my legends and the names of the countries that appear multiple times in one of the areas plots. enter image description here

i. As you can see the legend appears several times. I want this legend to be place like in the last picture. for both: symptoms and comorbidities:

  1. if this can appear above the whole plot as: Symptoms: chest pains, chills, etc. and Comorbidities: asthma, diabetes type one, etc.

ii. the sizes of each area plots are different

  1. They should be equal in size.

iii. Also the country labels. Nicely, these are appearing once. Would want them to appear like the last picture:

A - India, B - Pakistan, C - United Kingdom and positioned on the left hand side above each set of area plots.

Here is an example of the way I want my plot to look likeenter image description here

Here is the code and the fake datasets:

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 

Is there a way I can get them the way I want. Help is very much appreciated.

1

1 Answers

1
votes

You can harvest the legends using cowplot::get_legend then arrange them however you like. Here's a full reprex:

# Load packages and data

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"))

# plot 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)

# plot 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)

# plot 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)

# plot 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)

# Stitch plots together

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))

enter image description here

Created on 2020-11-14 by the reprex package (v0.3.0)