Eliminate titles from plots in order to have one main title

55 Views Asked by At

im trying to use ggarrange from ggpubr to have multiple, plots in one general plot. I have my plots saved, and each one has a title, i want to eliminate those titles from each plot when the multiple plot comes, here are my plots:

dic_plot_int <- ordenes_dic %>%
  filter(corteesp == 1) %>%
  group_by(espesor) %>%
  summarise(MTS.4 = sum(MTS.4)) %>% 
  ggplot(aes(espesor,MTS.4, fill= espesor))+     
  geom_col(width = .5)+ geom_bar(stat= "identity", colour ="black")+
  geom_text(aes(label = MTS.4), vjust = -0.2) +
  labs(title = "Indicador de Productividad de órdenes Integrales", subtitle = "Fenabel Aldama Diciembre 2023", x= "Espesor del Cristal en milímetros", y= "Metros Cuadrados", fill="Espesor")+
  theme(plot.title = element_text(hjust=0.5), plot.subtitle = element_text(hjust = 0.5))+
  scale_fill_manual(values = c("#21BCDF","#74EA2C","#EA441B","#D051F5"),  labels =c("6mm","8mm","9.5mm", "12.7mm"))+
  theme(panel.background = element_rect(fill = '#F7D2AF', color = 'purple'))+
  coord_cartesian(ylim = c(0,400))

jan_plot_int <- ordenes_ene %>%
  filter(corteesp == 1) %>%
  group_by(espesor) %>%
  summarise(MTS.4 = sum(MTS.4)) %>% 
  ggplot(aes(espesor,MTS.4, fill= espesor))+     
  geom_col(width = .5)+ geom_bar(stat= "identity", colour ="black")+
  geom_text(aes(label = MTS.4), vjust = -0.2) +
  labs(title = "Indicador de Productividad de órdenes Integrales", subtitle = "Fenabel Aldama Enero 2024", x= "Espesor del Cristal en milímetros", y= "Metros Cuadrados", fill="Espesor")+
  theme(plot.title = element_text(hjust=0.5), plot.subtitle = element_text(hjust = 0.5))+
  scale_fill_manual(values = c("#21BCDF","#EA441B","#D051F5"),  labels =c("6mm","9.5mm", "12.7mm"))+
  theme(panel.background = element_rect(fill = '#F7D2AF', color = 'purple'))+
  coord_cartesian(ylim = c(0,400))

I dont want to eliminate from each plot the title beacuse there are some parts on my markdown where I show each plot separately, but when i want to show both plots i want to eliminate the tiles and only have one main title.

I tried using this:

plot <- ggarrange(dic_plot_int,jan_plot_int,ncol = 2,nrow = 1,common.legend = TRUE, legend = "right")

annotate_figure(plot, top = text_grob("Indicador de Productividad órdenes Integrales", face = "bold", size = 14))

But it looks really squeeze with the titles from each plot, is there any way to eliminate the titles from those plots only for the multiple plot? or somehow have something like the common.legend thing but for the title? enter image description here

1

There are 1 best solutions below

0
stefan On

One option would be to put your plots in a list, then use e.g. lapply to remove the titles before passing the list of plots to ggarrange via the plotlist= argument.

Using a minimal reproducible example based on mtcars let's first reproduce your issue:

library(ggplot2)
library(ggpubr)
library(patchwork)

p1 <- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
  geom_point() +
  labs(title = "Scatter Plot")

p2 <- ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
  geom_bar() +
  labs(title = "Bar Chart")

plot <- list(p1, p2) |>
  ggarrange(plotlist = _)

annotate_figure(plot, top = text_grob("Indicador de Productividad órdenes Integrales", face = "bold", size = 14))

Now, using lapply and labs() we can remove the titles before passing the list of plots to ggarrange:

plot <- list(p1, p2) |>
  lapply(\(x) x + labs(title = NULL)) |>
  ggarrange(plotlist = _)

annotate_figure(plot, top = text_grob("Indicador de Productividad órdenes Integrales", face = "bold", size = 14))

Or as already suggested by @r2evans in his comment using patchwork you could do:

p1 + p2 &
  plot_annotation(
    title = "Indicador de Productividad órdenes Integrales",
    theme = theme_grey() + theme(plot.title = element_text(face = "bold", size = 14, hjust = .5))
  ) &
  labs(title = NULL)