generated from KSUDS/p3_spatial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolygon.r
304 lines (240 loc) · 9.14 KB
/
polygon.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
###10/11/2021 - Polygons###
install.packages("remotes")
remotes::install_github("ropensci/USAboundaries")
remotes::install_github("ropensci/USAboundariesData")
install.packages("USAboundaries")
install.packages("USAboundariesData", repos = "http://packages.ropensci.org", type = "source")
install.packages("leaflet")
library(tidyverse)
library(sf) #sf and stars packages operate on simple feature obj.
library(USAboundaries)
library(leaflet)
httpgd::hgd() #VScode
httpgd::hgd_browse() #VsCode
dat <- read_rds("/Users/ashleyrabanales/Projects_ST/p3_AshLee/data/chipotle_nested.rds")
dat <- dat %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
select(dat, street_address, region, geometry)
cal <- USAboundaries::us_counties(states = "California")
ggplot() +
geom_sf(data = cal) +
geom_sf(data = filter(dat, region == "CA"))
ggplot() +
geom_sf(data = cal, aes(fill = awater)) +
geom_sf_text(data = cal, aes(label = name), color = "grey")
cal %>%
select(-9) %>% # has state_name twice removing one.
mutate(
sf_area = st_area(geometry),
sf_middle = st_centroid(geometry)
)
chipotle_in_county <- st_join(dat, cal, join = st_within)
chipotle_in_county %>%
as_tibble() %>%
##polygon2##
library(tidyverse)
library(sf)
library(USAboundaries)
library(leaflet)
httpgd::hgd() # for VSCode
httpgd::hgd_browse() # for VSCode
dat <- read_rds("/Users/ashleyrabanales/Projects_ST/p3_AshLee/data/chipotle_nested.rds") %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
cal <- us_counties(states = "California") %>%
select(countyfp, countyns, name, aland, awater, state_abbr, geometry)
cal %>%
mutate(
states_area = aland + awater,
sf_area = st_area(geometry)) %>%
select(name, states_area, aland, sf_area, awater) %>%
filter(name == "Santa Barbara")
ksu <- tibble(lat = 34.037876, long = -84.58102) %>%
st_as_sf(coords = c("long", "lat"), crs = 3310)
# https://epsg.io/4326 units are degrees
calw <- cal %>% #st_transform - changing the projections
st_transform(3310) %>% # search https://spatialreference.org/ref/?search=california&srtext=Search. Units are in meters for buffer.
filter(name != "San Francisco") %>%
mutate(
aland_acres = aland * 0.000247105,
awater_acres = awater * 0.000247105,
percent_water = 100 * (awater / aland),
sf_area = st_area(geometry),
sf_center = st_centroid(geometry),
sf_length = st_length(geometry),
sf_distance = st_distance(sf_center, ksu),
sf_buffer = st_buffer(sf_center, 24140.2), # 24140.2 is 15 miles
sf_intersects = st_intersects(., filter(., name == "Los Angeles"), sparse = FALSE)
) #89 intersects county that arent part to add to LA.
ggplot(data = calw) +
geom_sf(aes(fill = sf_intersects)) +
geom_sf(aes(geometry = sf_buffer), fill = "white") +
geom_sf(aes(geometry = sf_center), color = "darkgrey") +
geom_sf_text(aes(label = name), color = "lightgrey") +
geom_sf(data = filter(dat, region == "CA"), color = "black") + # our chipotle locations
theme_bw()
ggplot(data = calw) +
geom_sf(aes(fill = sf_intersects)) +
geom_sf(aes(geometry = sf_buffer), fill =NA) +
geom_sf(aes(geometry = sf_center), color = "darkgrey") +
geom_sf_text(aes(label = name), color = "lightgrey") +
geom_sf(data = filter(dat, region == "CA"), color = "black") + # our chipotle locations
theme_bw()
##the join chart build process
store_in_county <- st_join(dat, cal, join = st_within) %>%
select(placekey, city, region, geometry, countyfp, name)
store_in_county_count <- store_in_county %>%
as_tibble() %>%
count(countyfp, name) %>%
filter(!is.na(countyfp)) # drop the NA counts.
calw <- calw %>%
left_join(store_in_county_count, fill = 0) %>%
replace_na(list(n = 0))
calw %>%
ggplot() +
geom_sf(aes(fill = n)) +
scale_fill_continuous(trans = "sqrt") +
geom_sf(data = filter(dat, region == "CA"), color = "white", shape = "x") +
theme_bw() +
theme(legend.position = "bottom") +
labs(fill = "Number of Chipotle\nstores")
#Unnesting, calculating, then joining
#Creating our county group columns
#Notice how the sf object structure is kept for the left object dat and the non-geometry columns
# are brought into dat from the cal object.
dat_wc <- st_join(dat, cal, join = st_within)
#Unnest and pivot - complicate join
days_week_long <- dat_wc %>%
filter(region == "CA") %>%
as_tibble() %>% # notice this line to break the sf object rules.
rename(name_county = name) %>%
unnest(popularity_by_day) %>%
select(placekey, city, region, contains("raw"),
name, value, geometry, countyfp, name_county)
#Now we have our table of popularity by day in long format. We want to move
#the days to columns with the counts by day in each column (pivot_wider())
days_week <- days_week_long %>%
pivot_wider(names_from = name, values_from = value)
days_week
#Calculating summaries
#Now we can create summaries using our new pivoted columns
# average visits per day over the n stores by county.
visits_day_join <- days_week %>%
group_by(countyfp, name_county) %>%
summarise(
count = n(),
Monday = sum(Monday, na.rm = TRUE) / count,
Tuesday = sum(Tuesday, na.rm = TRUE) / count,
Wednesday = sum(Wednesday, na.rm = TRUE) / count,
Thursday = sum(Thursday, na.rm = TRUE) / count,
Friday = sum(Friday, na.rm = TRUE) / count,
Saturday = sum(Saturday, na.rm = TRUE) / count,
Sunday = sum(Sunday, na.rm = TRUE) / count,
) %>%
ungroup()
#Combining summaries back into our calw sf object.
calw <- calw %>%
left_join(visits_day_join %>% select(-name_county)) %>%
replace_na(list(Monday = 0, Tuesday = 0, Wednesday = 0,
Thursday = 0, Friday = 0, Saturday = 0, Sunday = 0))
#Plotting Saturday use
#Let’s make a plot similar to our previous chart.
calw %>%
ggplot() +
geom_sf(aes(fill = Saturday)) +
geom_sf(data = filter(dat, region == "CA"), color = "white", shape = "x") +
theme_bw() +
theme(legend.position = "bottom") +
labs(fill = "Average store traffic",
title = "Saturday traffic for Chipotle")
#We can add our count variable to provide some additional insight
calw %>%
ggplot() +
geom_sf(aes(fill = Saturday)) +
geom_sf(aes(geometry = sf_center, size = count), color = "grey") +
theme_bw() +
scale_size_continuous(breaks = c(1, 5, 10, 25, 50, 75),
trans = "sqrt", range = c(2, 15)) +
labs(
fill = "Average store traffic",
size = "Number of stores",
title = "Saturday traffic for Chipotle")
#PLOTTING WITH LEAFLET
calw_4326 <- st_transform(calw, 4326) # will need in 4326 for leaflet
bins <- c(0, 10, 20, 30, 50, 70, 90, 110)
pal <- colorBin("YlOrRd", domain = calw_4326$n)
m <- leaflet(calw_4326) %>%
addPolygons(
data = calw_4326,
fillColor = ~pal(n),
fillOpacity = .5,
color = "darkgrey",
weight = 2) %>%
addCircleMarkers(
data = filter(dat, region == "CA"),
radius = 3,
color = "grey") %>%
addProviderTiles(providers$CartoDB.Positron)
###10/18 - A time and space example
#package & data
library(tidyverse)
library(sf)
library(USAboundaries)
library(leaflet)
library(geofacet)
httpgd::hgd()
httpgd::hgd_browse()
dat <- read_rds("chipotle_nested.rds") %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
states <- us_states() %>%
filter(!state_name %in% c("Alaska", "Hawaii", "Puerto Rico")) %>%
st_transform(4326)
##Build plotting data
#SPATIAL
dat_space <- dat %>%
select(placekey, street_address, city, stusps = region, raw_visitor_counts) %>%
filter(!is.na(raw_visitor_counts)) %>%
group_by(stusps) %>%
summarise(
total_visitors = sum(raw_visitor_counts, na.rm = TRUE),
per_store = mean(raw_visitor_counts, na.rm = TRUE),
n_stores = n(),
across(geometry, ~ sf::st_combine(.)),
) %>%
rename(locations = geometry) %>%
as_tibble()
states <- states %>%
left_join(dat_space)
#TEMPORAL
dat_space <- dat %>%
select(placekey, street_address, city, stusps = region, raw_visitor_counts) %>%
filter(!is.na(raw_visitor_counts)) %>%
group_by(stusps) %>%
summarise(
total_visitors = sum(raw_visitor_counts, na.rm = TRUE),
per_store = mean(raw_visitor_counts, na.rm = TRUE),
n_stores = n(),
across(geometry, ~ sf::st_combine(.)),
) %>%
rename(locations = geometry) %>%
as_tibble()
states <- states %>%
left_join(dat_space)
#PLOTS WITH GEOFACET
dat_time %>%
ggplot(aes(x = dayMonth, y = dayAverage)) +
geom_point() +
geom_smooth() +
geom_text(
aes(label = stores_label),
x = -Inf, y = Inf,
hjust = "left", vjust = "top") +
facet_geo(~region, grid = "us_state_grid2", label = "name")
dat_time %>%
ggplot(aes(x = dayMonth, y = dayAverage)) +
geom_point() +
geom_smooth() +
geom_text(aes(label = stores_label),
x = -Inf, y = Inf,
hjust = "left", vjust = "top") +
coord_cartesian(ylim = c(5, 25)) +
facet_geo(~region, grid = "us_state_grid2", label = "name")