如何获得点(经纬度)以显示在我的shapefile地图上?

问题描述 投票:1回答:1

我正在尝试将枪支暴力发生的坐标点绘制到纽约的shapefile地图上。我应用了Google脚本,将原始数据集的街道地址转换为纬度和经度坐标。我将其.csv导出到RStudio中。通过删除不必要的列和NA值+更改Lat,进一步清理数据。列到数值。

我似乎已经完成了所有工作,直到在shapefile映射上将点分层为止。当我运行以下代码时,它返回的坐标点与地图分开(请参见所附图像)。也就是说,它们没有分层在一起,因此我最终可以使用它来创建一个Choropleth贴图。另外,下面的点图像似乎并未显示数据集中的所有纬度/经度坐标。总共有大约500次事件,提供的坐标数据分布在整个纽约州。我对所显示的内容不太自信,但这可能是另一个问题的话题。

library(data.table)
library(sp)
library(rgdal)
library(ggplot2)


df_2 <- Gun_Violence_Clean_3 %>%
  select(-`Incident ID`, -Operations, -`City 2`, -Combine, -`Lat & Long`) %>%
  na.omit()
df_2

df_2$Lat <- as.numeric(df_2$Lat)

coordinates(df_2) = c("Lat","Long")
crs.geo1 = CRS("+proj=longlat")  
proj4string(df_2) = crs.geo1  

plot(df_2, pch = 20, col = "steelblue")


New_York = readOGR(dsn = "./NYS Boundaries", layer = "new-york-state-city-and-town- 
boundaries")
plot(New_York)
points(df_2, pch = 20, col = "orange")

可复制数据:

structure(list(`Incident ID` = c(1664753, 1664770, 1664768, 1664751, 
1664723, 1664721), `Incident Date` = c("23-Apr-20", "22-Apr-20", 
"22-Apr-20", "22-Apr-20", "22-Apr-20", "22-Apr-20"), State = c("New 
York", 
"New York", "New York", "New York", "New York", "New York"), 
`City Or County` = c("Buffalo", "Schenectady", "Schenectady", 
"Albany", "Brooklyn", "Corona (Queens)"), Address = c("50 block of 
Langmeyer Ave", 
"1009 McClyman St", "1013 McClyman St", "200 block of Second Ave", 
"255 Havemeyer St", "225-37 Murdock Ave"), `# Killed` = c(1, 
0, 0, 0, 0, 0), `# Injured` = c(0, 0, 1, 1, 0, 1), Operations = 
c("N/A", 
"N/A", "N/A", "N/A", "N/A", "N/A"), `City 2` = c("Buffalo", 
"Schenectady", "Schenectady", "Albany", "Brooklyn", "Corona (Queens)"
), Combine = c("50 block of Langmeyer Ave Buffalo", "1009 McClyman St 
Schenectady", 
"1013 McClyman St Schenectady", "200 block of Second Ave Albany", 
"255 Havemeyer St Brooklyn", "225-37 Murdock Ave Corona (Queens)"
), `Lat & Long` = c("42.92484, -78.815534", "#ERROR!", 
"42.80176729999999, -73.9331919", 
"42.6390962, -73.77026289999999", "40.7079479, -73.95942509999999", 
"40.703342, -73.731005"), Lat = c("42.92484000000", "#ERROR!", 
"42.80176730000", "42.63909620000", "40.70794790000", "40.70334200000"
), Long = c(-78.815534, NA, -73.9331919, -73.7702629, -73.9594251, 
-73.731005)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

PointsMap of New York

Dataset Screenshot

Where I got the shapefile of New York State

非常感谢您的帮助。

r ggplot2 maps sp
1个回答
0
投票

使用此答案作为指导:out of bounds latitude and longitude values in converted shape file using ggplot

这是最基本的解决方案:

library(rgdal)库(sp)

df_2 <- structure(list(`Incident Date` = c("23-Apr-20", "22-Apr-20", 
"22-Apr-20", "22-Apr-20", "22-Apr-20"), `City Or County` = c("Buffalo", 
"Schenectady", "Albany", "Brooklyn", "Corona (Queens)"), `# Killed` = c(1, 
0, 0, 0, 0), `# Injured` = c(0, 1, 1, 0, 1), Lat = c(42.92484, 
42.8017673, 42.6390962, 40.7079479, 40.703342), Long = c(-78.815534, 
-73.9331919, -73.7702629, -73.9594251, -73.731005)), row.names = c(NA, 
-5L), na.action = structure(c(`2` = 2L), class = "omit"), class = c("tbl_df", 
"tbl", "data.frame"))

#read shape file
New_York = readOGR(dsn = "./NYS Boundaries", layer = "Counties")

#Transform the shape file coordinates to Lat/Longitude
NY <-spTransform(New_York, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
plot(NY, xlab="long")
axis(1)  #Check the coordinates
axis(2)
#plot the points
points(x=df_2$Long, y=df_2$Lat, pch = 20, col = "orange")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.