-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-raster-maps-solution.R
87 lines (77 loc) · 1.71 KB
/
01-raster-maps-solution.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
library(tidyverse)
library(ggmap)
library(here)
# set default theme
theme_set(theme_minimal())
# load data
nyc_311 <- read_csv(file = here("data", "nyc-311.csv"))
glimpse(nyc_311)
# Obtain map tiles using ggmap for New York City
## store bounding box coordinates
nyc_bb <- c(
left = -74.263045,
bottom = 40.487652,
right = -73.675963,
top = 40.934743
)
nyc <- get_stamenmap(
bbox = nyc_bb,
zoom = 11
)
## plot the raster map
ggmap(nyc)
# Generate a scatterplot of complaints about sidewalk conditions
## initialize map
ggmap(nyc) +
# add layer with scatterplot
# use alpha to show density of points
geom_point(
data = filter(nyc_311, complaint_type == "Sidewalk Condition"),
mapping = aes(
x = longitude,
y = latitude
),
size = .25,
alpha = .05
)
# Generate a heatmap of complaints about sidewalk conditions
# Do you see any unusual patterns or clusterings?
## initialize the map
ggmap(nyc) +
# add the heatmap
stat_density_2d(
data = filter(nyc_311, complaint_type == "Sidewalk Condition"),
mapping = aes(
x = longitude,
y = latitude,
fill = stat(level)
),
alpha = .1,
bins = 50,
geom = "polygon"
)
# Obtain map tiles for Roosevelt Island
roosevelt_bb <- c(
left = -73.967121,
bottom = 40.748700,
right = -73.937080,
top = 40.774704
)
roosevelt <- get_stamenmap(
bbox = roosevelt_bb,
zoom = 14
)
## plot the raster map
ggmap(roosevelt)
# Generate a scatterplot of food poisoning complaints
## initialize the map
ggmap(roosevelt) +
# add a scatterplot layer
geom_point(
data = filter(nyc_311, complaint_type == "Food Poisoning"),
mapping = aes(
x = longitude,
y = latitude
),
alpha = 0.2
)