Skip to content

Instantly share code, notes, and snippets.

View clauswilke's full-sized avatar

Claus Wilke clauswilke

View GitHub Profile
@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 / 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",
library(purrr)
library(ggplot2)
# function that does the work
make_partial_plots <- function(p) {
g <- ggplot_build(p)
n <- length(g$plot$layers)
blank_out <- function(i) {
g1 <- g
library(rlang)
# Functions with names ending in `_impl` take quoted expressions as input.
# This removes the need for constant quoting and unquoting
simplify_sum_impl <- function(e1, e2) {
if (is_syntactic_literal(e1) & is_syntactic_literal(e2)) {
return(eval_bare(e1 + e2))
}
if (e1 == 0) {
@clauswilke
clauswilke / internet.R
Last active February 17, 2018 05:58
Internet users over time; plot variations
library(tidyverse) # code uses development version of ggplot2 for viridis scales; otherwise, viridis package would be needed
df_internet <- read.csv("internet.csv")
# sort countries by internet users in final year
internet_summary <- df_internet %>%
group_by(country) %>%
summarize(last = users[n()]) %>%
arrange(last)
@clauswilke
clauswilke / Internet user per 100.csv
Last active February 13, 2018 19:52
Internet adoption over time
We can make this file beautiful and searchable if this error is corrected: It looks like row 9 should actually have 23 columns, instead of 14 in line 8.
country,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011
Abkhazia,,,,,,,,,,,,,,,,,,,,,,
Afghanistan,0,,,,,,,,,,,0.004188346,0.004092114,0.079875078,0.097163516,1.130397829,1.947423469,1.751202161,1.688485448,3.246305573,3.654114396,4.580669921
Akrotiri and Dhekelia,,,,,,,,,,,,,,,,,,,,,,
Albania,0,,,,,0.011168695,0.032196828,0.048593919,0.06502737,0.081437045,0.114097347,0.325798377,0.390081273,0.971900415,2.420387798,6.043890864,9.609991316,15.03611541,23.86,41.2,45,49
Algeria,0,,,,0.000360674,0.001768954,0.001738533,0.010268463,0.020238555,0.199523843,0.491705679,0.646114017,1.59164126,2.195359731,4.634475088,5.843942092,7.375984956,9.451190626,10.18,11.23,12.5,14
American Samoa,0,,,,,,,,,,,,,,,,,,,,,
Andorra,0,,,,,,1.526601023,3.050175385,6.886209218,7.635686143,10.53883561,,11.26046872,13.54641288,26.83795439,37.60576622,48.936847,70.87,70.04,78.53,81,81
Angola,0,,,,,,0.000775929,0.005673746,0.018453724,0.071964087,0.105045562,0.136013867,0.27037
@clauswilke
clauswilke / Aus_athletes_cheeseplot.R
Last active June 24, 2018 17:21
Cheese plot of height and bodyfat % of Australian athletes
# original version
library(DAAG)
library(ggplot2)
library(ggridges)
ggplot(ais, aes(x=ht, y=sport, point_size=pcBfat, point_color=sex, group=sport)) +
geom_density_ridges(jittered_points=TRUE, scale = .8, rel_min_height = .01, fill = "gray90",
points_scaling_range = c(.1, .8)) +
scale_y_discrete(expand = c(.01, 0)) +
scale_x_continuous(expand = c(0, 0), name = "height [cm]") +
scale_point_size_continuous(range = c(0.1, 5), name = "% bodyfat") +