Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add main title multiple plots ggarange

I have one data frame dt with data about dive behavior and at three different areas BA, MI, FA. I want to add main title to my multiple plots (ba, mi, fa) using ggarrange, but I didn't find something that worked. I want to add main title "Dive depths (m)" at top of my 3 plots

library(data.table)
library(ggplot2)

dt = data.table(area= c("BA", "FA", "MI"),
                dmean = c(30, 50, 200, 76, 467, 87, 98, 10, 240, 176, 89, 400, 340, 10, 40, 54, 89, 340, 205),
                sex = c("F", "M"))

ba<-ggplot(dt[dt$area=="BA",], mapping = aes(y = dmean, x = sex, color = sex, fill=sex))+
  geom_violin(alpha=.5,scale = "width",trim = FALSE, position=position_dodge(1))+
  ggtitle("Dive mean at BA and sex")+
  scale_y_log10(breaks = c(10, 30, 50, 100, 200, 300, 400, 500)) +   
  scale_fill_discrete(name="Social class",
                      labels=c("Female", "Male"))+
  xlab("Habitat")+
  ylab("Dive depth (m)")+
  theme_bw();ba

mi<-ggplot(dt[dt$area=="MI",], mapping = aes(y = dmean, x = sex, color = sex, fill=sex))+
  geom_violin(alpha=.5,scale = "width",trim = FALSE, position=position_dodge(1))+
  ggtitle("Dive mean at MI and sex")+
  scale_y_log10(breaks = c(10, 30, 50, 100, 200, 300, 400, 500)) +   
  scale_fill_discrete(name="Social class",
                      labels=c("Female", "Male"))+
  xlab("Habitat")+
  ylab("Dive depth (m)")+
  theme_bw();mi

fa<-ggplot(dt[dt$area=="FA",], mapping = aes(y = dmean, x = sex, color = sex, fill=sex))+
  geom_violin(alpha=.5,scale = "width",trim = FALSE, position=position_dodge(1))+
  ggtitle("Dive mean at FA and sex")+
  scale_y_log10(breaks = c(10, 30, 50, 100, 200, 300, 400, 500)) +   
  scale_fill_discrete(name="Social class",
                      labels=c("Female", "Male"))+
  xlab("Habitat")+
  ylab("Dive depth (m)")+
  theme_bw();fa

t<-ggarrange(ba, mi, fa, 
             ncol=3, nrow=1, common.legend = TRUE, legend="bottom");t

#I tried insert:
 ggtitle = "Dive depths (m)"
 top = "Dive depths (m)")
 top=textGrob("Dive depths (m)"

Someone know how to do?

like image 995
Érika Soares Coelho Avatar asked Jan 13 '21 01:01

Érika Soares Coelho


People also ask

How do I combine multiple Ggplots?

Combine the plots over multiple pagesThe function ggarrange() [ggpubr] provides a convenient solution to arrange multiple ggplots over multiple pages. After specifying the arguments nrow and ncol, ggarrange()` computes automatically the number of pages required to hold the list of the plots.

Does par () work with ggplot?

The base R functions such as par() and layout() will not work with ggplot2 because it uses a different graphics system and this system does not recognize base R functionality for plotting. However, there are multiple ways you can combine plots from ggplot2 . One way is using the cowplot package.

How do you increase space between plots in Ggarrange?

Currently, the only way to change the space between multiple plot is ggarrange() is to change the margins of each plot (#58). An option to change the spacing in the ggarrange() itself would improve the user experience.


1 Answers

You can use annotate_figure :

library(ggpubr)

plot<- ggarrange(ba,mi,fa, ncol=3, nrow=1, common.legend = TRUE,legend="bottom")

annotate_figure(plot, top = text_grob("Dive depths (m)", 
               color = "red", face = "bold", size = 14))

enter image description here

like image 142
Ronak Shah Avatar answered Sep 28 '22 16:09

Ronak Shah