Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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.
#################################################