Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created September 21, 2022 15:10
Show Gist options
  • Save walkerke/3d1788533595d5989a879c3686fa5155 to your computer and use it in GitHub Desktop.
Save walkerke/3d1788533595d5989a879c3686fa5155 to your computer and use it in GitHub Desktop.
cities <- structure(
list(
name = c(
"Mesa",
"Phoenix",
"Tucson",
"Fresno",
"Los Angeles",
"Sacramento",
"San Diego",
"San Francisco",
"San Jose",
"Denver",
"Washington",
"Jacksonville",
"Chicago",
"Indianapolis",
"Louisville",
"Baltimore",
"Boston",
"Detroit",
"Kansas City",
"Las Vegas",
"Albuquerque",
"New York",
"Charlotte",
"Columbus",
"Oklahoma City",
"Portland",
"Philadelphia",
"Memphis",
"Nashville",
"Austin",
"Dallas",
"El Paso",
"Fort Worth",
"Houston",
"San Antonio",
"Seattle",
"Milwaukee"
),
pctchange = c(
15.15,
17.89,
7.07,
14.09,
3.16,
17.91,
14.34,
13.37,
10.84,
30.5,
30.08,
24.21,
-0.2,
15.29,
14.74,
-5.26,
25.65,
-24.34,
15.32,
20.07,
15.25,
6.43,
46.23,
30.74,
33.34,
25.04,
12.08,
-2.2,
29.88,
42.09,
12.53,
16.28,
55.56,
17.8,
20.76,
36.68,
2.22
)
),
row.names = c(NA,-37L),
class = c("tbl_df",
"tbl", "data.frame")
)
# Not great; we can make this better!
ggplot(cities, aes(x = pctchange, y = name)) +
geom_col()
cities$type <- ifelse(cities$pctchange > 0,
"growth", "decline")
gg <- ggplot(cities,
aes(x = pctchange,
y = reorder(name,
pctchange),
color = type,
fill = type)) +
geom_col(width = 0.01)
gg <- gg + geom_point(size = 3) +
theme_minimal()
gg <- gg + theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = "none")
gg <- gg + scale_x_continuous(labels = scales::label_percent(scale = 1)) +
scale_color_manual(values = c("red", "#90b4d2"))
gg <- gg + labs(x = "Percent change between 2005 and 2021",
y = "",
title = "Municipal population change, 2005-2021",
subtitle = "Cities with populations above 500,000",
caption = "Data acquired with the R tidycensus package. Chart by @kyle_e_walker.")
gg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment