[R-sig-Geo] Map layer

Roger Bivand Roger.Bivand at nhh.no
Thu Aug 14 11:28:11 CEST 2008


On Thu, 14 Aug 2008, Paul Hiemstra wrote:

> Hi Fernando,
>
> If you use the sp classes for spatial data you can use the spplot function to 
> make very nice plots. See 
> http://cran.r-project.org/doc/Rnews/Rnews_2005-2.pdf for more information on 
> sp. See 
> http://wiki.r-project.org/rwiki/doku.php?id=tips:spatial-data:import_export 
> for information on how to import your data into sp-classes.
>
> In regard to your problem with the points outside the map. If your data is 
> stored in sp objects you can use the overlay() function from the sp-package 
> to determine which points are outside the polygon, see ?overlay for more 
> details. Those points can be deleted from the sp-object and they will not 
> appear in the plot.

In fact, there was a bug in the interface function map2SpatialPolygons() 
in the maptools package - it failed for map objects with only one closed 
polygon. From 0.7-15 (submitted to CRAN, may take a little while because 
both Kurt Hornik (CRAN) and Uwe Ligges (Windows binary packages) are at 
the excellent useR! conference in Dortmund - I am too), this works:

library(maps)
library(sp)
library(maptools)
ill <- map('state','illinois', plot=FALSE, fill=TRUE)
ill_SP <- map2SpatialPolygons(ill, IDs=ill$names,
   proj4string=CRS("+proj=longlat"))
plot(ill_SP)
# next two lines to generate some random points on the bounding
# box of Illinois
SG <- Sobj_SpatialGrid(ill_SP)$SG
pts <- spsample(SG, n=500, type="random")
plot(pts, add=TRUE)
o <- overlay(pts, ill_SP)
plot(pts[!is.na(o),], col="red", add=TRUE)

Hope this helps,

Roger

PS. The useR conference has lots of excellent talks, and surprisingly many 
are spatial. Next year's conference will be in Rennes, France, and in 
2010, it will probably be in North America.

>
> hth,
>
> Paul
>
> Fernando Miguez wrote:
>>  Hi all,
>>
>>  I would like to be able to do the following
>>
>>  map('state','illinois')
>>  points(x,y)
>>
>>  Up to this point everything is fine. However, some of the points fall
>>  outside the Illinois map boundaries. What I would like to do is to add a
>>  layer where the inside of the Illinois polygon is transparent (so that the
>>  points can be seen) but the points outside the boundary are covered so
>>  they are not seen. I thought that using some variation of map with its
>>  arguments would work, but I haven't been able to figure it out. I see that
>>  it is quite possible that I'm using the wrong tools all together ... let
>>  me know.
>>
>>  Any help is appreciated. Thanks.
>>
>>  Fernando
>> 
>
>
>

-- 
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