R:使用gridExtra将多个图布置在一起 [英] R: arranging multiple plots together using gridExtra

查看:64
本文介绍了R:使用gridExtra将多个图布置在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我正在尝试安排"plot1,plot2,plot3,plot4"到在同一页面上:

I am using the R programming language. I am trying to arrange "plot1, plot2, plot3, plot4" on the same page:

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()"时,函数,我得到一个错误: gList中的错误-gList中只允许'grobs'"

However, when I use the "grid.arrange()" function, I get an error : Error in gList - only 'grobs' allowed in gList"

现在,我正在考虑运行每个情节"语句,然后使用MS Paint手动将它们全部加入.有更好的方法吗?

Right now, I am considering running each of the "plot" statements individually, and manually joining them all using MS Paint. Is there a better way to do this?

谢谢

推荐答案

如果要保持使用的方法,只需添加

If you want to keep the approach you are using just add

par(mfrow=c(2,2))

在所有四个情节之前.

如果您希望所有内容都在同一行上,请添加

If you want everything on the same line add instead

par(mfrow=c(1,4))

这篇关于R:使用gridExtra将多个图布置在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆