[R-sig-Geo] unable to customize a plot produced from a shapefile

Roger Bivand Roger.Bivand at nhh.no
Mon Feb 13 12:36:43 CET 2012


On Mon, 13 Feb 2012, Manuel Schneider wrote:

> Try
> plot(1, t="n", xlim=c(bbox(testS)[1,1], bbox(testS)[1,2]), ylim=c(30,50))
> plot(testS, add=T)

The issues here are quite complex. Firstly, you should have added a 
coordinate reference system to the object (no direct impact on ylim, but a 
contributing factor). Methods for plotting take an asp= argument for 
aspect; in sp methods, the asp= is set to 1 for non-geographic 
coordinates, and "stretched" for geographic coordinates depending on the 
distance from the equator, see ?mapasp.

If the plot window is set with xlim or ylim, they will not respond 
directly, but will create a rectangle of appropriate aspect, which means 
that the plot region does not respect the x/ylim, but will expand to 
values matching asp=. Next, read up on xaxs= and yaxs= in ?par; by 
default, the axes are extended by 4% at each end, if x/yaxs="i", the 
extension more or less respects the requested plot region. Then the 
plotting devices clip to this region. So to get close to what you wanted, 
you'd have to work out the geometry of the output rectangle, open a device 
with the required shape, and go from there (using Manuel's contribution):

library(rgdal)
climagdiv = readOGR("US102ClimateDivisions", "US102ClimateDivisions")
proj4string(climagdiv) <- CRS("+proj=longlat +datum=WGS84")
x11(width=7, height=4)
plot(as(climagdiv, "Spatial"), xlim=bbox(climagdiv)[1,], ylim=c(20,40),
   xaxs="i", yaxs="i", axes=TRUE)
plot(climagdiv, add=TRUE)

or similar.

It is also possible to use rgeos:

library(rgeos)
SP <- SpatialPoints(rbind(c(-125, 25), c(-67, 40)),
   proj4string=CRS("+proj=longlat +datum=WGS84"))
clipbox <- gEnvelope(SP)
GI <- gIntersects(climagdiv, clipbox, byid=TRUE)
plot(climagdiv)
plot(climagdiv[GI[1,],], border="red", add=TRUE)

to choose Polygons objects at least partly within the chosen rectangle. 
Not that this is not strictly appropriate for geographical coordinates, 
and you may not want to clip either, as entities will appear strange in 
the ouput graphics.

This isn't as simple as it appears, really.

Hope this clarifies,

Roger

>
> Abw
> Manuel
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
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