Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting SpatialPointsDataFrame over a SpatialPolygonsDataFrame

Tags:

r

spatial

rgdal

I have a SpatialPolygonsDataFrame that I plot using the following command:

spplot(milanoNILNew, "percPolVita",at = c(0,0.0696,0.08979,0.0974,0.1116,0.181),col.regions =pal,main="Quota Clienti Unipol con Polizze Vita ")

I have created a spatialPointsDataFrame with the following command

agenziePoints<-SpatialPointsDataFrame(coords=datiAgenzie[,c("Long","Lat")],data=datiAgenzie,proj4string = llCRS, match.ID = TRUE).

I'm able to plot the polygons but I am not able to represent agenziePoints on the polygons on the same plot. Thanks in advance for the support.

like image 886
Giorgio Spedicato Avatar asked May 08 '14 16:05

Giorgio Spedicato


People also ask

What is a SpatialPointsDataFrame?

names (i.e. is a data. frame), then the SpatialPointsDataFrame object is formed by matching the row names of both components, leaving the order of the coordinates in tact. Checks are done to see whether both row names are sufficiently unique, and all data are matched.

What is an SP object in R?

The sp package provides classes and methods for dealing with spatial data in R1. The spatial data structures implemented include points, lines, polygons and grids; each of them with or without attribute data.


1 Answers

Just use the base plot function with the add=TRUE argument.

require(sp)

# Create polygons
sr=SpatialPolygons(list(
  Polygons(list(Polygon(cbind(c(180114,180553,181127,181477,181294,181007,180409, 
  180162,180114),c(332349, 332057, 332342, 333250, 333558, 333676, 332618, 332413, 332349)))),"1"),
  Polygons(list(Polygon(cbind(c(180042,180545,180553,180314,179955,179142,179437,179524,179979,180042), 
  c(332373,332026,331426,330889,330683,331133,331623,332152,332357,332373)))),"2")))
srdf=SpatialPolygonsDataFrame(sr, data.frame(cbind(1:2,1:2), row.names=c("1","2")))

# Add points (sp meuse dataset)
data(meuse)
coordinates(meuse) = ~x+y

# Create a color vector for the meuse points
color <- rep("xx", nrow(meuse@data))
  color[(meuse@data$copper > 0)&(meuse@data$copper <= 31)] <- "black"
    color[(meuse@data$copper  > 31)] <- "red"

# Plot polygons with points overlaid    
plot(srdf, col=c("grey","blue"))
  plot(meuse, col=color, pch=19, add=TRUE) 

If you really want to use the spplot function you could implements something like this:

# spplot approach
pts=list("sp.points", meuse, pch=19, col=color)
  spplot(srdf, "X1", col.regions=c("grey","blue"), colorkey=FALSE,
         sp.layout=list(pts)) 
like image 75
Jeffrey Evans Avatar answered Sep 29 '22 18:09

Jeffrey Evans