[R-sig-Geo] spplot with legend only, and legend inside the plotting area

Mauricio Zambrano-Bigiarini mauricio.zambrano at jrc.ec.europa.eu
Wed Mar 7 17:38:58 CET 2012


On 07/03/12 13:11, Oscar Perpiñán Lamigueiro wrote:
> Mauricio Zambrano-Bigiarini<mauricio.zambrano at jrc.ec.europa.eu>  writes:
>
>> I need to produce two different spplot figures of spatial points, and
>> I would like to ask your help for the creation of the second one:
>>
>> 1) the first figure has to show the values but without any legend:
>>
>> library(sp)
>> xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100))
>> coordinates(xyz)=~x+y
>> spplot(xyz, cex=10, alpha=0.7, auto.key=FALSE)
>>
>> (I don't have any problem with this)
>>
>>
>> 2) The second figure has to show ONLY the legend for the values of the
>> previous figure (without plotting the points), inside the plotting
>> area.
>>
>> I tried with:
>>
>> spplot(xyz, cex=0, alpha=0.7, auto.key=TRUE, key.space="center")
>
> Hello,
>
> Try this:
>
> spplot(xyz, auto.key=list(x=.5, y=.5), cex=0)
>
> Change the values of x,y for your convenience. You can also find useful
> "corner" instead of x,y. For more information read the "auto.key", "key" and
> "legend" parts of the help page of xyplot.
>
> Cheers,
>
> Oscar.

Thank you very much you both Oscar and Jon for your help.

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 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)
webinfo    : http://floods.jrc.ec.europa.eu/
=======================================================
DISCLAIMER:
"The views expressed are purely those of the writer
and may not in any circumstances be regarded as stating
an official position of the European Commission."
=======================================================
Linux user #454569 -- Ubuntu user #17469
=======================================================
"If Columbus had turned back, no one would have blamed him.
Of course, no one would have remembered him either."
(Source Unknown)



More information about the R-sig-Geo mailing list