Skip to content

Instantly share code, notes, and snippets.

@ryanbthomas
Created February 26, 2020 19:22
Show Gist options
  • Save ryanbthomas/9e6c6d8cf88f70de9227c4f4922fdc6c to your computer and use it in GitHub Desktop.
Save ryanbthomas/9e6c6d8cf88f70de9227c4f4922fdc6c to your computer and use it in GitHub Desktop.
---
title: "R Notebook"
output: html_notebook
---
```{r}
# setup
library(readr)
library(dplyr)
library(forcats)
library(ggplot2)
library(tibble)
```
```{r}
#load data
url <- "https://gist.githubusercontent.com/dgkeyes/a9b47cfa9a7bd8313e5603d18cf3d444/raw/3de0e4074db23636ebd420af78e90c4009aa10c7/oregon-economic-mobility-data.csv"
raw_data <- read_csv(url, col_names = c("name", "upward_mobile"), skip = 1)
```
```{r}
# clean data
extract_percentage <- function(x) {
sans_percent <- substr(x, 1, nchar(x) - 1)
as.numeric(sans_percent)
}
clean_data <- mutate(raw_data, upward_mobile = extract_percentage(upward_mobile))
state_mean <- mean(clean_data$upward_mobile)
county_data <- clean_data %>%
mutate(highlow = if_else(upward_mobile > state_mean, "High", "Low"),
simple_name = stringr::str_extract(string = name,
pattern = "^[A-Za-z ]+(?= County, OR)"))
state_data <- tibble(name = "Oregon Average",
upward_mobile = state_mean,
highlow = "",
simple_name = "Oregon Average")
plot_data <- bind_rows(county_data, state_data) %>%
arrange(upward_mobile) %>%
mutate(simple_name = forcats::fct_inorder(simple_name),
highlow = forcats::fct_inorder(highlow))
```
```{r, fig.height=10, fig.width=5}
# plot
vert_axis <- 7
use_palette <- c("#f5a52f", "#5c5c5c","#2f50f5")
ggplot(plot_data, aes(x = upward_mobile, y = simple_name)) +
geom_segment(aes(x = vert_axis, xend = upward_mobile, yend = simple_name, color = highlow), size = 1.5) +
geom_point(aes(color = highlow), size = 4) +
geom_text(aes(x = upward_mobile + 0.8, y = simple_name, label = scales::percent(upward_mobile/100))) +
scale_color_manual(values = use_palette) +
scale_x_continuous(limits = c(vert_axis, 16), expand = expand_scale()) +
labs(title = "Upward Mobility") +
theme_classic() +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.background = element_rect(fill = "#fffff0"),
panel.background = element_rect(fill = "#fffff0"),
axis.text.x = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks = element_blank(),
title = element_text(size = 20) )
```
@jkaupp
Copy link

jkaupp commented Feb 26, 2020

FYI, reader::parse_number() is a standard drop in for your extract_percentage()

@ryanbthomas
Copy link
Author

Thanks... I actually noted that in your gist. Going to remember that one for the future.

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