在r中对多边形和位置点进行分层

时间:2018-09-12 16:04:22

标签: r ggplot2 leaflet

俄克拉荷马州最近使医用大麻合法化,我正在绘制可在何处设立药房的地图。这取决于两件事:它必须在正确的分区区域内,并且不能离学校,教堂或操场太近。我有两个地图可以显示这些内容,但是无法弄清楚如何将它们分层。我要达到的目的是显示由于离学校,教堂等太近了,所以正确的分区区域中有多少是禁区。

分区代码:

zoning_shapes <- "Primary_Zoning.shp"
zoning <- st_read(zoning_shapes)
library(dplyr)
zoning_1 <- filter(zoning, P_ZONE!="R-1")
zoning_2 <- filter(zoning_1, P_ZONE!="SPUD")
zoning_3 <- filter(zoning_2, P_ZONE!="AA")
zoning_4 <- filter(zoning_3, P_ZONE!="R-2")
zoning_5 <- filter(zoning_4, P_ZONE!="R-4")
zoning_6 <- filter(zoning_5, P_ZONE!="PUD")
zoning_7 <- filter(zoning_6, P_ZONE!="I-3")
zoning_8 <- filter(zoning_7, P_ZONE!="R-A")
zoning_9 <- filter(zoning_8, P_ZONE!="O-1")
zoning_10 <- filter(zoning_9, P_ZONE!="R-3")
zoning_11 <- filter(zoning_10, P_ZONE!="R-A2")
zoning_12 <- filter(zoning_11, P_ZONE!="R-1ZL")
zoning_13 <- filter(zoning_12, P_ZONE!="R-3M")
zoning_14 <- filter(zoning_13, P_ZONE!="R-4M")
zoning_15 <- filter(zoning_14, P_ZONE!="R-MH-1")
zoning_16 <- filter(zoning_15, P_ZONE!="R-MH-2")
zoning_17 <- filter(zoning_16, P_ZONE!="C-HC")
zoning_18 <- filter(zoning_17, P_ZONE!="HP")
zoning_19 <- filter(zoning_18, P_ZONE!="NC")
zoning_20 <- filter(zoning_19, P_ZONE!="AE-1")
zoning_21 <- filter(zoning_20, P_ZONE!="AE-2")
library(ggplot2)
library(sf)
ggplot(zoning_21) + geom_sf() +
  theme_void() +
  theme(panel.grid.major = 
          element_line(colour = 'transparent'))

禁区代码:

library(dplyr)
library(tigris)
library(sf)
library(ggplot2)
library(leaflet)
library(readr)
locations <- read_csv("Marijuana_map_CSV.csv")
View(locations)
mew <- colorFactor(c("red", "blue", "purple"), domain=c("School", "Church", "Playground"))
okc_locations <- leaflet(locations) %>%
  addTiles() %>%
  setView(-97.5164, 35.4676, zoom = 7) %>% 
  addCircles(~Longitude, ~Latitude, popup=locations$Name, 
             weight = 3, radius=304.8, 
             color=~mew(locations$Type), stroke = T, 
             fillOpacity = 0.8) %>%
  addPolygons(data=zoning_21, fillColor = "limegreen",
              fillOpacity = 0.5, weight = 0.2,
              smoothFactor = 0.2)
okc_locations

我遇到的问题是,当我尝试将okc_locations代码添加到zoning_21代码时,我得到了一个距离很远的红点和一个非常压缩的城市分区版本。当我尝试将分区多边形添加到禁止点地图时,它们不会显示。

关于如何使这两张地图一起玩的任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

根据我们在评论中的谈话,看来您在使用不同的预测时遇到了问题,在这种情况下,您将要使用st_transform(记录在here中)

首先,我编造了一些虚假数据:

locations <-
  data.frame(Name = c("St. Josephs", "St. Anthony", "Edwards Elementary"),
              type = c("Church", "Playground", "School"),
              long = c(35.4722725, 35.4751038, 35.4797194),
              lat = c(-97.5202865,-97.5239513,-97.4691759))

我下载了所有县的老虎shapefile,然后缩小到俄克拉荷马州:

us_counties <- read_sf("cb_2017_us_county_500k.shp")
ok_county <- subset(us_counties, STATEFP == "40" & NAME == "Oklahoma")

> print(st_crs(ok_county))
Coordinate Reference System:
  EPSG: 4269 
  proj4string: "+proj=longlat +datum=NAD83 +no_defs"

所以我然后使用了st_transform

t2 <- st_transform(ok_county, "+proj=longlat +datum=WGS84")
> print(st_crs(t2))
Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"

并按如下方式将其加载到leaflet中:

leaflet(locations) %>%
  addTiles() %>%
  setView(-97.5164, 35.4676, zoom = 11) %>% 
  addMarkers(~lat, ~long, popup=locations$Name) %>%
  addPolygons(data=t2, fillColor = "limegreen",
              fillOpacity = 0.5, weight = 0.2,
              smoothFactor = 0.2)

产生此地图: enter image description here