Skip to content

Instantly share code, notes, and snippets.

View ctesta01's full-sized avatar
🌱

Christian Testa ctesta01

🌱
View GitHub Profile
@ctesta01
ctesta01 / black_history_milestones.R
Last active August 21, 2026 16:46
Black History Milestones Scraped from History.com and Visualized
library(ggplot2)
library(magrittr)
library(rvest)
library(dplyr)
# url to fetch html from
html_data <- "https://www.history.com/topics/black-history/black-history-milestones"
# parse html
html_text <- read_html(html_data)
@ctesta01
ctesta01 / dorling_style_covid_cases_and_deaths.R
Last active January 27, 2022 16:47
Plot US COVID Cases and Deaths in the "SLOWDOWN" (or phase-plane) style
library(tidyverse)
library(magrittr)
library(geomtextpath)
df_usa <- readr::read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us.csv")
ma <- function(x, n = 31){stats::filter(x, rep(1 / n, n), sides = 2)}
df_usa %<>% mutate(
deaths_avg = as.numeric(ma(deaths_avg)),
@ctesta01
ctesta01 / covid_audio_wave.R
Created January 25, 2022 17:34
Generate an audio-wave of COVID-19 deaths in the United States with an animated wave form visualization
library(tuneR)
library(dplyr)
library(magrittr)
library(ggplot2)
library(gganimate)
# advice from https://stackoverflow.com/questions/23310005/permission-denied-when-playing-wav-file
# you may not need this (or need something else) for other operating systems
setWavPlayer('/usr/bin/afplay')

Converting Categorical Variables to Dummy Variables and Vice-Versa

This document introduces a pair of functions which should be intuitive to tidyverse users and standardizes the process for converting categorical columns of data back and forth from a set of dummy variables (columns of TRUE/FALSE indicators for each of the categorical levels).

library(tidyverse)
@ctesta01
ctesta01 / covid_cases_polar_coordinates.R
Last active January 14, 2022 21:47
Plot the NYTimes moving average of cases in polar coordinates
library(tidyverse)
library(magrittr)
library(lubridate)
df <- readr::read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us.csv")
library(urbnmapr)
library(plotly)
library(sf)
# Sometimes when we have spatial polygons (sf objects) and rendering plots with
# them is going unexpectedly slow, it's because the objects are defined in quite
# high resolution. This can be great if you want to render a high resolution map,
# but sometimes we don't need that much resolution and would like to simplify
# the polygon shapes to make rendering visualizations faster.
# animate a saddle differential equation system
#
# dX = -x - y
# dY = x - y
# dZ = .25 * z
#
# 25 trajectories are simulated and animated -- rendered using rgl in R
#
# Read more about rgl: https://dmurdoch.github.io/rgl/
library(plotly)
library(sf)
library(dplyr)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
N <- 10
# make random data
df <- lapply(1:N, function(x) nc) %>% bind_rows %>%
@ctesta01
ctesta01 / nyt_7day_avg.R
Last active October 26, 2021 15:06
Reconstructing the 7-day moving average function NYT uses in their COVID-19 rolling averages data which omits negative counts
library(tidyverse)
library(magrittr)
# New York Times style 7-day moving average
#
# Use each value from the given vector and the prior 6 values
# to compute a moving average where values less than 0 are dropped
# both from the sum in the numerator and from the count of days
# in the denominator.
#
@ctesta01
ctesta01 / check_out_new_2020_census_tracts.R
Created August 13, 2021 13:52
check_out_new_2020_census_tracts.R
install.packages("tigris")
library(tigris)
library(sp)
fulton20 <- tracts(state = 'GA', county = c('Fulton'), year = 2020)
fulton10 <- tracts(state = 'GA', county = c('Fulton'), year = 2010)
fulton00 <- tracts(state = 'GA', county = c('Fulton'), year = 2000)
library(leaflet)