I am trying to overlay two shapefiles on a raster grid. The raster is a digital elevation model. The first shapefile is a polygon, which shows the outline of a country, and the second shapefile is a polyline, which represents rivers. I am currently using levelplot to plot the raster and spplot to overlay the polyline and polygon. The polyline is coloured according to the attribute table using the STATUS column - this plots "perennial" status as blue and "ephemeral" status as orange. My code is shown below. This produces a plot with a colorkey for the raster, but I can't work out how to add a key for the polyline. Apologies if this is very obvious but I can't seem to find an answer on any existing threads.
Any help would be much appreciated.
The code is as follows:
# Read rasterdata_location_DEM1 <- "E:/AGA_ProcessDatasets/DEM_Data/gt30e020n40.tif"iDEM <- raster(data_location_DEM1)
# Read shapefilesioutlineCountry <- readOGR(dsn = "E:/AGA_ProcessDatasets", layer = "UNAfricanCountries_Final_Buffer100m")proj4string(ioutlineCountry) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
irivers <- readOGR(dsn = "E:/AGA_ProcessDatasets/Hydrology_Data", layer = "Rivers_Africa_37333_ForPlotting_Split")proj4string(irivers) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
# define colours for riversrivercolours <- c("orange","blue")
# define colours for DEMcols <- gray.colors(128,start=1,end=0)
# define extents for plottingplotmin <- cellStats(iDEM,stat='min')plotmax <- cellStats(iDEM,stat='max')breaks <- (plotmax - plotmin)/128legendbreaks <- round((plotmax - plotmin)/5,0)scale <- seq(plotmin, plotmax, by=breaks)xmin <- iDEM at extent@xminxmax <- iDEM at extent@xmaxymin <- iDEM at extent@yminymax <- iDEM at extent@ymax
# define output png filefilenamePre = paste(iCountryName,"_Hydrology2.png",sep="")png(filename="Hydrology.png",width=1000,height=486,units="px")
# plot dataa<-levelplot(iDEM,margin=FALSE,xlab="",ylab="",at=scale,col.regions=cols, colorkey=list(space="right",width=0.75,height=0.75)) + spplot(irivers,zcol="STATUS",col.regions=rivercolours) + spplot(ioutlineCountry,zcol="NAME_0",fill="transparent",col="black") # add title to legend a$legend$right <- list(fun = mergedTrellisLegendGrob(a$legend$right,list(fun = textGrob,args = list("maSL",rot = 0)),vertical = FALSE))
# printprint(a,split=c(1,1,2,1),newpage=FALSE)
dev.off()
Multiple legends with levelplot and spplot
2 messages · kirsty fulton, Oscar Perpiñan
3 days later
Hello,
You could try to define the legend for the Spatial* object inside the
levelplot call using the `key` argument. For example:
library(sp)
library(raster)
library(rasterVis)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
data(meuse)
coordinates(meuse) = c("x", "y")
meuse$class <- cut(meuse$zinc, 3)
ptsCols <- c('black', 'red', 'blue')
lvp <- levelplot(r, margin = FALSE,
## Define the legend for the spplot graphic
key = list(space = 'top', text = list(levels(meuse$class)),
points = list(pch = 21, fill = ptsCols)),
## Legend for the Raster object
colorkey=list(space="right",width=0.75,height=0.75))
spp <- spplot(meuse["class"], col.regions = ptsCols)
lvp + spp
Best,
Oscar.
Oscar Perpi??n Lamigueiro Dpto. Ingenier?a El?ctrica (ETSIDI-UPM) Grupo de Sistemas Fotovoltaicos (IES-UPM) URL: http://oscarperpinan.github.io