简体   繁体   English

将 hist() 和 boxplot() object 转换为 ggplot() object 以使用 R 中的 Cowplot Package 中的 plot_grid() 对齐我的绘图

[英]Converting a hist() and boxplot() object into a ggplot() object to align my plots using plot_grid() in the Cowplot Package in R

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 ).我正在检查我的数据是否正常,我创建了箱形图 - boxplot() 、直方图 - hist()ggqqplots() (使用ggpubr package,它与ggplot对齐)。 The data shows measurements of acoustic parameters within spectrograms using Raven Software.数据显示使用 Raven 软件测量的声谱图中的声学参数。

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.对于这个项目,我需要在视觉上整齐地表示我的结果,我想在cowplot Package 中使用plot_grid()将我的图对齐为 9 列和 3 行。 I have produced 9 boxplots, 9 histograms, and 9 ggqqplot() objects.我制作了 9 个箱线图、9 个直方图和 9 个ggqqplot()对象。

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) .当我尝试使用plot_grid() function 对齐我的图时,打印了 qqggplot() 但 hist() 和 boxplots() 对象没有(见图 1) I learned that I need to convert my hist() and boxplot() objects into grob objects.我了解到我需要将我的hist()boxplot()对象转换为 grob 对象。

I found a website (below) that stated could use as.glob() or as.ggplot() to convert my diagrams.我发现一个网站(如下)声明可以使用as.glob()as.ggplot()来转换我的图表。 I used the information from this link below, although, unsuccessfully.我使用了下面这个链接中的信息,但没有成功。

Conversion to grob objects转换为 grob 对象

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).在我尝试使用as.grob() function 将hist()和 boxplot boxplot()对象转换为grob object (因此它们将类似于plot_grid()命令的 ggplot 对象)后,我收到错误消息 2(见下文) .

R Code R 代码

#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错误信息 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图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().要使用 plot_grid() function 将 hist() 和 boxplot() 等基础对象转换为 ggplot 对象进行排列,可以使用 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:图表:

在此处输入图像描述

Have you tried using the grid.arrange() function of the gridExtra package?您是否尝试过使用 gridExtra package 的grid.arrange() gridExtra

There is an example on Cran here这里有一个关于 Cran的例子

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cowplot程序包:在R中使用plot_grid()将许多图安排成一个图后,如何垂直向下调整图例 - Cowplot Package: How to align legends vertically downwards after arranging many plots into one plot using plot_grid() in R 如何使用`cowplot` package中的`plot_grid`对齐包含在最后一列图中的图例? - How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package? ggplot:使用RColorBrewer软件包填充Boxplots +使用R Cowxlot软件包中的plot_grid()绘制Boxplots - ggplot: Fill Boxplots Using the Package RColorBrewer + Plot Boxplots Using plot_grid() in the Cowplot Package in R 使用 coord_equal() 时使用 cowplot::plot_grid() 垂直对齐不同高度的图 - Vertically align of plots of different heights using cowplot::plot_grid() when using coord_equal() 如何使用 plot_grid() 将两个图组合在一起在“cowplot”中有一个单一的网格背景? 在 R 语言 - How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language 我在 R 和 ggplot2 中使用 cowplot 的 plot_grids 缺少网格中的四个图之一? - My plot_grids using cowplot in R with ggplot2 are missing one of the four plots in the grid? 使用 cowplot 的 plot_grid 放置图例 - Placement of legend using plot_grid of cowplot 在R的网格中将图与Cowplot对齐 - Align plots with Cowplot in a grid in R cowplot plot_grid自动缩小图的大小 - cowplot plot_grid scales down size of plots automatically 使用 cowplot::plot_grid 组装图时出错 - Error when assembling plots with cowplot::plot_grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM