Skip to content

Instantly share code, notes, and snippets.

View mdlincoln's full-sized avatar

Matthew Lincoln mdlincoln

View GitHub Profile
year nationality artists_strict
1550 Flemish 67
1550 Dutch 38
1551 Flemish 66
1551 Dutch 38
1552 Flemish 70
1552 Dutch 37
1553 Flemish 72
1553 Dutch 37
1554 Flemish 76
@mdlincoln
mdlincoln / yday_months.R
Created September 30, 2015 14:21
ggplot2 scale that displays the months of the year for data that has only the day of the year as an integer
yday_months_x <- scale_x_discrete(breaks = cumsum(c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)), labels = month.name)
@mdlincoln
mdlincoln / replace_each.R
Last active October 2, 2015 19:48
Replace values in a vector based on a named vector dictionary
#' Replace all matching values
#'
#' @param v Vector whose values are to be replaced
#' @param pairs A named vector of replacements
#' @export
#' @examples
#' x <- c("a", "a", "b", "c")
#' p <- c("a" = "alpha", "b" = "beta")
#' replace_each(x, p)
replace_each <- function(v, pairs) {
msg <- function(..., prob = 0.25) {
if (runif(1) > prob) {
return(invisible())
}
messages <- c(...)
message(sample(messages, 1))
}
encourage <- function() {
@mdlincoln
mdlincoln / rotate_labels.R
Created October 31, 2015 19:15
Add this snippet to rotate x-axis labels by 45º in ggplot2
theme(axis.text.x = element_text(angle = 45, hjust = 1))
@mdlincoln
mdlincoln / mogrify.sh
Last active November 4, 2015 19:48
imageMagick convert any image to 300x300 DPI
convert image -density 300 -units pixelsperinch outimage
<?
/////////////////////
// slack2html
// by @levelsio
/////////////////////
//
/////////////////////
// WHAT DOES THIS DO?
/////////////////////
//
# Friends don't let friends use
# two spaces after a period
sed -e 's/\. /. /g'
@mdlincoln
mdlincoln / europop.R
Last active December 18, 2015 14:26
Create an animated GIF of European urban population 1500-1800
library(devtools)
install_github("mdlincoln/europop")
library(europop)
library(dplyr)
library(rgdal)
library(ggplot2)
library(animation)
@mdlincoln
mdlincoln / range_simulation.R
Last active December 18, 2015 16:33
Animated GIF of simulation replications
library(ggplot2)
library(dplyr)
library(animation)
load(url("http://matthewlincoln.net/assets/docs/sim_data.rda"))
saveGIF({
# We want one frame per replication
for(i in 1:max(bm_raw_sims$r)) {