[R-sig-Geo] fix color scale

Paul Hiemstra p.hiemstra at geo.uu.nl
Sun Oct 31 11:31:48 CET 2010


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:
> Hello!
>
> I have a problem.
>
> I am using IDW to interpolate a daily series of geospatial
> observations. Thus, I want to produce a large number of sequential
> maps.
>
> I want them to all represent the same color scale. Is there any way to
> fix the color scale so that it is the same for all the plots?
>
> Thanks,
>
> Pete
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>    


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 253 5773
http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770



More information about the R-sig-Geo mailing list