Skip to content

Instantly share code, notes, and snippets.

@k-barton
k-barton / bwplot.R
Last active November 20, 2020 11:04
A low-level, customizable box- or dot-whiskers plot, using base graphics.
bwplot <-
function(x, y, lower, upper, box.lower, box.upper, w = .25, boxw = 0.8,
names = NULL, xlab = NULL, ylab = NULL, main = NULL,
lty = par("lty"), lty.stap = lty, lty.vert = lty, lty.box = lty,
lwd = par("lwd"),
border = par("fg"),
col.box = "white", col.pt = par("fg"),
xlim, ylim, add = FALSE, ann = !add, axes = TRUE,
pch = par("pch"), cex = par("cex"), bg = par("bg")) {
@k-barton
k-barton / checkers.R
Last active November 20, 2020 11:05
Add chequerboard background to the plot (with base graphics)
#' @param nx,ny number of checkers horizontally and vertically
#' @param size size in inches, alternative way to specify number of checkers.
#' Ignored if `nx` or `ny` are given.
#' @param ratio numeric scalar, x/y ratio of checker side
#' @adj adjust numeric of length 1 or 2, adjusts horizontal and vertical alignment
#' of the checkerboard. Preferably within 0-1 range.
#' @param col1,col2 first and second colour for the checkers
#' @param add logical, if `TRUE` (the default) draws the checkerboard over the existing plot
checkers <-
function(nx = NULL, ny = NULL, size = NULL, ratio = 1,
@k-barton
k-barton / connector-line.R
Last active November 20, 2020 11:05
Draw a connecting line to a label, not overlapping the label text (with base graphics).
# draws a line from point `x`, `y` to a text `label` located at `ly`, `ly`,
#' without overlapping the text area.
#' @example
#' plot(c(-1, 1), c(-1, 1), type = "n", ann = FALSE)
#' label.x <- .666
#' label.y <- .123
#' lab <- "multiline\nlabel"
#' cex <- 1.2345
#' a <- seq(0, pi, length.out = 15)
#' connector.line(sin(a), cos(a), label.x, label.y, lab, cex = cex, pad = c(.02, .02))
@k-barton
k-barton / mapscale.R
Last active November 20, 2020 11:07
Add a versatile map scale onto the plot (with base graphics).
mapscale <-
function(mapunit, unitname = "km", pos = c("bottom", "left", "right", "top"),
width = 0.15,
style = c("line", "bar", "double"),
height = 0, n = 5,
fine.part = NULL, lab.below = FALSE,
cex = par("cex.axis") * par("cex"),
line.lab = 0.8, offx = 0.25, offy = offx,
tcl = par("tcl"), extreme.ticks = FALSE,
double.bar = FALSE,
@k-barton
k-barton / subplot.R
Last active February 13, 2021 02:35
Create a subplot within the current plotting region (using base graphics).
#' @param expr an \R expression to produce the plot content.
#' @param fig numeric of length 1 or 2. The proportion of the plotting region to
#' be taken as the subplot plotting region (horizontal, vertical).
#' @param pos subplot position as a keyword, including "top", "bottom", "left",
#' "right". Empty string for centered plot.
#' @param inset numeric of length 1 or 2, giving inset distance(s) from the
#' margins in inches.
subplot <-
function(expr, fig = .33, pos = "topleft", inset = c(0, 0)) {
@k-barton
k-barton / bugs_helper.R
Last active April 17, 2019 22:46
JAGS/BUGS helper functions (define model as R code, execute JAGS code in R /simulate/, etc.)
### BUGS/JAGS helper functions
### Example usage:
#
## Define JAGS model (currently only `model` section
#mocode <- bugsmodel({
# alpha ~ dunif(0, 1)
# for(i in 1:10) {
# beta[i] ~ dunif(0, 1)
# }
# },
@k-barton
k-barton / remove-google-tracking.user.js
Last active March 7, 2021 13:50
Greasemonkey user script: Remove Google's link tracking
// ==UserScript==
// @name Remove Google's link tracking
// @description Removes onmousedown event from external Google links to allow direct links
// @namespace https://gist.github.com/k-barton
// @include http://*.google.*
// @include https://*.google.*
// @version 0.2.1
// @grant unsafeWindow
// @run-at document-end
// ==/UserScript==
@k-barton
k-barton / remove-fb-tracking.user.js
Last active September 27, 2023 16:58
Greasemonkey user script: Remove Facebook's external link tracking
// ==UserScript==
// @name Remove Facebook's external link tracking
// @description Removes redirection and the "click identifier" from external links on FBs
// @namespace https://gist.github.com/k-barton
// @match https://*.facebook.com/*
// @version 0.3.7
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==
(function() {