-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathspatial-join.R
60 lines (44 loc) · 1.55 KB
/
spatial-join.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
library(sf)
library(ggplot2)
library(dplyr)
libraries <- read_sf("https://data.cityofchicago.org/resource/psqp-6rmg.geojson")
areas <- read_sf("https://data.cityofchicago.org/resource/igwz-8jzy.geojson")
# won't work if not projected
st_intersects(areas, libraries)
# project the data
st_crs(libraries)
libraries <- st_transform(libraries, 32616)
areas <- st_transform(areas, 32616)
# make a map of both!
ggplot() +
geom_sf(data = areas) +
geom_sf(data = libraries)
# find which libraries are in which areas
intersects <- st_intersects(areas, libraries)
no_library_areas <- filter(areas, lengths(intersects) == 0)
# map places in chicago without libraries
ggplot() +
geom_sf(data = areas) +
geom_sf(data = no_library_areas, fill = "pink")
# try making a map of areas in chicago with more than 1 library!
many_library_areas <- filter(areas, lengths(intersects) > 1)
ggplot() +
geom_sf(data = areas) +
geom_sf(data = no_library_areas, fill = "red") +
geom_sf(data = many_library_areas, fill = "green")
?ggsave
ggsave("gis-visualization/spring-2019/doc/figs/green-red-libraries.jpg", width = 5)
# SPATIAL JOIN
?st_join
# count how many libraries in each area
libraries_with_areas <- st_join(libraries, areas)
count(libraries_with_areas, community)
# use the pipe to do it in one line
st_join(libraries, areas) %>% count(community)
naniar::vis_miss(libraries_with_areas)
naniar::gg_miss_var(libraries_with_areas)
st_join(areas, libraries)
ggplot() +
geom_sf(data = areas) +
geom_sf(data = libraries_with_areas, aes(color = community))
# alt - gives you <-