I'm wondering how I can add a customized color palette that I used to display a sp.points layer in a call to levelplot (lattice plotting) in the rasterVis package in R.

I used a function that Dave X created here in his answer to a question on how to map numeric values to colors. Now, I am using it in side-by-side maps with different raster backgrounds, as the color scale that a points layer is mapped to. I want this custom color ramp to appear in a legend but I don't need it to show up twice - it's the same on both panels of the plot.

A quick summary of the maps I'm making: A two-paneled plot of

  1. elevation, and
  2. aspect (calculated from the elevation layer),

for a district in rural Tanzania. Plotted over these rasters on each panel is a polygons layer denoting the district's adm boundaries, and a points layer representing grain mills in the district that I visited on a prior field trip. The color represents the mean Aflatoxin level observed in samples of milled maize taken from each mill. The code for one of the panels of the plot (to give you an idea) is below:

(See Dave X's post linked above for the code for map2color() )

    AF <- MillGeo@data$AF_mean
    AF_colors <- colorRampPalette(c("light yellow", "dark red"))(length(AF))
    pal <- map2color(AF, AF_colors)
    p1 <- levelplot(elev_raster, margin=FALSE, 
            at=seq(600,2200, by=50), col.regions=terrain.colors(160), 
            xlab=NULL, ylab=NULL) + layer(sp.polygons(Kongwa)) +     
            sp.points(MillGeo, pch=20, cex=1.5, col=pal)

Imagine that there are two such plots next to one another and they both use the same color palette for the points representing Aflatoxin levels. I want to customize a legend that I can represent the scale according to the values of Aflatoxin used to create it, ideally just showing up once below the two panels. Is there a way to do this?

Right now, my code for showing the plots is:

    grid.arrange(p1,p2, ncol=2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.