[R-sig-Geo] spplot : varying dots for multiple parameters

Roger Bivand Roger.Bivand at nhh.no
Mon Dec 14 18:18:39 CET 2009

On Mon, 14 Dec 2009, Servet Ahmet Cizmeli wrote:

> Dear Roger, Edzer,
> Thanks a lot for your kindly help. I just switched to R from Matlab
> after 12 years of Matlab experience. It is actually an agonizing
> experience as R is indeed very different than Matlab. There are so many
> different ways to perform one given task that I am completely at loss
> right now. But I am not giving up ;o)

Dear Servet,

Good, I expect that you will find some more similarities in time. Note 
that many more R functions return structured objects (usually lists with a 
class attribute). Looking at how lm() works:


will tell you a lot about lattice graphics, often a formula argument, a 
data argument, and maybe a grouping argument. If you have a chance to 
visit the R graphics gallery:


or, even better, get hold of Paul Murrell's R Graphics book and/or 
Deepayan Sarkar's Lattice graphics book (see the books links on the R web 
page), you may find it easier to see what corresponds to what. Very often 
the defaults (like empty space) are there for a reason - recall that the 
very similar Trellis graphics were written by Bill Cleveland at Bell Labs 
with John Chambers, the author of the S language and an important core 
contributor to R.

> I just run the command install.packages("sp") which reinstalled the
> latest version : sp_0.9-51.tar.gz. The Warning messages described in my
> first message are still there, though. The output of sessionInfo() is
> given at the end of the message for your convenience.
> Thanks a lot Roger for your code. It indeed improves my graphs a lot.
> I have some other questions if you don't mind :
> Question 1 : Wouldn't it help if the bubble function supported the
> convenient rm.na option?

Perhaps, but this might lead to unwanted subsetting too, so it isn't 

> Question 2 : Each bubble() plot is widely spaced one from the other.
> There is a lot of waste of space. How can I adjust the margins so that
> the maps are tightly placed just like in lattice? The bHEX object
> returned by bubble() is of class "lattice" and I don't seem to find any
> information explaining how to perform such a fine-tuning. I would
> appreciate any guidance that would point me to the place where all this
> is explained. I don't presently seem to look at the right place.
> There is also the 3rd plot box which is of different size than the rest in
> Roger's code...
> How to make all the boxes aligned and of exactly the same size?

That is the subsetting problem. You overcome it by setting xlim and ylim 
arguments. The white space comes from dividing the default device up into 
four rectangles, the relative shapes of which most likely do not match the 
shapes of the data bounding boxes. Try changing the shape of the device 
(width and height arguments) if you need more control. Note that 
analytical and presentation graphics should be treated differently, as you 
can "eyeball" many displays without needing to polish to your presentation 
standards. If need be, that can be done afterwards.

> On Mon, 2009-12-14 at 08:02 +0100, Edzer Pebesma wrote:
>> I don't think spplot allows this; you'd have to use lattice' function
>> xyplot, to which spplot (for points) is a rather thin wrapper.
> Yes, I would like to learn better "lattice" tools as they seem to be very
> powerful.
> That is why I was experimenting with spplot :
>> xyplot(Insitu_sp[!idx,])
> Error in UseMethod("xyplot") :
> no applicable method for 'xyplot' applied to an object of class
> "SpatialPointsDataFrame"

See args(xyplot) - from the help page, you see that x is most often a 
formula, say y ~ x.

> Question 3 :
> How to tell xyplot to create maps of georeferenced "sp" objects? There is
> no mention of
> this in the help page of xyplot?

No, because it is a general method. Use spplot().

> I would like to plot a legend by using the following code :

Until you have control of the graphic itself, do not worry about 
decorations, they are not important.

Hope this helps,


> dataset = c("SPM","CHL_A")
> for (AA in 1:length(dataset)) {
> idx = as.logical(is.na(Insitu[dataset[AA]]))
> tmp <- bubble(Insitu_sp[!idx,], dataset[AA], xlim=xlim, ylim=ylim,
> sp.layout = my.layout, key.space=list(x=0.1,y=.95,corner=c(0,1)))
> plot(tmp, split=c(1,AA,1,2), more=TRUE)
> }
> but no legend is drawn and the following warning is being displayed twice :
> Warning messages:
> 1: In names(legend)[1] <- space :
> number of items to replace is not a multiple of replacement length
> Question 4 :
> How can I place a legend at a custom place?
> best
> Servet
> Output of sessionInfo()
> ************************************************************************
> R version 2.10.0 (2009-10-26)
> i486-pc-linux-gnu
> locale:
> [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
> [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
> [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
> [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
> [9] LC_ADDRESS=C               LC_TELEPHONE=C
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> other attached packages:
> [1] rgdal_0.6-23 sp_0.9-51
> loaded via a namespace (and not attached):
> [1] grid_2.10.0     lattice_0.17-26 tools_2.10.0
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no

More information about the R-sig-Geo mailing list