简体   繁体   中英

Plotting points on a map in the rgdal package with R

Borrowing code from Rob Berry ( http://rob-barry.com/2015/06/14/Mapping-in-R/ ), I make a map of NY city. I have many lat long points I wish to plot on the map. The problem is that plotting a map like this results in plot area way outside of reasonable lat long ranges, so I assume there must either be a way to convert my points to the map scale, or rescale the map so the plot space can lay down lat lon points with the points() function. Here is the code from Rob Berry:

download.file(destfile = "nypp_15b.zip")
unzip(zipfile = "nypp_15b.zip")
library("rgdal")
nypp <- readOGR("nypp_15b", "nypp")
plot(nypp)

Nice map! But now notice the plot extents:

par(“usr”)

The plots space numbers look like 888196.7, 1092361.0, 114013.0, 278953.2, so clearly lat lon points like the ones below won't show up on the map. So how do I get my points to plot correctly on the map?

lat <- c(40.75002, 40.74317)
lon <- c(-73.96905 -74.00366)

The following doesn't work because the scale is so different:

points(lat,lon, col = “red”)

Thank you very much.

nypp is in projected coordinate system, so you need to change to coordinate system of your points or of nypp. You can do something like this :-

nypp <- readOGR("nypp_15b", "nypp")
## Check the CRS of nypp
crs(nypp)
## CRS arguments:
 +proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000
+y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0 

plot(nypp)

lat <- c(40.75002, 40.74317)
lon <- c(-73.96905, -74.00366)

df <- data.frame(lat, lon)

## Convert to spatial dataframe and change the coordinates of the points
coordinates(df) <- ~lon + lat
crs(df) <- CRS("+proj=longlat +datum=WGS84")
df <- spTransform(df, crs(nypp))

## Add points to the plot
points(df$lon, df$lat, col = "red", pch =19)

Result:

点叠加

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM