Skip to content

Instantly share code, notes, and snippets.

View kjhealy's full-sized avatar

Kieran Healy kjhealy

View GitHub Profile
@kjhealy
kjhealy / position_nudge.r
Created August 11, 2022 16:43
Using position_nudge() to make the comparison to a background distribution a little nicer.
## Using position_nudge() to make showing a background
## distribution a little nicer, by pushing it very slightly
## to the right. Most of the work is getting the penguins
## data to the point where I can demonstrate position_nudge()
library(tidyverse)
library(palmerpenguins)
# Classify every penguin's flipper length into
# bins with widths of 10mm; then sum up
```{r}
library(tidyverse)
library(here)
library(fs)
library(socviz)
```
```{r}
here()
```
library(tidyverse)
library(babynames)
babynames %>%
filter(sex == "M") %>%
mutate(endletter = str_sub(name, -1)) %>%
group_by(year, endletter) %>%
summarize(letter_count = n()) %>%
mutate(letter_prop = letter_count / sum(letter_count),
letter_rc = case_when(
@kjhealy
kjhealy / collapse_mask.R
Created February 15, 2022 03:13 — forked from grantmcdermott/collapse_mask.R
Benchmarking collapse_mask
## Context: https://twitter.com/grant_mcdermott/status/1493400952878952448
options(collapse_mask = "all") # NB: see `help('collapse-options')`
library(dplyr)
library(data.table)
library(collapse) # Needs to come after library(dplyr) for collapse_mask to work
flights = fread('https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv')
@kjhealy
kjhealy / pnc.r
Last active September 25, 2021 03:29
library(tidyverse)
library(cowplot)
df <- read_csv("https://osf.io/qmhgw/download")
out <- df %>%
ggplot(mapping = aes(x = year,
y = oneper,
group = country,
color = Region)) +
# UK map v. quick example
## Libraries
library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
## Get the map data
@kjhealy
kjhealy / ffmpeg.md
Created March 2, 2021 03:16 — forked from dvlden/ffmpeg.md
Convert video files to MP4 through FFMPEG

This is my personal list of functions that I wrote for converting mov files to mp4!

Command Flags

Flag Options Description
-codec:a libfaac, libfdk_aac, libvorbis Audio Codec
-quality best, good, realtime Video Quality
-b:a 128k, 192k, 256k, 320k Audio Bitrate
-codec:v mpeg4, libx264, libvpx-vp9 Video Codec
library(tidyverse)
library(gapminder)
library(ggalt) ## install with install.packages("ggalt", repos = "http://cran.rstudio.com")
## Make some sample data with three countries that each has nine measures
example_wide <- gapminder %>%
select(country, year, lifeExp, pop, gdpPercap) %>%
filter(country %in% c("United States", "Canada", "Mexico"),
year %in% c(1957, 1977, 2007)) %>%
pivot_wider(names_from = year, values_from = lifeExp:gdpPercap)
@kjhealy
kjhealy / apple-covid.R
Last active May 27, 2020 15:54
Get Apple's COVID data in R
## Get current Apple COVID data
## Kieran Healy / @kjhealy
## E.g. apple_covid <- get_apple_data()
## 1. Find today's URL
## Thanks to David Cabo (@dcabo) for alerting me to the existence of the json file.
get_apple_target <- function(cdn_url = "https://covid19-static.cdn-apple.com",
json_file = "covid19-mobility-data/current/v3/index.json") {
tf <- tempfile(fileext = ".json")
library(dplyr, warn.conflicts = FALSE)
library(gapminder)
probs <- c(0.1, 0.5, 0.9)
gapminder %>%
group_by(continent) %>%
summarise(
probs = probs,
across(is.numeric & !year, ~ quantile(.x, probs))
)