Adding main title using ggarrange ()

Hi I am trying to add a main title to a group of plots I made using the ggarrange function.
Each plot has a title but I need a main title for both figures. Something like "Plot 1: Distribution by gender" over figure 1 and 2.

Rplot

Here is my code.

library(tidyverse)
library(ggpubr)

my_data_2018 <- tibble(Var = c(900, 1500, 350, 1200, 750, 100,125,250,300),
                      Gender = c("W", "W", "W", "M", "M", "W", "W", "M", "W"),
                      my_weights_2018 = c(2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

my_data_2019 <- tibble(Var = c(32, 21, 21, 900, 1500, 350, 1200, 750, 100,125,250,300),
                       Gender = c("W", "W", "W","W", "W", "W", "M", "M", "W", "W", "M", "W"),
                       my_weights_2019 = c(2.2, 3.1, 8.2, 2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

my_data_2018  %>%
    ggplot(aes(Var, Gender, weight = my_weights_2018, fill = Gender))+
  geom_violin(color = "black", scale = "count")+
  ggtitle("Figure 1")+ 
  theme(legend.position = "none")-> plot_1


my_data_2019  %>%
  ggplot(aes(Var, Gender, weight = my_weights_2019, fill = Gender))+
  geom_violin(color = "black", scale = "count")+
  ggtitle("Figure 2")+ 
  theme(legend.position = "none")-> plot_2

ggarrange(plot_1, plot_2)

something like this


text <- "example title"

# Create a text grob
tgrob <- text_grob(text,size = 20)
# Draw the text
plot_0 <- as_ggplot(tgrob) + theme(plot.margin = margin(0,3,0,0, "cm"))

ggarrange(plot_0,NULL,plot_1, plot_2,
          ncol = 2,nrow = 2,heights = c(1,5))

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.