Skip to content

Instantly share code, notes, and snippets.

## Create NYT Spiral Animations
## Most Code comes from here: https://bydata.github.io/nyt-corona-spiral-chart/
library(tidyverse)
library(lubridate)
library(gganimate)
library(viridis)
owid_url <- "https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.csv?raw=true"
### Code mostly inspired by https://github.com/HudsonJamie/tidy_tuesday/blob/main/2021/week_46/afrilearndata.R
library(pdftools)
library(tidygeocoder)
library(tidyverse)
library(janitor)
library(rgdal)
library(osmdata)
library(sf)
library(ggfx)
library(sf)
library(cowplot)
library(showtext)
library(sysfonts)
library(ggplot2)
showtext_auto()
font_add_google("Quicksand")
font_add_google("Fira Sans")
font1 <- "Quicksand"
### The following functions borrow heavily from this gist by @jnolis
### https://gist.github.com/jnolis/a41c196a5e22e2a2115d28e853d4780c
### Unfortunately, the shiny app framework didn't work for me
### So I created an interactive console script that guides you through authentification
### No shiny needed
# This function creates a URL for users to click to authenticate.
# You should use it to show a URL when users haven't authenticated yet.
get_authorization_url <- function(app, permission=NULL){
@favstats
favstats / collapseoutput.js
Last active July 13, 2021 09:40 — forked from emitanaka/collapseoutput.js
Collapsible Code Output for `xaringan`
<script>
(function() {
var divHTML = document.querySelectorAll(".details-open");
divHTML.forEach(function (el) {
var preNodes = el.getElementsByTagName("pre");
var outputNode = preNodes[1];
outputNode.outerHTML = "<details open class='output'><summary>Run</summary>" + outputNode.outerHTML + "</details>";
})
})();
(function() {
mutate(type = case_when(
type == "Regio" ~ "Local Politics",
type == "Rest" ~ "Party Youth Organization/Dutch Politicians (Other)",
type == "Kandidaat" ~ "Dutch Politician",
type == "Landelijk" ~ "Dutch National Party"
))
paadc_ids_2021_01_22 <- old_data%>%
# distinct(paadc) %>%
mutate(impressionTime = lubridate::ymd_hms(impressionTime)) %>%
group_by(paadc) %>%
filter(impressionTime == min(impressionTime)) %>%
ungroup() %>%
distinct(paadc, .keep_all = T) %>%
arrange(impressionTime) %>%
mutate(firstImpressionTime = as.character(impressionTime)) %>%
select(paadc, firstImpressionTime) %>%
library(tidyverse)
library(ggbeeswarm)
library(ggthemes)
library(glue)
elex <- read_csv("https://raw.githubusercontent.com/favstats/USElection2020-NYT-Results/master/data/latest/presidential.csv")
elex %>%
mutate(votes_perc = votes/sum(votes)) %>%
mutate(abs_margin2020 = abs(margin2020)) %>%
@favstats
favstats / measurement_invariance.R
Last active February 21, 2020 00:48
Function performs multiple group analysis to check for measurement invariance
library(tidyverse)
library(lavaan)
measurement_invariance <- function(model, data, group, fit_measures) {
## Configural Model
model1 <- cfa(model = model, data = data, group = group)
## Weak/metric invariance
model2 <- cfa(model = model, data = data, group = group, group.equal=c("loadings"))
## Strong/scalar invariance
# Install these packages if you don't have them yet
# if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, egg, gridExtra, grid, patchwork)
## Function to fix panel width and add "buffers" for right-alignment
## based on: https://gist.github.com/baptiste/eadeecd7d3756a7f294eb3cade8b0b17
facet_cleaner <- function(p, panel_size = 5, panel_height = 1) {
p %>%
egg::set_panel_size(