Last active
January 4, 2019 17:06
-
-
Save matt-dray/23313cd903136dd149994cbc31c3f981 to your computer and use it in GitHub Desktop.
Testing the alluvial function for creating Sankey-like static plots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# demo of alluvial::alluvial() | |
# see the vignette: https://cran.r-project.org/web/packages/alluvial/vignettes/alluvial.html | |
# matt dray | |
# july 2018 | |
# packages | |
library(dplyr) | |
library(readr) | |
library(alluvial) | |
# get data | |
download.file( | |
url = "https://raw.githubusercontent.com/mwdray/datasets/master/pokemon_go_captures.csv", | |
destfile = "~/Desktop/pokemon_go_captures.csv" # where to save it to | |
) | |
# read data | |
pkmn <- read_csv(file = "~/Desktop/pokemon_go_captures.csv") | |
# select, filter and summarise | |
pkmn_wt_ht <- pkmn %>% | |
filter(species %in% c("pidgey", "rattata", "drowzee")) %>% | |
select(species, weight_bin, height_bin) %>% | |
group_by(species, weight_bin, height_bin) %>% | |
summarise(count = n()) %>% | |
ungroup() | |
# create alluvial plot | |
alluvial( | |
pkmn_wt_ht[, 1:3], # exclude the count column from the plot | |
freq = pkmn_wt_ht$count, # column containing frequencies | |
col = ifelse( | |
test = pkmn_wt_ht$weight_bin == "extra_small", | |
yes = "blue", # colour where test is met | |
no = "grey" | |
), | |
layer = pkmn_wt_ht$weight_bin %in% c("extra_large", "normal") # order of layers | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment