Hi all,

following my previous post on the subject which I paste here:

##################################
Hi all,
I am using spplot to plot two maps in the same device. In my
case, I have a map of a certain variable and the same map of the standard
deviation of the same variable. The range of the second is much smaller
than the range of the first so my wish is to plot both but to have them on
different scales for the color legend (hence, to have two separated
legends). Is it
possible?

I have worked out a way to do that, which passes through stretching the
second variable to the range of the first, and then work hardly on the "key"
list  from within the trellis object structure.
I was wondering if there is a more straightforward way.

######################################

Here is how I have worked around to get what I wanted. The code is pasted
below.

Still, if there is a more straightforward way to do that I would appreciate
comments.

Thanks

#####################################################################
#####################################################################

library(sp)
coords <- expand.grid(1:50, 1:50)

mean.df <- rnorm(length(coords[,1]), 100, 25) ## generate fake data
sd.df <- rnorm(length(coords[,1]), 10, 2) ## generate fake standard
deviations for such data, values are approx 1/10 of mean.df
original.data <- data.frame(mean=mean.df, sd=sd.df, x=coords[,1],
y=coords[,2]) # create a dataframe
normalized.data <- original.data ## copy original data
normalized.data[,1:2] <- scale(normalized.data[,1:2]) ## scale data
gridded(original.data) <- c('x', 'y') # make it gridded
gridded(normalized.data) <- c('x', 'y') # make it gridded
original.map <- spplot(original.data, col.regions=topo.colors,
layout=c(1,2)) ## spplot
original.map # nothing can be seen on sd panel

normalized.map <- spplot(normalized.data, col.regions=topo.colors,
layout=c(1,2)) ## spplot
normalized.map # now both variables are stretched into the same range, but
the legend needs to be fixed

tmp.leg <- normalized.map$legend$right ## save settings of the original
legend in a tmp object
normalized.map$legend$right <- NULL ## erase legend on the right
normalized.map$legend <- list(1,1) ## two elements for legend
names(normalized.map$legend) <- c('inside', 'inside') ## both named 'inside'
pos.legend1 <- tmp.leg$args$key$at ## store position of labels in original
legend
pretty.at.legend.sd <- pretty(sd.df) ## find pretty values for sd map ticks
real.at.legend.sd <- (pretty.at.legend.sd-mean(sd.df))/sd(sd.df) ##
normalize them as before
pretty.at.legend.mean <- pretty(mean.df) ## same for mean
real.at.legend.mean <- (pretty.at.legend.mean-mean(mean.df))/sd(mean.df)
normalized.map$legend[[1]] <- tmp.leg ## put legend object at its place
normalized.map$legend[[2]] <- tmp.leg ## put legend object at its place
normalized.map$legend[[1]]$args$key$labels$labels <- pretty.at.legend.sd ##
labels for sd legend
normalized.map$legend[[1]]$args$key$labels$at <- real.at.legend.sd     ##
correspondent 'at' values
normalized.map$legend[[2]]$args$key$labels$labels <- pretty.at.legend.mean
## same for mean
normalized.map$legend[[2]]$args$key$labels$at <- real.at.legend.mean ##
normalized.map$legend[[2]]$args$key$axis.line <-
trellis.par.get("axis.line") ## box and ticks for color legend
normalized.map$legend[[1]]$args$key$axis.line <-
trellis.par.get("axis.line") ## box and ticks for color legend
normalized.map$legend[[1]]$x <- -0.25 ## customized position fo the two
legends
normalized.map$legend[[1]]$y <- 0.75 ## customized position fo the two
legends
normalized.map$legend[[1]]$args$key$height <- 0.4 ## customized position fo
the two legends
normalized.map$legend[[2]]$x <- 1.02 ## customized position fo the two
legends
normalized.map$legend[[2]]$y <- 0.25 ## customized position fo the two
legends
normalized.map$legend[[1]]$args$key$height <- 0.4 ## customized position fo
the two legends
normalized.map$legend[[2]]$args$key$height <- 0.4 ## customized position fo
the two legends

normalized.map ## much better than first one

#####################################################################
#####################################################################

-- 
Gianluca Filippa
PhD
ARPA-VdA
Skype: gianluca.filippa

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to