简体   繁体   中英

Plotting map points in R

I have successfully loaded a shapefile of NYC PUMA areas into R with maptools and I want to plot 55 points on top of it that I have in another file as follows:

X   Y   pumace10    events_2008 events_2009
-73.9092456917  40.8916125162   3701    2   0
-73.8617096298  40.8899373255   3702    0   0
-73.8010284966  40.8460832277   3703    1   1

However, the points will not plot.

First I do this to plot the shapefile:

plot(nycs)

And it plots the shapefile 这是shapefile

Then I try to plot the points on top but no matter which of the following I do it always fails:

points(nyc_data$X,nnyc_data$Y,pch=20,col="red")

or

plot(nyc_data, pch=16, col='firebrick',add=TRUE)

or

plot(nyc_data$X,nyc_data$Y,pch=20,col="red")

(that final one plots the data on a new plot that is just an XY scatter instead of overlaid on the shapefile)

Any ideas how to do this?

EDIT, files added (amended to working files, hopefully!):

Shapefile info: https://www.sendspace.com/file/wbqrpb Points file: https://www.sendspace.com/file/9yrrbu

Anyway you could send the files you are using?

My guess is that you have one of two problems:

1) either the "points" spatial data frame or the NYC PUMA file do not have a coordinate reference system.

2) they both have reference systems, but they are different.

Likely, your problem is that the points lack a reference system.

EDIT:

The problem is that the two datasets are in different coordinates, and I'm not sure what the coordinate system for the points is. Here is my code. The problem is that the CRS for the points is clearly neither regular lat/long nor the projection used in the shapefile. If you have more info on the source of the data points maybe we can see what projection they use.

library(sp)
library(maptools)
library(rgdal)
library(rgeos)

Points=read.csv("nyc_data_sample.csv",stringsAsFactors=F)
shapefile=readOGR("shapefiles","nyu_2451_34512")



Points_Shape = SpatialPoints(Points[,c("X","Y")],proj4string=CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
#Points_Shape = SpatialPoints(Points[,c("X","Y")],proj4string=CRS(proj4string(shapefile)))

Points_Shape = SpatialPointsDataFrame(Points_Shape,Points)

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