Skip to content

Instantly share code, notes, and snippets.

View scbrown86's full-sized avatar

Stuart Brown scbrown86

View GitHub Profile
@scbrown86
scbrown86 / alignTwoGGPlots.R
Created February 8, 2018 04:40
Aligning two ggplots when one has a legend
## Function to align two ggplot objects when 1 has a legend and one doesn't
## Copied from https://stackoverflow.com/a/30414008/1710632 on 2018-02-08
AlignPlots <- function(...) {
LegendWidth <- function(x) x$grobs[[8]]$grobs[[1]]$widths[[4]]
plots.grobs <- lapply(list(...), ggplotGrob)
max.widths <- do.call(unit.pmax, lapply(plots.grobs, "[[", "widths"))
plots.grobs.eq.widths <- lapply(plots.grobs, function(x) {
@scbrown86
scbrown86 / diverge0.R
Created January 28, 2018 00:47 — forked from scipionesarlo/diverge0.R
Plot a rasterVis::levelplot with a colour ramp diverging around zero
diverge0 <- function(p, ramp) {
# p: a trellis object resulting from rasterVis::levelplot
# ramp: the name of an RColorBrewer palette (as character), a character
# vector of colour names to interpolate, or a colorRampPalette.
require(RColorBrewer)
require(rasterVis)
if(length(ramp)==1 && is.character(ramp) && ramp %in%
row.names(brewer.pal.info)) {
ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp)))
} else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) {
@scbrown86
scbrown86 / stratified.R
Last active February 16, 2021 15:29 — forked from mrdwab/stratified.R
Stratified random sampling from a `data.frame` in R
stratified <- function(df, group, size, select = NULL,
replace = TRUE, bothSets = FALSE) {
if (is.null(select)) {
df <- df
} else {
if (is.null(names(select))) stop("'select' must be a named list")
if (!all(names(select) %in% names(df)))
stop("Please verify your 'select' argument")
temp <- sapply(names(select),
function(x) df[[x]] %in% select[[x]])
@scbrown86
scbrown86 / ColoredQuant_ggplot2.R
Created August 30, 2017 04:00 — forked from Rekyt/ColoredQuant_ggplot2.R
Color Quantile from a density in ggplot2 with labeled quantiles
# Script from a density to color quantiles area
# Packages ---------------------------------------------------------------------
library(ggplot2)
library(dplyr)
# Data -------------------------------------------------------------------------
r_data = rnorm(100) # Computes 100 values
r_quant = quantile(r_data, probs = seq(0, 1, 0.1)) # Computes quantiles
@scbrown86
scbrown86 / diverge0.R
Created May 22, 2017 06:04 — forked from johnbaums/diverge0.R
Plot a rasterVis::levelplot with a colour ramp diverging around zero
diverge0 <- function(p, ramp) {
# p: a trellis object resulting from rasterVis::levelplot
# ramp: the name of an RColorBrewer palette (as character), a character
# vector of colour names to interpolate, or a colorRampPalette.
require(RColorBrewer)
require(rasterVis)
if(length(ramp)==1 && is.character(ramp) && ramp %in%
row.names(brewer.pal.info)) {
ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp)))
} else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) {