Skip to content

Instantly share code, notes, and snippets.

View k5cents's full-sized avatar

Kiernan Nicholls k5cents

View GitHub Profile
@k5cents
k5cents / FiveThirtyPlot.R
Created March 1, 2019 01:28
A custom ggplot2 theme to mimick the FiveThirtyEight graphics style
library(tidyverse)
theme_538 <- function() {
theme(
plot.title = element_text(face = "bold",
family = "Helvetica",
size = 18,
color = "#222222",
lineheight = 0.5,
vjust = 1),
plot.subtitle = element_text(family = "Helvetica",
@k5cents
k5cents / past_npvic.R
Last active May 29, 2019 21:04
Past NPVIC Attempts
library(tidyverse)
library(janitor)
library(rvest)
npvic_wiki <- read_html("https://en.wikipedia.org/wiki/National_Popular_Vote_Interstate_Compact")
enacted <- npvic_wiki %>%
html_node("table.wikitable:nth-child(71)") %>%
html_table() %>%
as_tibble(.name_repair = make_clean_names) %>%
@k5cents
k5cents / uk_coalpower.R
Last active May 28, 2019 00:26 — forked from cavedave/coalpower.r
Visualizing UK Coal Usage (Simplified Code)
# this script automates the collection, processing, and plotting of gridwatch energy data
# 2019-05-27
# @kiernann
# u/WannabeWonk
# install.packages("pacman")
pacman::p_load(
RSelenium,
readr,
dplyr,
@k5cents
k5cents / hello_internet_code.R
Created June 19, 2019 18:05
Extracting the second letter from every ninth word in the Hello Internet (Last Jedi) Christmas Special
pacman::p_load(tidyverse, tidytext)
read_html("http://podcastsearch.david-smith.org/episodes/2003") %>%
html_nodes("p") %>%
html_text() %>%
str_squish() %>%
str_trim() %>%
str_remove("◼ ► ") %>%
enframe(NULL, "line_text") %>%
rownames_to_column("line_no") %>%
unnest_tokens(word, "line_text") %>%
@k5cents
k5cents / tale_two_cands.R
Created July 15, 2019 14:42
Comparing the performance of Elizabeth Warren and Beto O'Rourke
pacman::p_load(
tidyverse,
lubridate,
snakecase,
here,
glue,
fs
)
mid <- 3633
library(eurostat) # eurostat data
library(rnaturalearth) # worldwide map data
library(tidyverse) # tidy data transformation
library(lubridate) # date and time support
library(sf) # simple features GIS
euro_pop <-
get_eurostat('demo_r_pjanaggr3', stringsAsFactors = FALSE) %>%
filter(
str_length(geo) == 5, # NUTS-3
@k5cents
k5cents / material-pal.R
Created October 2, 2019 20:27
R function to return material design color palettes.
mdp <- structure(
list(
color = rep(
x = c(
"amber", "blue", "brown", "cyan", "deepOrange", "deepPurple",
"green", "grey", "indigo", "lightBlue", "lightGreen", "lime",
"orange", "pink", "purple", "red", "teal", "yellow"
),
each = 10
),
@k5cents
k5cents / partisan_indexing.R
Created October 14, 2019 21:29
Comparing the states by partisanship and other development metrics
library(tidyverse)
library(janitor)
library(magrittr)
library(campfin)
library(rvest)
url_edu <- "https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_educational_attainment"
url_life <- "https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_life_expectancy"
url_gdp <- "https://en.wikipedia.org/wiki/List_of_U.S._states_by_GDP_per_capita"
url_hdi <- "https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_Human_Development_Index"
@k5cents
k5cents / separate-right-to-left.R
Created October 24, 2019 16:26
Trying to separate() a tibble right-to-left
library(tibble)
library(tidyr)
library(glue)
data <- tribble(
~ address,
"8548 Mammoth Drive, Sumter, SC, 29150",
"9725 Spring St., Apt 1, Piscataway, NJ, 08854",
"Apple co., 75 Applegate Ave., Encino, CA, 91316",
"26 Sherwood Ave., Reading, MA, 01867",
@k5cents
k5cents / emoji-codes.R
Created November 11, 2019 21:42
Code to replace emoji with markdown short code
library(jsonlite)
library(tidyverse)
library(magrittr)
url <- "https://gist.githubusercontent.com/oliveratgithub/0bf11a9aff0d6da7b46f1490f86a71eb/raw/ac8dde8a374066bcbcf44a8296fc0522c7392244/emojis.json"
shortcodes <-
fromJSON(url) %>%
use_series("emojis") %>%
as_tibble() %>%
na_if("") %>%