[R-sig-Geo] plot shape file using lattice xyplot

Roger Bivand Roger.Bivand at nhh.no
Thu Dec 17 09:05:01 CET 2009


On Wed, 16 Dec 2009, Zia Ahmed wrote:

> Thanks Andrews. It works fine. Nothing wrong with sp plot.  I need to plot 
> boundary around of sampling locations. R-script I have  written, I used 
> xyplot and levelplot.

This look like a two-step question, assuming that you need a line 
representation of possible polygon boundaries from the shapefile. If it is 
a SpatialLines object, omit the first step (it will work on 
SpatialPolygons* objects with appropriate slot names, but the intention 
is to use lines, so make sure that they are):

library(sp)
library(maptools)
xx <- readShapeSpatial(system.file("shapes/sids.shp",
   package="maptools")[1])
proj4string(xx) <- CRS("+proj=longlat +ellps=clrk66")
# assigning geographical coordinates for this data set
class(xx)

lxx <- as(xx, "SpatialLines")
class(lxx)

lns <- slot(lxx, "lines")
olns <- lapply(lns, slot, "Lines")
ocrds <- matrix(nrow=0, ncol=2)
for (i in seq(along=olns)) {
   for (j in seq(along=olns[[i]])) {
     crds <- rbind(slot(olns[[i]][[j]], "coords"), c(NA, NA))
     ocrds <- rbind(ocrds, crds)
   }
}

plot(ocrds, type="l")
# sanity check

pts <- coordinates(spsample(xx, n=400, type="random"))

library(lattice)
xyplot(y~x, as.data.frame(pts), pch=1, cex=.8, col="black",
   xlab=list("Easting (m)",cex=.9), ylab=list("Northing (m)",cex=.9),
   aspect = mapasp(xx), # setting aspect for geographical coordinates
   panel = function(...) {
     panel.xyplot(...)
     panel.lines(ocrds)
   }
)

In summary: make an S-style coordinates matrix (lines separated by NA 
rows), then pass through panel.lines().

Hope this helps,

Roger

>
> Thanks  again
>
> Zia
>
> Felix Andrews wrote:
>> What's wrong with using spplot? It is based on xyplot anyway.
>> 
>> If you want to use xyplot, you can use the panel function that sp defines:
>> sp.polygon
>> 
>> You can read in the shapefile with the RGDAL package:
>> shp <- readOGR(...)
>> 
>> 
>> 2009/12/17 Zia Ahmed <zua3 at cornell.edu>:
>> 
>>> Hi,
>>> 
>>> I am trying to plot a shape file  in R. I can do  it easily using plot or
>>> spplot function.  But I want plot this map with  lattice xyplot function.
>>> Any one has any idea? I do not want use "maps" package.
>>> 
>>> Help will be appreciated...
>>> 
>>> Thanks
>>> 
>>> Zia
>>> 
>>> library(maptools)
>>> library(lattice)
>>> bound<-read.shape("Boundary.shp")
>>> #OR
>>> bound<-readShapePoly("Boundary.shp")
>>> plot(bound)
>>> spplot(bound)
>>> 
>>> # Or following way
>>> 
>>> bound<-Map2poly(bound)
>>> xybd<-attr(bound,"maplim")
>>> 
>>> plot(xybd$x,xybd$y, type="n",asp=1, xlab="", ylab="")
>>> for (i in 1:length(bound)) lines(bound[[i]]) # How do I apply this code in
>>> following  panel.function of  xyplot?
>>> 
>>> 
>>> windows(width=5, height=5)
>>> xyplot(y~x, as.data.frame(tala),pch=1, cex=.8,col="black",
>>>      xlab=list("Easting (m)",cex=.9),ylab=list("Northing (m)",cex=.9),
>>>      aspect = "iso",
>>>      panel = function(...) {
>>>             panel.xyplot(...)
>>>             panel.abline(h = 0:4*5000 + 545000, v= 0:4*5000 + 2650000,
>>>             col = "light grey")
>>>             panel.points(coordinates(tala.nr),cex=.95,pch=20, col="black")
>>>             panel.text(2671000, 563000, cex=1, " (a)")
>>>             },
>>> )
>>> 
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>> 
>> 
>> 
>> 
>> 
>
> _______________________________________________
> 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