1

I am checking my data for normality and I have created boxplots - boxplot(), histograms - hist(), and ggqqplots() (using the ggpubr package, which is aligned with ggplot). The data shows measurements of acoustic parameters within spectrograms using Raven Software.

For this project, I need to visually represent my results neatly and I want to use plot_grid() in the cowplot Package to align my plots in 9 columns and 3 rows. I have produced 9 boxplots, 9 histograms, and 9 ggqqplot() objects.

When I attempted to align my plots using the plot_grid() function, the qqggplot() printed but the hist() and boxplots() objects did not (see diagram 1). I learned that I need to convert my hist() and boxplot() objects into grob objects.

I found a website (below) that stated could use as.glob() or as.ggplot() to convert my diagrams. I used the information from this link below, although, unsuccessfully.

Conversion to grob objects

After I attempted to convert my hist() and boxplot() objects to grob object (so they will analogously be ggplot objects for the plot_grid() command) using the as.grob() function, I got error message 2 (see below).

R Code

#Install library packages
library("ggpubr")
library("grid")
library("ggplotify")
library(ggplot2)
library(cowplot)

#Box plots
Low_Freq_Box<-boxplot(main_path$Low.Freq..Hz.)
High_Freq_Box<-boxplot(main_path$High.Freq..Hz.)
Peak_Freq_Box<-boxplot(main_path$Peak.Freq..Hz.)
Delta_Freq_Box<-boxplot(main_path$Delta.Freq..Hz.)
Peak_Time_Box<-boxplot(main_path$ Peak.Time..s.)
Delta_Time_Box<-boxplot(main_path$Delta.Time..s.)
Center_Freq_Box<-boxplot(main_path$Center.Freq..Hz.)
Start_Freq_Box<-boxplot(main_path$Start.Freq..Hz.)
End_Freq_Box<-boxplot(main_path$End.Freq..Hz.)

#Histograms
Low_Freq_hist<-hist(main_path$Low.Freq..Hz.)
High_Freq_hist<-hist(main_path$High.Freq..Hz.)
Peak_Freq_hist<-hist(main_path$Peak.Freq..Hz.)
Delta_Freq_hist<-hist(main_path$Delta.Freq..Hz.)
Peak_Time_hist<-hist(main_path$Peak.Time..s.)
Delta_Time_hist<-hist(main_path$Delta.Time..s.)
Center_Freq_hist<-hist(main_path$Center.Freq..Hz.)
Start_Freq_hist<-hist(main_path$Start.Freq..Hz.)
End_Freq_hist<-hist(main_path$End.Freq..Hz.)

#QQ Plots
Low_Freq_qqplot<-ggqqplot(main_path$Low.Freq..Hz.)
High_Freq_qqplot<-ggqqplot(main_path$High.Freq..Hz.)
Peak_Freq_qqplot<-ggqqplot(main_path$Peak.Freq..Hz.)      
Delta_Freq_qqplot<-ggqqplot(main_path$Delta.Freq..Hz.)   
Delta_Time_qqplot<-ggqqplot(main_path$Delta.Time..s.)    
Peak_Time_qqplot<-ggqqplot(main_path$Peak.Time..s.)    
Center_Freq_qqplot<-ggqqplot(main_path$Center.Freq..Hz.)    
Start_Freq_qqplot<-ggqqplot(main_path$Start.Freq..Hz.)    
End_Freq_qqplot<-ggqqplot(main_path$End.Freq..Hz.)    

Error Message 1

#Plot the box plots, histograms, and qqplots on the same grid
plot_grid(Low_Freq_Box, High_Freq_Box, Peak_Freq_Box, Delta_Freq_Box, Peak_Time_Box, Delta_Time_Box, Center_Freq_Box, Start_Freq_Box, End_Freq_Box,
          Low_Freq_hist, High_Freq_hist, Peak_Freq_hist, Delta_Freq_hist, Peak_Time_hist, Delta_Time_hist, Center_Freq_hist, Start_Freq_hist, End_Freq_hist, 
          Low_Freq_qqplot, High_Freq_qqplot, Peak_Freq_qqplot, Delta_Freq_qqplot, Delta_Time_qqplot, Peak_Time_qqplot, Center_Freq_qqplot, Start_Freq_qqplot, End_Freq_qqplot,
          labels=c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14', 15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"),
          ncol=9, nrow=3)

    #Warning Messages
plot_grid(Low_Freq_Box, High_Freq_Box, Peak_Freq_Box, Delta_Freq_Box, Peak_Time_Box, Delta_Time_Box, Center_Freq_Box, Start_Freq_Box, End_Freq_Box,
+           Low_Freq_hist, High_Freq_hist, Peak_Freq_hist, Delta_Freq_hist, Peak_Time_hist, Delta_Time_hist, Center_Freq_hist, Start_Freq_hist, End_Freq_hist, 
+           Low_Freq_qqplot, High_Freq_qqplot, Peak_Freq_qqplot, Delta_Freq_qqplot, Delta_Time_qqplot, Peak_Time_qqplot, Center_Freq_qqplot, Start_Freq_qqplot, End_Freq_qqplot,
+           labels=c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"),
+           ncol=9, nrow=3,
+           align="hv",
+           label_size = 12)
There were 20 warnings (use warnings() to see them)

Diagram 1

enter image description here

Alice Hobbs
  • 1,021
  • 1
  • 15
  • 31
  • 1
    Please provide a `dput()` of your data for reproducibility – Benson_YoureFired May 18 '22 at 12:26
  • The code you provided has several typos. Error 1 is caused because you have a colon `:` right before `"8"` in the `labels=c(..)` section of the `plot_grid`. You also write over `End_Freq_Box` in the histogram section (the last one should be `End_Freq_hist`). – jpsmith May 18 '22 at 13:23
  • Thank you jpsmith, I changed the typos and I managed to print out the qqggplot() objects but the hist() and boxplot() objects did not print because they are not grobe objects to align with ggplot. Would you know how to change these objects into grobe objects? Many thanks if you can help – Alice Hobbs May 18 '22 at 20:02
  • Benson_YoureFired: I cannot share the data based on ownership but I will try and produce some dummy data and post it tomorrow. – Alice Hobbs May 18 '22 at 20:03

2 Answers2

1

Answer:

To convert base objects such as hist() and boxplot() into ggplot objects to arrange using the plot_grid() function, you can use the function as.ggplot().

#Box plots
Low_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Low.Freq))
High_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$High.Freq))
Peak_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Peak.Freq))
Delta_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Delta.Freq))
Peak_Time_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Peak.Time))
Delta_Time_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Delta.Time))
Center_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Center.Freq))
Start_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$Start.Freq))
End_Freq_Box<-as.ggplot(~boxplot(New_Acoustic_Parameters$End.Freq))

#Histograms
Low_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Low.Freq))
High_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$High.Freq))
Peak_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Peak.Freq))
Delta_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Delta.Freq))
Peak_Time_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Peak.Time))
Delta_Time_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Delta.Time))
Center_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Center.Freq))
Start_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$Start.Freq))
End_Freq_hist<-as.ggplot(~hist(New_Acoustic_Parameters$End.Freq))

#QQ Plots
Low_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$Low.Freq)
High_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$High.Freq)
Peak_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$Peak.Freq)      
Delta_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$Delta.Freq)   
Delta_Time_qqplot<-ggqqplot(New_Acoustic_Parameters$Peak.Time)    
Peak_Time_qqplot<-ggqqplot(New_Acoustic_Parameters$Delta.Time)    
Center_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$Center.Freq)    
Start_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$Start.Freq)    
End_Freq_qqplot<-ggqqplot(New_Acoustic_Parameters$End.Freq)    

#Open a new graphics window
dev.new()

#Plot the box plots, histograms, and qqplots on the same grid
plot_grid(Low_Freq_Box, High_Freq_Box, Peak_Freq_Box, Delta_Freq_Box, Peak_Time_Box, Delta_Time_Box, Center_Freq_Box, Start_Freq_Box, End_Freq_Box,
          Low_Freq_hist, High_Freq_hist, Peak_Freq_hist, Delta_Freq_hist, Peak_Time_hist, Delta_Time_hist, Center_Freq_hist, Start_Freq_hist, End_Freq_hist, 
          Low_Freq_qqplot, High_Freq_qqplot, Peak_Freq_qqplot, Delta_Freq_qqplot, Delta_Time_qqplot, Peak_Time_qqplot, Center_Freq_qqplot, Start_Freq_qqplot, End_Freq_qqplot,
          labels=c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"),
          ncol=9, nrow=3,
          align="hv",
          label_size = 9,
          axis = "tblr")

Diagram:

enter image description here

Alice Hobbs
  • 1,021
  • 1
  • 15
  • 31
0

Have you tried using the grid.arrange() function of the gridExtra package ?

There is an example on Cran here

For you it would look like this

library(gridExtra)
library(grid)
library(ggplot2)

grid.arrange(Low_Freq_Box, High_Freq_Box, Peak_Freq_Box, Delta_Freq_Box, Peak_Time_Box, Delta_Time_Box, Center_Freq_Box, Start_Freq_Box, End_Freq_Box,
          Low_Freq_hist, High_Freq_hist, Peak_Freq_hist, Delta_Freq_hist, Peak_Time_hist, Delta_Time_hist, Center_Freq_hist, Start_Freq_hist, End_Freq_hist, 
          Low_Freq_qqplot, High_Freq_qqplot, Peak_Freq_qqplot, Delta_Freq_qqplot, Delta_Time_qqplot, Peak_Time_qqplot, Center_Freq_qqplot, Start_Freq_qqplot, End_Freq_qqplot)

  • Hello Benon_YoureFired. I would like to thank you for you answer, it was deeply appeciated. I ran your code and I got this error message back. – Alice Hobbs May 18 '22 at 20:06
  • Error in gList(list(stats = c(3479.4, 7017.199, 8019.656, 9380.698, 12540.284 : only 'grobs' allowed in "gList" – Alice Hobbs May 18 '22 at 20:06