【发布时间】:2020-08-09 13:21:57
【问题描述】:
我正在尝试将枪支暴力发生坐标点绘制到纽约的 shapefile 地图上。我应用了一个 Google 脚本将原始数据集街道地址转换为纬度和经度坐标。我将它.csv 导出到 RStudio。通过删除不必要的列和 NA 值 + 更改 Lat,进一步清理数据。列到数字。
在将点分层到 shapefile 地图上之前,我似乎已经完成了所有工作。当我运行以下代码时,它会分别返回与地图不同的坐标点(见附图)。也就是说,它们没有分层在一起,因此我最终可以使用它来创建等值线图。此外,下面的点图像似乎没有显示数据集中的所有纬度/经度坐标。总共有 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"))
Where I got the shapefile of New York State
非常感谢您的帮助。
【问题讨论】:
-
我将您的链接评论中包含的代码放在 plot(New_York) 和 New_York = readOGR 之间。它仍然分别返回纽约的点和地图。这是添加的新代码: map