【问题标题】:R: arranging multiple plots together using gridExtraR:使用 gridExtra 将多个图排列在一起
【发布时间】:2021-04-26 04:33:26
【问题描述】:

我正在使用 R 编程语言。我正在尝试在同一页面上安排“plot1、plot2、plot3、plot4”:

library(kohonen) #fitting SOMs

library(ggplot2) #plots

library(GGally) #plots

library(RColorBrewer) #colors, using predefined palettes

 

iris_complete <-iris[complete.cases(iris),] #only complete cases... the iris dataset floats around in the sky with diamonds.

iris_unique <- unique(iris_complete) # Remove duplicates

 

#scale data

iris.sc = scale(iris_unique[, 1:4])

 

#build grid

iris.grid = somgrid(xdim = 10, ydim=10, topo="hexagonal", toroidal = TRUE)

 

# build model

set.seed(33) #for reproducability

iris.som <- som(iris.sc, grid=iris.grid, rlen=700, alpha=c(0.05,0.01), keep.data = TRUE)

 

 

###plots

 

var <- 1 #define the variable to plot

plot1 = plot(iris.som, type = "property", property = getCodes(iris.som)[,var], main=colnames(getCodes(iris.som))[var], palette.name=terrain.colors)

 

var <- 2 #define the variable to plot

plot2 = plot(iris.som, type = "property", property = getCodes(iris.som)[,var], main=colnames(getCodes(iris.som))[var], palette.name=terrain.colors)

 

var <- 3 #define the variable to plot

plot3 - plot(iris.som, type = "property", property = getCodes(iris.som)[,var], main=colnames(getCodes(iris.som))[var], palette.name=terrain.colors)

 

var <- 4 #define the variable to plot

plot4 = plot(iris.som, type = "property", property = getCodes(iris.som)[,var], main=colnames(getCodes(iris.som))[var], palette.name=terrain.colors)

 

 

 

 

g1 <- grid.arrange(plot1, nrow = 1)

g2 <- grid.arrange(plot2, nrow = 1)

g3 <- grid.arrange(plot3, nrow = 1)

g4 <- grid.arrange(plot4, nrow = 1)

 

grid.arrange(g1, g2, g3, g4, ncol = 1)

但是,当我使用“grid.arrange()”函数时,我得到一个错误:Error in gList - only 'grobs' allowed in gList"

现在,我正在考虑单独运行每个“绘图”语句,并使用 MS Paint 手动将它们全部连接起来。有没有更好的方法来做到这一点?

谢谢

【问题讨论】:

    标签: r ggplot2 data-visualization data-manipulation


    【解决方案1】:

    如果你想保留你正在使用的方法,只需添加

    par(mfrow=c(2,2))
    

    在所有四个地块之前。

    如果你想在同一行添加所有内容

    par(mfrow=c(1,4))
    

    【讨论】:

    • 我接受这个答案是因为它非常简单!是否必须在绘制图之前输入 par(mfrow=c(2,2))?
    • 是的,在生成图之前。如有必要,您可以更改边距。如果后面需要在脚本中生成单张图,记得加par(mfrow = c(1,1))恢复默认视图!
    • 我在这里处理一个类似的问题:stackoverflow.com/questions/65798482/… 如果你有时间,也许你可以看看它?谢谢
    【解决方案2】:

    看起来您正在处理 R 中的基本绘图系统,并且将绘图分配给变量不会生成任何内容(例如 plot1 是 NULL)。您可以使用layout 将图形区域拆分为多个子部分,然后将各个图绘制成这些子部分:

    library(kohonen)
    library(RColorBrewer)
    
    iris_unique <- unique(iris[complete.cases(iris),]) # Remove duplicates
    
    #scale data
    iris.sc <- scale(iris_unique[, 1:4])
    
    #build grid
    iris.grid <- somgrid(xdim = 10, ydim=10, topo="hexagonal", toroidal=TRUE)
    
    # build model
    set.seed(33)
    iris.som <- som(iris.sc, grid=iris.grid, rlen=700, alpha=c(0.05,0.01), keep.data=TRUE)
    
    ### plots
    # prepare plot layout
    graphics::layout(matrix(seq_len(4), 2, 2, byrow=TRUE), respect=FALSE)
    
    # generate plots
    for (var in seq_len(4)){
        plot(iris.som, type = "property", property = getCodes(iris.som)[,var], 
             main=colnames(getCodes(iris.som))[var], palette.name=terrain.colors)
    }
    

    reprex package (v0.3.0) 于 2021-01-21 创建

    【讨论】:

    【解决方案3】:

    也许尝试使用包ggpubr 而不是grid.arrange。 我假设您需要使用recordPlot() 保存您的绘图

    install.packages("ggpubr")
    install.packages("gridGraphics")
    library(ggpubr)
    library(gridGraphics)
    
    plot1 = recordPlot(iris.som, type = "property", property = getCodes(iris.som)[,var], main=colnames(getCodes(iris.som))[var], palette.name=terrain.colors)
    
    [...]
    
    ggarrange(plot1,plot2,plot3,plot4,ncol = 1)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多