library(tidyverse)
library(sf) # fix annoying tidyverse tibble issue with sfc_MULTIPOLYGON/sfc
library(clipr)
import tables from our cleaned data. We’ll need both our county-only table and all-terminations
all_terminations <- read_rds("data-processed/02-all-terminations.rds") ## table including out of state and country rows
all_terminations
county_terminations <- read_rds("data-processed/01-county-terminations.rds")
county_terminations
Let’s take a look at our age demographics for terminations
terminations_by_age <- all_terminations %>%
select(
-(asian:total_terminations) #get rid of age columns
) %>%
pivot_longer(
cols = under_15_years:age_not_stated, # select columns to pivot
names_to = "age", # new column name
values_to = "terminations"
) %>%
mutate(
age = str_replace_all(age, "_", " "), # fix value names to be human-readable
age = str_replace(age, "x", "")
) %>%
group_by(year, age) %>% # group by race
summarise(
total_terminations = sum(terminations) # add all the termination counts together
)
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
terminations_by_age
Let’s look at the age demographic data for terminations using a grouped bar chart (position=position_dodge() in ggplot)
ggplot(terminations_by_age, aes(x=year, y=total_terminations, fill=age)) +
geom_bar(stat="identity", position=position_dodge())
To make a grouped bar chart for datawrapper, we need to have this data in a slightly different format to work there
terminations_by_age_datawrapper <- terminations_by_age %>%
filter(
age != "under 15 years", age != "age not stated" # exclude these two since they're insignificant for chart
) %>%
pivot_wider(
names_from = age, values_from = total_terminations
)
terminations_by_age_datawrapper %>% write_clip(allow_non_interactive = T) # copy to clipboard
Since under 15 and age not stated are hard to see in the chart on datawrapper, calculate the amounts so we can note them in the chart and remove them
terminations_by_age %>%
group_by(age) %>%
summarise(total_terminations = sum(total_terminations))
Link to datawrapper chart here: Abortions by age
terminations_by_age %>%
filter(
year == "2020"
) %>%
arrange(
total_terminations %>% desc()
)