Skip to content

Instantly share code, notes, and snippets.

@wpetry
wpetry / DemogDarkCorners.R
Created April 11, 2017 20:55
COM(P)ADRE blog code
#################################################
## 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.
#################################################
@wpetry
wpetry / PipeLigatures.Rmd
Created September 27, 2017 20:41
Working towards ASCII characters that can be ligated for magrittr pipe
# dplyr pipe ligature ideas:
## %>% forward-pipe operator
˙>.
## %T>% tee operator
˙T>.
## %<>% compound assignment pipe-operator
˙◇.
@wpetry
wpetry / format_pval.R
Created January 23, 2018 10:57
Format p-values for Rmarkdown publications
#################################################
## 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)
@wpetry
wpetry / theme_ms.R
Last active August 21, 2020 07:32
ggplot2 theme for publication figures
library(ggplot2)
library(grid)
# define consistent ggplot theme to apply to all figures
theme_ms <- function(base_size=12, base_family="Helvetica") {
library(grid)
(theme_bw(base_size = base_size, base_family = base_family)+
theme(text=element_text(color="black"),
axis.title=element_text(face="bold", size = rel(1.3)),
axis.text=element_text(size = rel(1), color = "black"),
legend.title=element_text(face="bold"),
@wpetry
wpetry / water_year_tools.R
Created February 22, 2018 04:15
Takes a date and calculates: the water year, the week of the water year, the day of the water year
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
# 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) {
@wpetry
wpetry / rCompos.R
Last active April 9, 2018 07:21
Generate random compositions that sum to <=1
## 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)
#################################################-
## Evaluate color palettes for colorblindness accessibility ----
## W.K. Petry
#################################################-
## Preliminaries ----
#################################################-
library(colorspace)
library(colorscience)
library(paletteer) # devtools::install_github("EmilHvitfeldt/paletteer")
library(tidyverse)
@wpetry
wpetry / get_pkg_versions.R
Last active July 5, 2018 11:42
function that reports the names and version numbers of packages in an R workspace
#' 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
@wpetry
wpetry / plot.windrose.R
Created July 28, 2018 12:25
plot histogram of wind speed and direction on polar coordinates
# 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,