Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put Multiple Plots on a Single Page in R with spplot?

Tags:

plot

r

I know how to plot two plots when using the simple function plot:

 old.par <- par(mfrow=c(1, 2))
 plot(faithful, main="Faithful eruptions")
 plot(large.islands, main="Islands", ylab="Area")
 par(old.par)

this would return sth like:

enter image description here

I need to do the same for a fairly complex spplot function. What I'd like to have is a 3 x 3 square.

The function I want to plot 9 times is:

labelat = fivenum(gwr.res$SDF$Unempl)
labeltext = labelat 


spplot(gwr.res$SDF, "Unempl", cuts = 4, at = c(fivenum(gwr.res$SDF$Unempl)),     col.regions = Greens,
    colorkey=list(width=0.3,      
              space="right", 
              tick.number=5, 
              labels=list(  
                at=labelat, 
                labels=labeltext ))) 

Any idea on how to solve this?

Thanks,

like image 919
Blue Moon Avatar asked Dec 09 '22 01:12

Blue Moon


2 Answers

Use grid.arrange from package gridExtra. Doing grid.arrange(spplot(..),spplot(...),spplot(.....)) and so on will arrange them in a grid.

Here's an example using the meuse data set that generates 9 such plots and then uses do.call to save having to do grid.arrange(plots[[1], plots[[2]], and so on up to 9:

> require(gridExtra)
> plots = lapply(names(meuse)[1:9], function(.x) spplot(meuse,.x))
> do.call(grid.arrange,plots)

3x3 grid

like image 161
Spacedman Avatar answered Jan 11 '23 01:01

Spacedman


Or even simpler:

grid.arrange(spplot(df.voro2, "my.data", xlab = "x", ylab = "y", main = "my title") , 
             spplot(df.voro, "dummy", xlab = "x2", ylab = "y2", main = "my title2" ))

enter image description here

like image 37
maycca Avatar answered Jan 11 '23 00:01

maycca