-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_maps.Rmd
More file actions
252 lines (166 loc) · 6.07 KB
/
04_maps.Rmd
File metadata and controls
252 lines (166 loc) · 6.07 KB
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
# Maps
```{r}
library(RColorBrewer)
library(leaflet)
library(terra)
# for metadata/attributes- vectors or rasters
library(raster)
library(sf)
library(rnaturalearthdata)
library(rnaturalearth)
library(tidyverse)
df_simplified <- read_rds(here::here("data/processed/formaps.rds")) # org and shape data
neighborhoods <- read_rds(here::here("data/processed/neighborhood_shape_data.rds"))#neighborhood stat data (read into R)
```
# Plot using `plot()` function
```{r}
# Plotting simple features (sf) with plot
plot(df_simplified$geometry.x)
plot(df_simplified$geometry.y)
library(sp)
library(leaflet)
```
# Using ggplot2
Start by building the map area:
```{r}
multi_polygon_sf <- st_as_sf(df_simplified) #converting to sf object
outline_plot <-ggplot() +
geom_sf(data = multi_polygon_sf$geometry.y, fill = "lightblue", color = "blue") +
theme_minimal() +
labs(title = "Nonprofits and Neighborhood Demographics")
outline_plot
```
# Neighborhood Demographics
```{r}
# make data at neighborhood level to plot maps
neighborhoods_sf <- st_as_sf(neighborhoods) # convert to sf object
neighborhoods_sf <- neighborhoods_sf %>% mutate(Percent_AA
= round(Blk_AfAm/Population*100, digits = 2)) %>% # make our variables like before but this time at neighborhood level only
# create new Majority_AA variable that indicates if Percent_AA is greater than 50% or not %>%
mutate(Majority_AA = case_when(
Percent_AA > 50 ~ "Yes",
Percent_AA < 50 ~ "No")) %>%
# create a new variable about this in text
mutate(Neighborhood_type = case_when(
Percent_AA > 50 ~ "Majority\nBlack",
Percent_AA < 50 ~ "Majority\nNon-Black")) %>%
# make this a factor and order by level appearance in the data
mutate(Neighborhood_type = as_factor(Neighborhood_type),
Neighborhood_type = forcats::fct_inorder(Neighborhood_type))
neighborhood_dem <- ggplot() +geom_sf(data = neighborhoods_sf, aes(fill = Neighborhood_type))+
scale_fill_manual(values = c("Majority\nBlack" = "#482677FF", "Majority\nNon-Black" = "#20A387FF"))+
theme_minimal()
neighborhood_dem
```
# Map of High vs Low Asset Orgs
```{r}
asset_plot_high_Vs_low <- neighborhood_dem +
geom_sf(data = multi_polygon_sf, alpha = 0.8, shape = 21, color ="black", fill = "#DCE319FF")+ facet_grid(~ASSET_High_text)+
theme_void() +
labs(title = "High vs Low Asset Non-Profits in Baltimore")
asset_plot_high_Vs_low
```
# Map of different types of orgs by asset
```{r}
asset_plot_all_faceted <- neighborhood_dem +
geom_sf(data = multi_polygon_sf, alpha = 0.8, shape = 21, color ="black", fill = "#DCE319FF")+ facet_wrap(~NTEE_text, ncol = 3 )+
theme_void() +
labs(title = "Non-Profits in Baltimore by Type")
asset_plot_all_faceted
```
# High Asset Non-Profits in Baltimore by Type
```{r}
data_to_map <-multi_polygon_sf %>% filter(ASSET_High_text == "High Asset")
asset_plot <- neighborhood_dem +
geom_sf(data = data_to_map, alpha = 0.8, shape = 21, color ="black", fill = "#DCE319FF")+ facet_wrap(~NTEE_text, ncol = 3 )+
theme_void() +
labs(title = "High Asset Non-Profits in Baltimore by Type")
asset_plot
```
# Old work
```{r}
multi_polygon_sf <- st_as_sf(df_simplified)
AA_plot <-ggplot() +
geom_sf(data = multi_polygon_sf, aes(color = Majority_AA)) +
theme_minimal() +
labs(title = "Neighborhood by Demographics")
AA_plot
#+ facet_grid(~multi_polygon_sf$Majority_AA)+
```
```{r}
outline_plot +
geom_sf(data = multi_polygon_sf, aes(color = Majority_AA)) +
theme_void()
```
```{r}
outline_plot +
geom_sf(data = multi_polygon_sf, aes(color = Majority_AA))+
facet_grid(~ASSET_High_text)+
theme_void()
```
```{r}
points_plot <-outline_plot +
geom_sf(data = multi_polygon_sf$geometry.x, fill = "lightblue", color = "blue", alpha = 0.5) +
theme_minimal() +
labs(title = "plot example")
points_plot
```
```{r}
coords <- st_coordinates(df_simplified$geometry.x)
coords_df <- as.data.frame(coords)
colnames(coords_df) <- c("longitude", "latitude")
df_simplified <-bind_cols(coords_df, df_simplified)
#leaflet(coords_df) %>% addMarkers() %>% addTiles()
```
```{r}
df_simplified %>% filter(Majority_AA == "Yes") %>%
ggplot( aes(x = longitude, y = latitude, color = ASSET_High)) + geom_point()+
theme_minimal()
```
```{r}
df_simplified %>%
ggplot( aes(x = longitude, y = latitude)) +facet_grid(ASSET_High ~ Majority_AA) + geom_point()+
theme_minimal()
```
- keep high asset overall for online
-
#############old stuff:
```{r, include = FALSE}
#old
# geo_clean <- df_simplified %>% dplyr::select(ein, lon, lat) %>% drop_na(lon) # one row - will need to remove form cbos
# coordinates(geo_clean) <- ~lon+lat
# leaflet(geo_clean) %>% addMarkers() %>% addTiles()
```
```{r, eval = FALSE}
df_simplified <-bind_cols(coords_df, df_simplified)
library(ggmap)
library(osmdata)
library(devtools)
mad_map <- get_map(getbb("Limete, Kinshasa"), maptype = "terrain", source = "osm")
ggmap(mad_map)
```
```{r, eval = FALSE}
for_plot <-df_simplified %>% dplyr::select(Name, ORG_NAME_CURRENT, longitude, latitude)
# getting the map
mapplotarea <- get_map(location = c(lon = mean(for_plot$longitude), lat = mean(for_plot$latitude)), zoom = 11,maptype = "satellite", scale = 2)
# plotting the map with some points on it
plot1 <- ggmap(mapplotarea) +
geom_point(data =for_plot, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 2, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
```
```{r, eval = FALSE}
neighborhoods <-df_simplified$geometry.x
library(rnaturalearthdata)
library(rnaturalearth)
world <- ne_countries(scale = "medium", returnclass = "sf")
glimpse(world)
outline_plot<-ggplot(data = world) +
geom_sf() + geom_sf(data = neighborhoods)+
coord_sf(xlim = c(-76.74, -76.5), ylim = c(39.19, 39.4),
expand = FALSE)
outline <-plot(df_simplified$geometry.y)
multi_polygon_sf <- st_as_sf(df_simplified)
outline +geom_point(data = df_simplified, aes(x = longitude, y = latitude, fill = BMF_ASSET_CODE), size = 2,
shape = 23)
test <-outline + geom_point(data = df_simplified, aes(x = longitude, y = latitude))
```