Skip to content

Instantly share code, notes, and snippets.

View clauswilke's full-sized avatar

Claus Wilke clauswilke

View GitHub Profile
---
title: "World maps"
output:
html_document:
df_print: paged
---
```{r echo = FALSE, message = FALSE}
library(tidyverse)
library(sf)
@clauswilke
clauswilke / normal.R
Created March 7, 2022 03:41
Deterministically generated normal variates
# Deterministically generated normal variates, implemented here in R but more
# useful for GLSL programming.
library(ggplot2)
fract <- function(x) x - floor(x)
rand <- function(x) fract(3482954.234 * sin(24934.342 * x + 12394.32))
norm <- function(x) {
@clauswilke
clauswilke / GLSL-Noise.md
Created January 16, 2022 22:56 — forked from patriciogonzalezvivo/GLSL-Noise.md
GLSL Noise Algorithms

Generic 1,2,3 Noise

float rand(float n){return fract(sin(n) * 43758.5453123);}

float noise(float p){
	float fl = floor(p);
  float fc = fract(p);
	return mix(rand(fl), rand(fl + 1.0), fc);
}
@clauswilke
clauswilke / animate_labels.R
Created May 15, 2018 14:25
rotate plot labels
library(ggplot2) # requires 2.3.0
library(purrr)
make_plot <- function(frame) {
ggplot(mtcars, aes(mpg, hp, color = factor(cyl))) +
geom_point() +
scale_color_brewer(
palette = 2, type = "qual", name = "cyl",
guide = guide_legend(
direction = "horizontal",
@clauswilke
clauswilke / correlation_matrix.R
Last active May 12, 2020 14:11
Tidyverse approach to calculating and plotting correlation matrices
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
library(colorspace)
# data to analyze
data <- select(MASS::fgl, -type, -RI, -Si)
# cluster
@clauswilke
clauswilke / CFR.Rmd
Created March 17, 2020 02:57
COVID-19 Case Fatality Rate (CFR) by age
---
title: "COVID-19 Case Fatality Rate (CFR) by age"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
library(sf)
library(dplyr)
library(ggplot2)
library(gganimate) # needs development version from github
# helper function to place a geometric object at a desired position
# and scale
place_geometry <- function(geometry, position, scale = 1) {
(geometry - st_centroid(geometry)) * scale +
st_sfc(st_point(position))
# Code for presentation at rstudio::conf2019
# Slides:
# https://docs.google.com/presentation/d/1zMuBSADaxdFnosOPWJNA10DaxGEheW6gDxqEPYAuado/edit?usp=sharing
# Setup -------------------------------------------------------------------
library(ggplot2)
library(gganimate)
library(ungeviz)
library(mgcv)
@clauswilke
clauswilke / contour_lines.cpp
Last active December 27, 2018 21:50
Contour lines and bands example for R, written in Rcpp
// This file implements the 2D isoline and isoband algorithms described
// here: https://en.wikipedia.org/wiki/Marching_squares
// Written by Claus O. Wilke
#include <Rcpp.h>
using namespace Rcpp;
#include <iostream>
#include <vector>
#include <utility> // for pair
library(tidyverse)
library(sf)
# Development version of colorspace is needed. Install via:
# install.packages("colorspace", repos = "http://R-Forge.R-project.org")
library(colorspace)
crs_goode <- "+proj=igh"
world_goode <- st_as_sf(rworldmap::getMap(resolution = "low")) %>%