-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path04-gis-3.R
49 lines (36 loc) · 1.39 KB
/
04-gis-3.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
# Load libraries for use
library(sf)
library(ggplot2)
library(dplyr)
# Read in libraries and areas data
libraries <- st_read("https://data.cityofchicago.org/resource/psqp-6rmg.geojson")
areas <- st_read("https://data.cityofchicago.org/resource/igwz-8jzy.geojson")
# Project both
libraries <- st_transform(libraries, 32616)
areas <- st_transform(areas, 32616)
# Make a ggplot with libraries and community areas
ggplot() +
geom_sf(data = areas) +
geom_sf(data = libraries)
# Find which areas intersect with libraries and save as a variable called "intersects"
intersects <- st_intersects(areas, libraries)
intersects
?st_intersects
# Filter areas by *without* libraries. Save as a variable called "no_lib" Hint: use "==" instead of ">" in the logical comparison
no_lib <- filter(areas, lengths(intersects) == 0)
# Make a ggplot with libraries, community areas, and community areas without libraries
ggplot() +
geom_sf(data = areas) +
geom_sf(data = no_lib, fill = "pink") +
geom_sf(data = libraries)
ggsave("doc/areas_without_libraries_big.png", width = 4, height = 4)
st_join(libraries, areas)
st_join(libraries, areas["community"])
areas <- select(areas, community, area_numbe, geometry)
lib_join <- st_join(libraries, areas)
# This is just to help me figure out which columns to select
names(areas)
str(areas)
glimpse(areas)
lib_counts <- count(lib_join, community)
arrange(lib_counts, desc(n))