Skip to content

Instantly share code, notes, and snippets.

## GOAL:
## re-create a figure similar to Fig. 2 in Wilson et al. (2018),
## Nature 554: 183-188. Available from:
## https://www.nature.com/articles/nature25479#s1
##
## combines a boxplot (or violin) with the raw data, by splitting each
## category location in two (box on left, raw data on right)
## call required packages
# 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) {
@benmarwick
benmarwick / gist:d0789be8014bb8dd079dd8c57cc587ea
Created April 12, 2024 05:54
Get data from the SOTA api to plot how many activations have been logged in each region of an association
library(rvest)
library(httr)
library(tidyverse)
# get list of regions in the W7W association
# via API
sota_regions_w7w <- "https://api2.sota.org.uk/api/associations/w7w"
r <- GET(sota_regions_w7w)
dat <-
jsonlite::fromJSON(content(r, as = "text")) %>%
@benmarwick
benmarwick / super-and-sub-script-labels.R
Last active April 11, 2024 23:09
ggplot axis labels with superscript and subscript
library(tidyverse)
# using expression() for the text formatting:
ggplot(mtcars,
aes(disp,
mpg)) +
geom_point() +
# ~ for spaces, and * for no-space between (unquoted) expressions
ylab(expression(Anthropogenic~SO[4]^{"2-"}~(ngm^-3))) +
xlab(expression(italic(delta)^13*C[ap]*"‰")) +
@benmarwick
benmarwick / rotate-axis-labels-ggplot2.R
Last active March 30, 2024 08:00
I can never remember how to rotate the x-axis labels with ggplot2: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
# Adapted from https://stackoverflow.com/a/7267364/1036500 by Andrie de Vries
# This is it: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
library(ggplot2)
td <- expand.grid(
hjust=c(0, 0.5, 1),
vjust=c(0, 0.5, 1),
angle=c(0, 45, 90),
@benmarwick
benmarwick / 000_geom_bag.r
Last active March 28, 2024 10:52
Basic bagplot geom for ggplot2
#' Bagplot
#'
#' The bag geom is useful for graphical summaries of scatterplots. It
#' is effective at showing the location, spread, skewness, and
#' outliers of a data set.
#'
#' A bagplot is a bivariate generalization of the well known boxplot. It
#' was proposed by Rousseeuw, Ruts, and Tukey. This geom plots bagplots that
#' are very similar to the one described in Rousseeuw et al. and
#' uses code from their bagplot functions in the aplpack pacakge.
@benmarwick
benmarwick / computing-sota-az-boundaries.R
Last active March 25, 2024 07:52
Compute polygons for SOTA activation zone boundaries for the W7W Association
library(raster)
library(terra)
library(tidyterra)
library(ggplot2)
library(sf)
library(tidyverse)
library(ggrepel)
library(ggspatial)
# get all summits via the GeoJSON download
@benmarwick
benmarwick / GCMSAgilentDfileImport
Last active March 22, 2024 16:57
Function to import Agilent GCMS Chemstation D files in R
##' Function readDFile
##'
##' Function readDFile
##' @param pathname the pathname of the directory containing the data to import
##' @return outData Output is a matrix of ion counts with rows as scantime and
##' columns as mass, and the respective values as labels
##' @export
readDFile<-function(pathname){
filename<-file.path(pathname,'DATA.MS')
@benmarwick
benmarwick / magnetic-susceptibility-data.R
Last active February 8, 2024 23:23
Exploring the magnetic susceptibility data from Boomplas
library(tidyverse) # you may need to install.packages("tidyverse") in the console
library(googlesheets4) # ditto
# change to your own UW email
gs4_auth("bmarwick@uw.edu")
# Get our google sheet of information about the location of each point
ms_data <-
read_sheet("https://docs.google.com/spreadsheets/d/1rDyfJQ6OnWJktIWMZfnLe8YRtCiW1PVrP9nKW1kERN4/edit?pli=1#gid=0")
@benmarwick
benmarwick / inspect-rmd-diff-from-last-commit.R
Last active February 8, 2024 21:17
GitHub doesn't show rich diffs for Rmd files. That can make collaborative writing tough. Here's how to see rich diffs of two commits of a single R Markdown document on a GitHub repo or local Git repo
# another method
# remotes::install_github("ropenscilabs/reviewer")
browseURL(reviewer::diff_rmd("analysis/paper/paper.qmd",
# this gets the sha of the previous commit
git2r::commits(n=2)[[2]]$sha)$raw)