Skip to content

Instantly share code, notes, and snippets.

View clauswilke's full-sized avatar

Claus Wilke clauswilke

View GitHub Profile
@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") +
@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 / 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)
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) {
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
@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
library(ggplot2)
library(multiscales)
colors <- scales::colour_ramp(
colors = c(red = "#AC202F", purple = "#740280", blue = "#2265A3")
)((0:7)/7)
types <- c(
'Natural state geometry',
'Cartogram weigted by electoral votes'
@clauswilke
clauswilke / 2018-06_cleaned.csv
Created July 29, 2018 15:13
Visualizing a high-dimensional macroeconomics time series via PCA
We can't make this file beautiful and searchable because it's too large.
sasdate,RPI,W875RX1,DPCERA3M086SBEA,CMRMTSPLx,RETAILx,INDPRO,IPFPNSS,IPFINAL,IPCONGD,IPDCONGD,IPNCONGD,IPBUSEQ,IPMAT,IPDMAT,IPNMAT,IPMANSICS,IPB51222S,IPFUELS,CUMFNS,HWI,HWIURATIO,CLF16OV,CE16OV,UNRATE,UEMPMEAN,UEMPLT5,UEMP5TO14,UEMP15OV,UEMP15T26,UEMP27OV,CLAIMSx,PAYEMS,USGOOD,CES1021000001,USCONS,MANEMP,DMANEMP,NDMANEMP,SRVPRD,USTPU,USWTRADE,USTRADE,USFIRE,USGOVT,CES0600000007,AWOTMAN,AWHMAN,HOUST,HOUSTNE,HOUSTMW,HOUSTS,HOUSTW,PERMIT,PERMITNE,PERMITMW,PERMITS,PERMITW,AMDMNOx,AMDMUOx,BUSINVx,ISRATIOx,M1SL,M2SL,M2REAL,AMBSL,TOTRESNS,NONBORRES,BUSLOANS,REALLN,NONREVSL,CONSPI,S&P 500,S&P: indust,S&P div yield,S&P PE ratio,FEDFUNDS,CP3Mx,TB3MS,TB6MS,GS1,GS5,GS10,AAA,BAA,COMPAPFFx,TB3SMFFM,TB6SMFFM,T1YFFM,T5YFFM,T10YFFM,AAAFFM,BAAFFM,EXSZUSx,EXJPUSx,EXUSUKx,EXCAUSx,WPSFD49207,WPSFD49502,WPSID61,WPSID62,OILPRICEx,PPICMM,CPIAUCSL,CPIAPPSL,CPITRNSL,CPIMEDSL,CUSR0000SAC,CUSR0000SAD,CUSR0000SAS,CPIULFSL,CUSR0000SA0L2,CUSR0000SA0L5,PCEPI,DDURRG3M086SBEA,DNDGRG3M086SBEA,DSERRG3M086SBEA,CES0600000008,CES2000000008,CES300
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))