|
| 1 | +--- |
| 2 | +title: "maps" |
| 3 | +author: "Carrie" |
| 4 | +date: "2025-11-21" |
| 5 | +output: html_document |
| 6 | +--- |
| 7 | +# Maps |
| 8 | + |
| 9 | +```{r} |
| 10 | +
|
| 11 | +library(RColorBrewer) |
| 12 | +library(leaflet) |
| 13 | +library(terra) |
| 14 | +# for metadata/attributes- vectors or rasters |
| 15 | +library(raster) |
| 16 | +library(sf) |
| 17 | +library(rnaturalearthdata) |
| 18 | +library(rnaturalearth) |
| 19 | +library(tidyverse) |
| 20 | +
|
| 21 | +df_simplified <- read_rds(here::here("data/processed/formaps.rds")) |
| 22 | +neighborhoods <- read_rds(here::here("data/processed/neighborhood_shape_data.rds")) |
| 23 | +
|
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +```{r} |
| 28 | +# Plotting simple features (sf) with plot |
| 29 | +plot(df_simplified$geometry.x) |
| 30 | +plot(df_simplified$geometry.y) |
| 31 | +library(sp) |
| 32 | +library(leaflet) |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +ggplot2 |
| 38 | +```{r} |
| 39 | +multi_polygon_sf <- st_as_sf(df_simplified) |
| 40 | +outline_plot <-ggplot() + |
| 41 | + geom_sf(data = multi_polygon_sf$geometry.y, fill = "lightblue", color = "blue") + |
| 42 | + theme_minimal() + |
| 43 | + labs(title = "Nonprofits and Neighborhood Demographics") |
| 44 | +outline_plot |
| 45 | +
|
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +```{r} |
| 50 | +
|
| 51 | +# make data at neighborhood level to plot maps |
| 52 | +neighborhoods_sf <- st_as_sf(neighborhoods) |
| 53 | +neighborhoods_sf <- neighborhoods_sf %>% mutate(Percent_AA |
| 54 | + = round(Blk_AfAm/Population*100, digits = 2)) %>% |
| 55 | +
|
| 56 | + # create new Majority_AA variable that indicates if Percent_AA is greater than 50% or not %>% |
| 57 | + mutate(Majority_AA = case_when( |
| 58 | + Percent_AA > 50 ~ "Yes", |
| 59 | + Percent_AA < 50 ~ "No")) %>% |
| 60 | + # create a new variable about this in text |
| 61 | + mutate(Neighborhood_type = case_when( |
| 62 | + Percent_AA > 50 ~ "Majority\nBlack", |
| 63 | + Percent_AA < 50 ~ "Majority\nNon-Black")) %>% |
| 64 | + # make this a factor and order by level appearance in the data |
| 65 | + mutate(Neighborhood_type = as_factor(Neighborhood_type), |
| 66 | + Neighborhood_type = forcats::fct_inorder(Neighborhood_type)) |
| 67 | +
|
| 68 | +
|
| 69 | + neighborhood_dem <- ggplot() +geom_sf(data = neighborhoods_sf, aes(fill = Neighborhood_type))+ |
| 70 | + scale_fill_manual(values = c("Majority\nBlack" = "#482677FF", "Majority\nNon-Black" = "#20A387FF"))+ |
| 71 | + theme_minimal() |
| 72 | + neighborhood_dem |
| 73 | +
|
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +```{r} |
| 80 | +asset_plot_high_Vs_low <- neighborhood_dem + |
| 81 | + geom_sf(data = multi_polygon_sf, alpha = 0.8, shape = 21, color ="black", fill = "#DCE319FF")+ facet_grid(~ASSET_High_text)+ |
| 82 | + theme_void() + |
| 83 | + labs(title = "High vs Low Asset Non-Profits in Baltimore") |
| 84 | +
|
| 85 | +
|
| 86 | +asset_plot_high_Vs_low |
| 87 | +
|
| 88 | +``` |
| 89 | +```{r} |
| 90 | +
|
| 91 | +asset_plot_all_faceted <- neighborhood_dem + |
| 92 | + geom_sf(data = multi_polygon_sf, alpha = 0.8, shape = 21, color ="black", fill = "#DCE319FF")+ facet_wrap(~NTEE_text, ncol = 3 )+ |
| 93 | + theme_void() + |
| 94 | + labs(title = "Non-Profits in Baltimore by Type") |
| 95 | +asset_plot_all_faceted |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +```{r} |
| 100 | +data_to_map <-multi_polygon_sf %>% filter(ASSET_High_text == "High Asset") |
| 101 | +
|
| 102 | +asset_plot <- neighborhood_dem + |
| 103 | + geom_sf(data = data_to_map, alpha = 0.8, shape = 21, color ="black", fill = "#DCE319FF")+ facet_wrap(~NTEE_text, ncol = 3 )+ |
| 104 | + theme_void() + |
| 105 | + labs(title = "High Asset Non-Profits in Baltimore by Type") |
| 106 | +asset_plot |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +```{r} |
| 111 | +multi_polygon_sf <- st_as_sf(df_simplified) |
| 112 | +AA_plot <-ggplot() + |
| 113 | + geom_sf(data = multi_polygon_sf, aes(color = Majority_AA)) + |
| 114 | + theme_minimal() + |
| 115 | + labs(title = "Neighborhood by Demographics") |
| 116 | +AA_plot |
| 117 | +
|
| 118 | +#+ facet_grid(~multi_polygon_sf$Majority_AA)+ |
| 119 | +
|
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +```{r} |
| 124 | +outline_plot + |
| 125 | + geom_sf(data = multi_polygon_sf, aes(color = Majority_AA)) + |
| 126 | + theme_void() |
| 127 | +
|
| 128 | +
|
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +```{r} |
| 133 | +outline_plot + |
| 134 | + geom_sf(data = multi_polygon_sf, aes(color = Majority_AA))+ |
| 135 | + facet_grid(~ASSET_High_text)+ |
| 136 | + theme_void() |
| 137 | +
|
| 138 | +
|
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +```{r} |
| 145 | +
|
| 146 | +points_plot <-outline_plot + |
| 147 | + geom_sf(data = multi_polygon_sf$geometry.x, fill = "lightblue", color = "blue", alpha = 0.5) + |
| 148 | + theme_minimal() + |
| 149 | + labs(title = "plot example") |
| 150 | +points_plot |
| 151 | +
|
| 152 | +``` |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +```{r} |
| 160 | +coords <- st_coordinates(df_simplified$geometry.x) |
| 161 | +coords_df <- as.data.frame(coords) |
| 162 | +colnames(coords_df) <- c("longitude", "latitude") |
| 163 | +df_simplified <-bind_cols(coords_df, df_simplified) |
| 164 | +#leaflet(coords_df) %>% addMarkers() %>% addTiles() |
| 165 | +``` |
| 166 | + |
| 167 | +```{r} |
| 168 | +df_simplified %>% filter(Majority_AA == "Yes") %>% |
| 169 | +ggplot( aes(x = longitude, y = latitude, color = ASSET_High)) + geom_point()+ |
| 170 | + theme_minimal() |
| 171 | +``` |
| 172 | + |
| 173 | +```{r} |
| 174 | +df_simplified %>% |
| 175 | +ggplot( aes(x = longitude, y = latitude)) +facet_grid(ASSET_High ~ Majority_AA) + geom_point()+ |
| 176 | + theme_minimal() |
| 177 | +``` |
| 178 | + |
| 179 | +- keep high asset overall for online |
| 180 | +- |
| 181 | +#############old stuff: |
| 182 | +```{r, include = FALSE} |
| 183 | +#old |
| 184 | +# geo_clean <- df_simplified %>% dplyr::select(ein, lon, lat) %>% drop_na(lon) # one row - will need to remove form cbos |
| 185 | +# coordinates(geo_clean) <- ~lon+lat |
| 186 | +# leaflet(geo_clean) %>% addMarkers() %>% addTiles() |
| 187 | +
|
| 188 | +``` |
| 189 | + |
| 190 | +```{r, eval = FALSE} |
| 191 | +df_simplified <-bind_cols(coords_df, df_simplified) |
| 192 | +
|
| 193 | +library(ggmap) |
| 194 | +library(osmdata) |
| 195 | +library(devtools) |
| 196 | +
|
| 197 | +mad_map <- get_map(getbb("Limete, Kinshasa"), maptype = "terrain", source = "osm") |
| 198 | +ggmap(mad_map) |
| 199 | +``` |
| 200 | + |
| 201 | +```{r, eval = FALSE} |
| 202 | +
|
| 203 | +
|
| 204 | +
|
| 205 | +for_plot <-df_simplified %>% dplyr::select(Name, ORG_NAME_CURRENT, longitude, latitude) |
| 206 | +# getting the map |
| 207 | +mapplotarea <- get_map(location = c(lon = mean(for_plot$longitude), lat = mean(for_plot$latitude)), zoom = 11,maptype = "satellite", scale = 2) |
| 208 | +# plotting the map with some points on it |
| 209 | +plot1 <- ggmap(mapplotarea) + |
| 210 | + geom_point(data =for_plot, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 2, shape = 21) + |
| 211 | + guides(fill=FALSE, alpha=FALSE, size=FALSE) |
| 212 | +
|
| 213 | +
|
| 214 | +``` |
| 215 | + |
| 216 | +```{r, eval = FALSE} |
| 217 | +neighborhoods <-df_simplified$geometry.x |
| 218 | +library(rnaturalearthdata) |
| 219 | +library(rnaturalearth) |
| 220 | +world <- ne_countries(scale = "medium", returnclass = "sf") |
| 221 | +glimpse(world) |
| 222 | +outline_plot<-ggplot(data = world) + |
| 223 | + geom_sf() + geom_sf(data = neighborhoods)+ |
| 224 | + coord_sf(xlim = c(-76.74, -76.5), ylim = c(39.19, 39.4), |
| 225 | + expand = FALSE) |
| 226 | +
|
| 227 | +
|
| 228 | +outline <-plot(df_simplified$geometry.y) |
| 229 | +
|
| 230 | +
|
| 231 | +
|
| 232 | + multi_polygon_sf <- st_as_sf(df_simplified) |
| 233 | +
|
| 234 | +
|
| 235 | +
|
| 236 | +outline +geom_point(data = df_simplified, aes(x = longitude, y = latitude, fill = BMF_ASSET_CODE), size = 2, |
| 237 | + shape = 23) |
| 238 | +
|
| 239 | +test <-outline + geom_point(data = df_simplified, aes(x = longitude, y = latitude)) |
| 240 | +``` |
| 241 | + |
| 242 | + |
| 243 | + |
0 commit comments