Skip to content

Instantly share code, notes, and snippets.

View scbrown86's full-sized avatar

Stuart Brown scbrown86

View GitHub Profile
library(tidyverse)
library(scales)
data(diamonds)
diamonds %>%
filter(str_detect(cut, "Fair|Ideal")) %>%
ggplot(aes(price, carat)) +
geom_point(color = "skyblue", alpha = 0.5) +
facet_wrap(~cut, strip.position = "bottom") +
scale_x_continuous(labels = comma) +
# 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) {
@scbrown86
scbrown86 / polygonizer.R
Last active February 16, 2021 15:29 — forked from Pakillo/polygonizer.R
Convert raster to polygon using GDAL
# Convert raster to polygon using GDAL
## edited 2019-03-27 to use sf::st_read
polygonise <- function(x, outshape=NULL, gdalformat = 'ESRI Shapefile',
pypath=NULL, readpoly=TRUE, quietish=TRUE) {
## x: an R Raster layer, or the file path to a raster file recognised by GDAL
## outshape: the path to the output shapefile (if NULL, a temporary file will be created)
## gdalformat: the desired OGR vector format. Currently only supports ESRI Shapefile!
## pypath: the path to gdal_polygonize.py (if NULL, an attempt will be made to determine the location
## readpoly: should the polygon shapefile be read back into R, and returned by this function? (logical)
## quietish: should (some) messages be suppressed? (logical)
@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())) {