[R-sig-Geo] spplot with legend only, and legend inside the plotting area
Mauricio Zambrano-Bigiarini
mauricio.zambrano at jrc.ec.europa.eu
Thu Mar 8 17:59:49 CET 2012
On 07/03/12 17:38, Mauricio Zambrano-Bigiarini wrote:
>
> I'm almost there...
>
> The real problem appears when I try to produce several spplots within
> the same figure (each one of them for a different spatial area), with a
> unique legend for all the figures (that is the reason why I can not use
> 'legend' command).
>
> For example, while trying to produce 4 different plots within the same
> figure:
>
> library(sp)
> xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100))
> coordinates(xyz)=~x+y
>
> # only to get the default values from 'auto.key'
> dummy <- spplot(xyz, cex=10, alpha=0.7)
>
> # This has auto.key = "bottom" by default.
> cols = dummy$legend$bottom$args$key$points$col
> txt = dummy$legend$bottom$args$key$text[[1]]
>
> # number of elements in the legend
> n <- length(cols)
>
> p1 <- spplot(xyz, cex=10, alpha=0.7, auto.key=FALSE)
> p2 <- p1
> p3 <- p1
>
> # only the legend
> p4 <- spplot(xyz, cex=0, auto.key=list(x=.5, y=.5, corner = c(0.5, 0.5),
> title="my legend") )
>
> print(p1, split = c(1, 1, 2, 2), more = TRUE)
> print(p2, split = c(2, 1, 2, 2), more = TRUE)
> print(p3, split = c(1, 2, 2, 2), more = TRUE)
> print(p4, split = c(2, 2, 2, 2), more = FALSE)
>
> --------
> The previous solution has two problems (for me) that I couldn't solve:
> 1) the colors for the symbols are not correct
> 2) The symbols are plotted at the right side of the legend, while I
> would like to have them on the left side
I just solved the two previous problems, and I share the solution now
just in case it be useful for somebody else in the future:
library(sp)
mydata <- rnorm(100)
xyz = data.frame(expand.grid(x=1:10,y=1:10), mydata)
coordinates(xyz)=~x+y
# only to get the default values from 'auto.key'
dummy <- spplot(xyz, cex=10, alpha=0.7)
# This has auto.key = "bottom" by default.
cols = dummy$legend$bottom$args$key$points$col
txt = dummy$legend$bottom$args$key$text[[1]]
# number of elements in the legend
n <- length(cols)
p1 <- spplot(xyz, cex=10, alpha=0.7, auto.key=FALSE)
p2 <- p1
p3 <- p1
# legend levels
cuts <- unique( quantile( as.numeric(mydata), probs=c(0.25, 0.5, 0.75,
1), na.rm=TRUE) )
gof.levels <- cut(mydata, cuts)
# only the legend, in an empty plot
library(lattice)
p4 <- xyplot(1~1, type="n", xlab="", ylab="",
groups=gof.levels,
scales=list(draw=FALSE),
# automatic legend
key = list(x = .5, y = .5, corner = c(0.5, 0.5),
title="legend",
points = list(pch=16, col=cols, cex=1.5),
text = list(as.character(txt))
),
# removing outter box.
#From:
https://stat.ethz.ch/pipermail/r-help/2007-September/140098.html
par.settings = list(axis.line = list(col = "transparent")),
axis = function(side, ...) {
axis.default(side = side, ...)
},
)
# Creating the final figure
print(p1, split = c(1, 1, 2, 2), more = TRUE)
print(p2, split = c(2, 1, 2, 2), more = TRUE)
print(p3, split = c(1, 2, 2, 2), more = TRUE)
print(p4, split = c(2, 2, 2, 2), more = FALSE)
>
>
> I tried to solve the 2nd problem by using 'key' instead of 'auto.key':
>
> p4 <- spplot(xyz, cex=0,
> key = list(x = 0.5, y = 0.5, corner = c(0.5, 0.5),
> title="my legend", text = list(txt),
> points = list(pch = rep(16, length = n),
> col = cols,
> cex = rep(1.5, length = n)
> )
> )
> )
>
> print(p1, split = c(1, 1, 2, 2), more = TRUE)
> print(p2, split = c(2, 1, 2, 2), more = TRUE)
> print(p3, split = c(1, 2, 2, 2), more = TRUE)
> print(p4, split = c(2, 2, 2, 2), more = FALSE)
>
>
> and now the colors are correct, but the legend appears at the bottom of
> the plotting area, not in the centre as before.
>
>
> Does it mean that spplot ignores the x, y, and corner arguments in 'key' ?
>
>
> > sessionInfo() # part of it
> R version 2.14.1 (2011-12-22)
> Platform: x86_64-redhat-linux-gnu (64-bit)
> .
> .
> .
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] sp_0.9-95 lattice_0.20-0
>
> loaded via a namespace (and not attached):
> [1] cluster_1.14.2 grid_2.14.1 Hmisc_3.9-2
>
>
> Thanks in advance,
>
> Mauricio Zambrano-Bigiarini
>
--
=======================================================
FLOODS Action
Water Resources Unit (H01)
Institute for Environment and Sustainability (IES)
European Commission, Joint Research Centre (JRC)
TP 261, Via Enrico Fermi 2749, 21027 Ispra (VA), Italy
webinfo : http://floods.jrc.ec.europa.eu/
work-phone : (+39)-(0332)-789588
work-fax : (+39)-(0332)-786653
=======================================================
DISCLAIMER:\ "The views expressed are purely those of th...{{dropped:11}}
More information about the R-sig-Geo
mailing list