Skip to content
Prev 9782 / 29559 Next

fix color scale

Hi Peter,

When creating such a large amount of illustrations with the same 
colorscale, I automatically think of lattice graphics. Under the hood 
spplot also uses lattice graphics. Take a look at the levelplot() 
function from lattice which produces the grid plots for spplot (if I'm 
correct). Alternatively, I've been using ggplot now for quite a while to 
make plots of a lot of grids. A small example says more than a thousand 
words:

library(ggplot2)
library(sp)

data(meuse.grid)
summary(meuse.grid)

# Note that I do not transform meuse.grid to SpatialPixelsDataFrame
# Let's make a simple grid plot
dum = meuse.grid[c("x","y","dist")]
ggplot(aes(x = x, y = y, fill = dist), data = dum) + geom_tile()

# Let's make a few more attributes to the grid
# could be measurements on other dates for example
new_atts = do.call("cbind", lapply(1:100, function(num) dum$dist + 
runif(dum$dist)))
summary(new_atts)
dum = data.frame(cbind(dum, new_atts))

# Important step now is to
# restructure the data
dum_ggplot = melt(dum, id.vars = c("x","y"))

# Now make a plot using dum_ggplot
# of 'x' and 'y' using value as a 'fill'
# with a plot per 'variable', can take a minute to plot
ggplot(aes(x = x, y = y, fill = value), data = dum_ggplot) + geom_tile() 
+ facet_wrap(~variable) +
       scale_x_continuous('', labels = NA, breaks = NA) +
       scale_y_continuous('', labels = NA, breaks = NA) + 
opts(aspect.ratio = 1)
# These last two lines get rid of the labels on the axes and set aspect 
ratio to 1

Now you have a plot with 101 maps with the same colorscale, with ggplot 
doing all the hard work. It takes some time to get the hang of ggplot, 
but I think it is worth the investment, also for spatial plots.

cheers and hope this helps,
Paul
On 10/28/2010 09:12 PM, Peter Larson wrote: