-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcode.R
187 lines (153 loc) · 6.91 KB
/
code.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
library('dplyr')
library('readr')
library('ggplot2')
library('reshape2')
library('ggalt')
library('rgdal')
library('maptools')
places <- read_csv('bourdain_travel_places.csv')
lat.long <- colsplit(places$coordinates, ",", names = c("lat", "long"))
places <- cbind(places, lat.long)
places <- places %>% select(-coordinates)
## World Map
mdat <- map_data('world')
gmap <- ggplot() +
geom_polygon(dat = mdat,
aes(long, lat, group = group), fill="#e3e3e3") +
borders("world", colour = "white", size=0.1)
gmap.points <- gmap +
geom_point(data = places,
aes(long,
lat, colour = show, alpha = 0.75), size = 1) +
scale_color_manual(values = c('#000000', '#e41a1c', 'deepskyblue3')) +
ggtitle('"If I am an advocate for anything, it is to move."') +
labs(subtitle = "–Anthony Bourdain")
gmap.formatted <- gmap.points +
theme(plot.title = element_text(size = 18),
plot.subtitle = element_text(size = 14),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = 'bottom',
legend.text = element_text(size=10),
legend.title = element_blank(),
legend.key = element_rect(fill=NA),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank()) +
guides(alpha = FALSE)
gmap.formatted
ggsave('bourdain_travel_map.png', width = 6, height = 4.5)
# a different projection and look, via https://cfss.uchicago.edu/dataviz_geospatial.html#geospatial_visualization
gmap.formatted +
coord_map(projection = "polyconic") # too crazy-looking
gmap.formatted +
coord_map(projection = "mollweide", xlim=c(-180, 180)) # better
ggsave('bourdain_travel_map_mollweide.png', width = 6, height = 4.5)
# robinson projection via https://gis.stackexchange.com/questions/44387/use-proj4-to-specify-robinson-projection-with-r-ggmap-and-ggplot2-packages
# see also https://www.jessesadler.com/post/gis-with-r-intro/
gmap.formatted + coord_proj("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
ggsave('bourdain_travel_map_robinson.png', width = 6, height = 4.5)
## US Map with states via https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
# code adapted from https://cfss.uchicago.edu/dataviz_geospatial.html#using_shapefiles
usa <- readOGR("cb_2017_us_state_20m/cb_2017_us_state_20m.shp")
fortify(usa) %>%
head()
gpclibPermit()
usa2 <- usa %>%
fortify(region = "NAME") %>%
as_tibble() %>%
left_join(usa@data, by = c("id" = "NAME"))
usamap <- ggplot(data = usa2 %>% filter(id != "Alaska", id != "Hawaii", id != "Puerto Rico"), mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "white", fill = "#e3e3e3")
# add in A Cook's Tour locations, which Shane Turbeville added to my original dataset: https://public.tableau.com/profile/shanetville#!/vizhome/BourdainsTravels/BourdainsTravels
cooks.tour <- read_csv('Map_data.csv')
cooks.tour <- cooks.tour %>% filter(Show == "A Cook's Tour")
usamap.points <- usamap +
geom_point(data = cooks.tour %>% filter(Country == "United States"),
aes(Longitude,
Latitude, colour = Show, alpha = 0.75, group = Country), size = 2) +
geom_point(data = places %>% filter(!is.na(state) & state != "Hawaii"),
aes(long,
lat, colour = show, alpha = 0.75, group = country), size = 2) +
scale_color_manual(values = c('#264484', '#000000', '#e41a1c', 'deepskyblue3')) +
ggtitle("Anthony Bourdain's U.S. travels")
usamap.formatted <- usamap.points +
theme(plot.title = element_text(size = 18),
plot.subtitle = element_text(size = 14),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = 'bottom',
legend.text = element_text(size=10),
legend.title = element_blank(),
legend.key = element_rect(fill=NA),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank()) +
guides(alpha = FALSE)
usamap.formatted
ggsave('bourdain_travel_usa_contiguous.png', width = 6, height = 4.5)
# Hawaii
hawaiimap <- ggplot(data = usa2 %>% filter(id == "Hawaii"), mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "white", fill = "#e3e3e3")
hawaiimap.points <- hawaiimap +
geom_point(data = places %>% filter(state == "Hawaii"),
aes(long,
lat, colour = show, alpha = 0.75, group = country), size = 2) +
scale_color_manual(values = c('#000000', '#e41a1c', 'deepskyblue3')) +
theme(plot.title = element_text(size = 18),
plot.subtitle = element_text(size = 14),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = 'none',
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank()) +
guides(alpha = FALSE)
hawaiimap.points
ggsave('bourdain_travel_hawaii.png', width = 1.5, height = 1.5)
# Puerto Rico
prmap <- ggplot(data = usa2 %>% filter(id == "Puerto Rico"), mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "white", fill = "#e3e3e3")
prmap.points <- prmap +
geom_point(data = places %>% filter(grepl("Puerto Rico", city_or_area)),
aes(long,
lat, colour = show, alpha = 0.75, group = country), size = 2) +
scale_color_manual(values = c('#000000', '#e41a1c', 'deepskyblue3')) +
theme(plot.title = element_text(size = 18),
plot.subtitle = element_text(size = 14),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = 'none',
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank()) +
guides(alpha = FALSE)
prmap.points
ggsave('bourdain_travel_puertorico.png', width = 1.5, height = 1.5)