Multiple spplots in a page and with a single index

443 views Asked by At

I have a polygon shape file named CROPS.shp and the following key -

1 - Rice
2 - Wheat 
3 - Corn 
4 - Millets 

I read the shape file using the rgdal package into a variable named my_crops. Now my_crops@data has six different fields namely Y1, Y2, Y3, Y4, Y5 and Y6. Each of these fields has values from 1 to 4.

So for example, the first few rows of crops@data would be like

Y1   Y2   Y3   Y4   Y5    Y6 
1    2    4    1    1    2
3    4    1    1    1    2
4    2    2    2    1    3

What do I want to do?

I want to have a page with 6 plots (2 rows, 3 columns), where the first plot would be based on the field Y1, and the last plot would be based on the field Y6. The colour of the plots (polygon shapes) should be based on the values 1 to 4, in each of the shapefile plots.

The plots should be accompanied by a single legend at the bottom of the page.

1

There are 1 answers

0
Edzer Pebesma On

Do you mean something like this?

library(maptools)
nc.sids <- readShapePoly(system.file("etc/shapes/sids.shp", package="spdep")[1],
  ID="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

set.seed(1)
nc.sids$a = factor(sample(letters[1:4], 100, replace=T))
nc.sids$b = factor(sample(letters[1:4], 100, replace=T))
nc.sids$c = factor(sample(letters[1:4], 100, replace=T))
nc.sids$d = factor(sample(letters[1:4], 100, replace=T))
nc.sids$e = factor(sample(letters[1:4], 100, replace=T))
nc.sids$f = factor(sample(letters[1:4], 100, replace=T))
spplot(nc.sids[c("a", "b", "c", "d", "e", "f")], 
    colorkey = list(space = "bottom"), layout = c(3, 2),
    as.table = TRUE)

enter image description here