if (FALSE) {
library(tidycensus)
library(ggplot2)
# Identify variables for mapping
race_vars <- c(
Hispanic = "P2_002N",
White = "P2_005N",
Black = "P2_006N",
Asian = "P2_008N"
)
# Get data from tidycensus
baltimore_race <- get_decennial(
geography = "tract",
variables = race_vars,
state = "MD",
county = "Baltimore city",
geometry = TRUE,
year = 2020
)
# Convert data to dots
baltimore_dots <- as_dot_density(
baltimore_race,
value = "value",
values_per_dot = 100,
group = "variable"
)
# Use one set of polygon geometries as a base layer
baltimore_base <- baltimore_race[baltimore_race$variable == "Hispanic", ]
# Map with ggplot2
ggplot() +
geom_sf(data = baltimore_base,
fill = "white",
color = "grey") +
geom_sf(data = baltimore_dots,
aes(color = variable),
size = 0.01) +
theme_void()
}
Run the code above in your browser using DataLab