Recreate USAFACTS chart:Who spends more of their income on housing in the US?
# Clear workspace:
rm(list = ls())
# Setwd
setwd("D:/0 - My documents/TOOLS/R/Visual Capitalist/United States")
# Import csv file
library(rio)
renter <- import("D:/0 - My documents/TOOLS/R/Visual Capitalist/United States/data-8U6Pg.csv")
# Rename
names(renter) <- c("state","percent")
library(tidyverse)
renter <- renter %>%
filter(!state %in% c("Alaska", "Hawaii"))
renter$region <- tolower(renter$state)
renter$region <- trimws(renter$region)
library(stringi)
renter$percent <- stri_replace_all_regex(renter$percent, "%", "")
renter$percent <- as.numeric(renter$percent)
# Get state data
states <- map_data("state")
# merge and sort (plots in order, sort ensures states filled in)
renter.geo <- states %>%
left_join(renter, by = "region")# Plot
g1 <- ggplot(renter.geo, aes(long, lat)) +
geom_polygon(aes(group = group, fill = percent),
color = "white", linewidth = 0.1) +
coord_map()+
labs(title = "In 2022, Florida, Nevada, and California had the largest shares of housing-\nburdened renters.",
subtitle = "Percentage of renter households that spend 30% or more of their income on housing, 2022",
caption = "Source: Census Bureau")+
# Adjust background
theme(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(),
panel.grid = element_blank(),
plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(fill = NA),
panel.border = element_blank())+
theme(legend.title = element_blank())+
theme(plot.title.position = "plot")+
theme(plot.caption.position = "plot")+
# Adjust fill color and legend format
scale_fill_gradientn(colours = hcl.colors(5, "ag_Sunset", rev = TRUE),
guide = guide_colourbar(direction = "horizontal",
barheight = unit(2, units = "mm"),
barwidth = unit(30, units = "mm"),
title.hjust = 0.5,
label.hjust = 0.5),
na.value = "grey90")+
# Adjust legend position
theme(legend.position = c(0.1,0.1))+
# Adjust plot margin
theme(plot.margin = unit(c(0.2, 0.5, 0.2, 0.2), "cm"))+
# Adjust title, sub, caption
theme(plot.title = element_text(color = "grey20", size = 11, face = "bold")) +
theme(plot.subtitle = element_text(size = 10, color = "gray40")) +
theme(plot.caption = element_text(size = 8, colour = "grey40", face = "italic"))+
theme(legend.text = element_text(size = 8, colour = "grey40"))+
geom_text(label = "Florida has the most\nhousing-burdened renters.",
colour = "grey40",
x = -78,
y = 28,
hjust = 0,
size = 2)+
geom_text(label = "South Dakota has the least\nhousing-burdened renters.",
colour = "grey40",
x = -92,
y = 49,
hjust = 0,
size = 2)+
geom_segment(x = -92, y = 49,
xend = -100, yend = 45,
color = "grey40",
linewidth = 0.1,
arrow = arrow(length = unit(0.1, "cm")))+
geom_segment(x = -79, y = 28,
xend = -80, yend = 28,
color = "grey40",
linewidth = 0.1,
arrow = arrow(length = unit(0.1, "cm")))
ggsave("renter.png", width = 6, height = 4,dpi = 300,units = c("in"))# Clear workspace:
rm(list = ls())
# Setwd
setwd("D:/0 - My documents/TOOLS/R/Visual Capitalist/United States")
# Import csv file
library(rio)
owner <- import("D:/0 - My documents/TOOLS/R/Visual Capitalist/United States/data-el5Yq.csv")
# Rename
names(owner) <- c("state","percent")
library(tidyverse)
owner <- owner %>%
filter(!state %in% c("Alaska", "Hawaii"))
owner$region <- tolower(owner$state)
owner$region <- trimws(owner$region)
library(stringi)
owner$percent <- stri_replace_all_regex(owner$percent, "%", "")
owner$percent <- as.numeric(owner$percent)
# Get state data
states <- map_data("state")
# merge and sort (plots in order, sort ensures states filled in)
owner.geo <- states %>%
left_join(owner, by = "region")# Plot
g2 <- ggplot(owner.geo, aes(long, lat)) +
geom_polygon(aes(group = group, fill = percent),
color = "white", linewidth = 0.1) +
coord_map()+
labs(title = "In 2022, California, Hawaii, New Jersey and New York had the largest shares\nof housing-burdened homeowners.",
subtitle = "Percentage of homeowner households that spend 30% or more of their income on housing",
caption = "Source: Census Bureau")+
# Adjust background
theme(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(),
panel.grid = element_blank(),
plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(fill = NA),
panel.border = element_blank())+
theme(legend.title = element_blank())+
theme(plot.title.position = "plot")+
theme(plot.caption.position = "plot")+
# Adjust fill color and legend format
scale_fill_gradientn(colours = hcl.colors(5, "BluYl", rev = TRUE),
guide = guide_colourbar(direction = "horizontal",
barheight = unit(2, units = "mm"),
barwidth = unit(30, units = "mm"),
title.hjust = 0.5,
label.hjust = 0.5),
na.value = "grey90")+
# Adjust legend position
theme(legend.position = c(0.1,0.1))+
# Adjust plot margin
theme(plot.margin = unit(c(0.2, 0.5, 0.2, 0.2), "cm"))+
# Adjust title, sub, caption
theme(plot.title = element_text(color = "grey20", size = 11, face = "bold")) +
theme(plot.subtitle = element_text(size = 10, color = "gray40")) +
theme(plot.caption = element_text(size = 8, colour = "grey40", face = "italic"))+
theme(legend.text = element_text(size = 8, colour = "grey40"))+
geom_text(label = "West Virginia has the least\nhousing-burdened\nhomeowners.",
colour = "grey40",
x = -82,
y = 47,
hjust = 0,
size = 2)+
geom_text(label = "California has the most housing-\nburdened homeowners.",
colour = "grey40",
x = -122,
y = 30,
hjust = 0,
size = 2)+
geom_segment(x = -79, y = 45,
xend = -81, yend = 39,
color = "grey40",
linewidth = 0.1,
arrow = arrow(length = unit(0.1, "cm")))+
geom_segment(x = -118, y = 31.5,
xend = -118, yend = 33,
color = "grey40",
linewidth = 0.1,
arrow = arrow(length = unit(0.1, "cm")))