Skip to content

Instantly share code, notes, and snippets.

View schochastics's full-sized avatar
👨‍🍼

David Schoch schochastics

👨‍🍼
View GitHub Profile
@schochastics
schochastics / ggpyramid.R
Created May 30, 2019 14:15
ugly 3D pyramid plot
pyramid <- function(ymax=5,mcol="#CD3333",xoffset=0){
f <- function(a,b,y){
(y-b)/a
}
a1 <- ymax/0.65
b1 <- 0
c1 <- -ymax/(1-0.65)
d1 <- -c1
a2 <- -ymax/(1-0.65)
@schochastics
schochastics / lissajous.R
Last active October 22, 2019 19:47
animated lissajous curves
lissajou <- function(a,b,delta){
t <- seq(0,2*pi,0.01)
x <- sin(a*t+delta)
y <- sin(b*t)
tibble(x,y)
}
# setup parameters ----
n <- 7
params <- tibble(a=sample(1:10,n,replace = FALSE),
@schochastics
schochastics / time_prediction.R
Created November 12, 2019 18:56
time prediction
library(tidyverse)
library(ggforce)
d <- tibble(x = c(1, 6, 10, 23,18), xpred = c(4, 8, 11, 2,22), y = 1:5, id = factor(1:5))
n <- nrow(d)+1
time_angle <- tibble(hour=1:24,angle=0.25*60*hour)
d$start <- time_angle$angle[d$x]*pi/180
d$end <- time_angle$angle[d$xpred]*pi/180
d$r <- d$y
@schochastics
schochastics / prime_viz.R
Last active August 20, 2020 10:14
Visualization of Prime Factorization
# Recreation of https://twitter.com/stevenstrogatz/status/1295915404574040065/photo/1
library(tidyverse)
library(Polychrome)
fun <- function(x){
n <- c()
i <- 2
r <- x
while(prod(n)!=x){
if(!r%%i) {n=c(n,i);r=r/i;i=1}
i <- i+1
@schochastics
schochastics / layout_yearly.R
Created March 29, 2021 15:02
use constrained stress
library(igraph)
library(ggraph)
library(graphlayouts)
#handdrawn graph without isolates
el <- matrix(c(1,3,1,3,1,6,2,3,2,6,
3,4,3,4,3,6,4,6,4,8,
5,7,5,8,7,8,7,9,7,10,
7,11,9,10,10,11,9,11),ncol = 2,byrow = TRUE)
g <- graph_from_edgelist(el,F)
@schochastics
schochastics / get_tweets.R
Last active September 22, 2021 08:40
Get tweets from the Academic Research product track.
# start_time: %Y-%m-%dT%H:%M:%SZ
# end_time: %Y-%m-%dT%H:%M:%SZ
# needs jsonlite and httr
# next_token can be obtained from meta$next_token to paginate through results
get_tweets <- function(q="",n=10,start_time,end_time,token,next_token=""){
if(n>500){
warning("n too big. Using 500 instead")
n <- 500
}
if(n<5){
@schochastics
schochastics / pack_circles.cpp
Last active January 16, 2022 07:34
Reimplementation of @ijeamaka_a randomized circle packing R code with Rcpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double distance(double x1, double x2, double y1, double y2){
double dist = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
return dist;
}
// [[Rcpp::export]]
@schochastics
schochastics / age_vs_value.R
Created August 24, 2018 21:21
scrape mean age and market values for European Football leagues
library(tidyverse)
library(rvest)
library(ggimage)
library(lubridate)
#get first 25 leagues in Europe ----
url <- "https://www.transfermarkt.de/wettbewerbe/europa"
doc <- read_html(url)
leagues <- doc %>% html_nodes(".hauptlink a") %>% html_attr("href")
@schochastics
schochastics / cran.lua
Last active July 15, 2022 21:48
automatic styling and linking for R packages mentioned in quarto docs
function CRAN(handle)
local output = '<span class="cran-pkg"><a href="https://cran.r-project.org/package=' ..
pandoc.utils.stringify(handle) .. '">'..
pandoc.utils.stringify(handle)..'</a><i class="fa-brands fa-r-project fa-2xs"></i></span>'
return pandoc.RawBlock('html', output)
end
@schochastics
schochastics / weather_spain.R
Created August 12, 2022 13:17
ggraph circlepack on maps
library(tidyverse)
library(sf)
library(ggraph)
library(igraph)
# create some random data
country <- mapSpain::esp_get_prov(moveCAN=TRUE)
provinces <- st_cast(country,"MULTIPOLYGON") |> st_cast("POLYGON")
centroids <- distinct(provinces,iso2.prov.name.es,.keep_all = TRUE) |>
dplyr::filter(!iso2.prov.name.es%in%c("Baleares","Las Palmas","Santa Cruz de Tenerife","Ceuta","Melilla")) |>