0

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)

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

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?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83

3 Answers3

2

It looks like you are dealing with the base plot system in R, and assigning the plots to variables does not generate anything (e.g. plot1 is NULL). You can use layout to split the graphics area into subsections and then plot the individual plots into those:

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)
}

Created on 2021-01-21 by the reprex package (v0.3.0)

user12728748
  • 8,106
  • 2
  • 9
  • 14
  • Thank you for your reply! I posted a related question here:: https://stackoverflow.com/questions/65798482/labelling-points-on-a-plot-r-language can you please take a look at it? Thanks – stats_noob Jan 21 '21 at 19:40
2

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

par(mfrow=c(2,2))

before all four plots.

If you want everything on the same line add instead

par(mfrow=c(1,4))
Leonardo
  • 2,439
  • 33
  • 17
  • 31
  • I am accepting this answer because of its beautiful simplicity! Does the par(mfrow=c(2,2)) have to be entered prior to making the plots? – stats_noob Jan 21 '21 at 18:53
  • Yes, before generating the plots. If necessary, you can change the margins. If you need to generate a single plot later in the script, remember to add `par(mfrow = c(1,1))` to restore the default view! – Leonardo Jan 21 '21 at 18:56
  • I am working on a similar question over here: https://stackoverflow.com/questions/65798482/labelling-points-on-a-plot-r-language perhaps you could please take a look at it if you have time? Thank you – stats_noob Jan 21 '21 at 19:00
1

Maybe try the package ggpubr instead of grid.arrange. I assume you will need to save your plots with 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)
pascal
  • 1,036
  • 5
  • 15
  • Thank you for your reply! I posted a related question over here, could you please take a look at my question? https://stackoverflow.com/questions/65798482/labelling-points-on-a-plot-r-language thanks! – stats_noob Jan 21 '21 at 19:38