Skip to content

Instantly share code, notes, and snippets.

@kzembower
Last active June 11, 2023 15:37
Show Gist options
  • Save kzembower/a8d774f5d2ab7bbb974fb4815e39b758 to your computer and use it in GitHub Desktop.
Save kzembower/a8d774f5d2ab7bbb974fb4815e39b758 to your computer and use it in GitHub Desktop.
Creating map of neighborhood
## This gives degrees lat long:
library(tidyverse)
library(tigris)
options(tigris_use_cache = TRUE)
library(sf)
library(OpenStreetMap)
lat_max <- 39.3525
long_max <- -76.617
lat_min <- 39.3455
long_min <- -76.6095
nw <- c(lat_max, long_max)
se <- c(lat_min, long_min)
rw_map <- openmap(nw, se,
type = "osm",
mergeTiles = TRUE) %>%
openproj() %>%
OpenStreetMap::autoplot.OpenStreetMap() +
xlab("long") + ylab("lat")
png("rw_map.png")
plot(rw_map)
dev.off()
@kzembower
Copy link
Author

Output:
rw_map

@kzembower
Copy link
Author

## This gives map with grid in meters:
library(tidyverse)
library(tidycensus)
library(sf)
library(tmap)
library(tigris)
options(tigris_use_cache = TRUE)
library(tmaptools)

rw_block_list <- c("3000", "3001", "3002", "3005", "3006", "3007",
                   "3008", "3009", "3010", "3011", "3012")

## Get the RW blocks from the census:
rw_blocks <- blocks(state = "MD",
                    county = "Baltimore city",
                    year = "2020") %>%
    filter(substr(GEOID20, 6, 11) == "271101" &
           substr(GEOID20, 12, 15) %in% rw_block_list)

## Create a map of just the RW blocks:
rw_base_blocks <- read_osm(bb(rw_blocks, ext = 1.3))

tmap_mode("plot")

(RW_block_map <- tm_shape(rw_base_blocks) +
     tm_rgb() +
     tm_shape(rw_blocks) +
     tm_fill("MAP_COLORS", alpha = 0.2, palette = "Accent", n = 10) +
     tm_borders() +
     tm_scale_bar() +
     tm_grid() + tm_xlab("Long") + tm_ylab("Lat") +
     tm_layout(title = "Radnor-Winston Neighborhood")
)

tmap_save(RW_block_map, "rw_map_meters.png")

rw_map_meters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment