Skip to content

Instantly share code, notes, and snippets.

@gorkang
Created January 29, 2022 11:38
Show Gist options
  • Save gorkang/81a74060a1fd380a46e2d1ddfc16105b to your computer and use it in GitHub Desktop.
Save gorkang/81a74060a1fd380a46e2d1ddfc16105b to your computer and use it in GitHub Desktop.
happiness USA 1972-2021
# Data from: https://gssdataexplorer.norc.org/trends
# Inspiration: https://twitter.com/_cingraham/status/1487094406159110147
library(tidyverse)
library(readxl)
library(geomtextpath)
FILES = list.files(path = "~/Downloads/", pattern = "happy", full.names = TRUE)
DF_raw = map_df(FILES, read_excel, skip = 11)
DF = DF_raw %>% drop_na() %>% select(-...1) %>%
mutate(file = c("Very happy", "Pretty happy", "Not too happy")) %>%
select(file, everything()) %>%
pivot_longer(matches("[0-9]"), names_to = "year") %>%
mutate(value = gsub("(.*) \n.*", "\\1", value) %>% as.numeric() / 100)
PLOT = DF %>%
ggplot(aes(x = year, value, label = file, group = file, color = file)) +
geom_point() +
geom_smooth(alpha = .1, color = "darkgrey") +
geom_labelpath(text_smoothing = 30) +
theme_minimal() +
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, max(DF$value)), n.breaks = 10) +
ggthemes::theme_fivethirtyeight() +
theme(legend.position = "none") +
labs(title = "Life Satisfaction USA",
subtitle = "General happiness (1972-2021)",
caption = " \nBy @gorkang \n \nData: https://gssdataexplorer.norc.org/ -> Gender & Marriage -> Life Satisfaction")
PLOT
ggsave("~/Downloads/happiness.png", PLOT, width = 16, height = 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment