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
################################################# | |
## Illuminating the dark corners of demography | |
## W.K. Petry | |
## | |
## README: This code reproduces the analyses shown on the COM(P)ADRE blog. The analyses were run | |
## at a spatial resolution of 1/12° (>9.3 million cells to cover the Earth!), and will take several hours | |
## to run the necessary calculations on a standard desktop/laptop. It's recommended that the code be run | |
## at a coarser spatial resolution first, increasing the resolution only when accuracy is needed. See the | |
## 'res' parameter under the 'Preliminaries' heading. | |
################################################# |
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
# dplyr pipe ligature ideas: | |
## %>% forward-pipe operator | |
˙>. | |
## %T>% tee operator | |
˙T>. | |
## %<>% compound assignment pipe-operator | |
˙◇. |
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
################################################# | |
## Format p-values for use in Rmarkdown documents | |
## This function modifies base::format.pval() to automate | |
## the inclusion of equalities and inequalities | |
## Author: W.K. Petry | |
################################################# | |
# Arguments: | |
# pv a numeric vector | |
# digits how many significant digits to use; recommended value is 3 | |
# eps numerical tolerance; effectively the smallest p-value to report; recommended value is 10^(-digits) |
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
library(lubridate) | |
wtr_yr <- function(dates, start_month = 9) { | |
# Convert dates into POSIXlt | |
dates.posix <- as.POSIXlt(dates) | |
# Year offset | |
offset <- ifelse(dates.posix$mon >= start_month - 1, 1, 0) | |
# Water year | |
adj.year <- dates.posix$year + 1900 + offset | |
# Return the water year |
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
# somewhat hackish solution to: | |
# https://twitter.com/EamonCaddigan/status/646759751242620928 | |
# based mostly on copy/pasting from ggplot2 geom_violin source: | |
# https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r | |
library(ggplot2) | |
library(dplyr) | |
"%||%" <- function(a, b) { |
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
## Generate random compositions that sum to <=1 | |
library(gtools) | |
library(ggtern) | |
library(scatterplot3d) | |
rCompos <- function(n, subsamples, alphas = NULL){ | |
if(is.null(alphas)) alphas <- rep(1, subsamples) | |
sums <- matrix(runif(n), ncol = 1) | |
dirichlet <- gtools::rdirichlet(n, alphas) |
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
#################################################- | |
## Evaluate color palettes for colorblindness accessibility ---- | |
## W.K. Petry | |
#################################################- | |
## Preliminaries ---- | |
#################################################- | |
library(colorspace) | |
library(colorscience) | |
library(paletteer) # devtools::install_github("EmilHvitfeldt/paletteer") | |
library(tidyverse) |
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
#' Retrieve attached or loaded package names and their version numbers | |
#' | |
#' @param L an object of class 'sessionInfo'. Default is to retrieve the current | |
#' workspace session information. | |
#' @param n character specifying whether to use attached packages ('otherPkgs') or | |
#' packages only loaded via a namespace ('loadedOnly'). Base packages are always | |
#' omitted. | |
#' | |
#' @return a data frame with two columns: the package name and the package version number. | |
#' @export |
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
# WindRose.R | |
# modified from https://stackoverflow.com/questions/17266780/wind-rose-with-ggplot-r | |
require(ggplot2) | |
require(RColorBrewer) | |
require(dplyr) | |
require(tidyr) | |
plot.windrose <- function(data, | |
spd, | |
dir, |
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
#################################################- | |
## Subdivide multipolygon by regional multipolygon with sf ---- | |
## W.K. Petry | |
#################################################- | |
## Preliminaries ---- | |
#################################################- | |
library(sf) | |
library(tidyverse) | |
library(cowplot) |
OlderNewer