Skip to content

Instantly share code, notes, and snippets.

View maptracker's full-sized avatar

Charles Tilford maptracker

  • Work-related, non-proprietary coding
View GitHub Profile
@maptracker
maptracker / WorldHeat.r
Last active November 14, 2017 17:42 — forked from cavedave/WorldHeat.r
Heatmap of world Temperature in r package for ggplot2
### FORKED FROM: https://gist.github.com/cavedave/8ff22e94c882e9f6be933f99f2ae0b50
## Charles changed:
## Absolute URLs to library/data (rather than local files)
## Raster rather than tiles (didn't like the grid lines)
## Gradient from red (hot) to blue (cold) with black at zero (no change from average)
## Removed year filter
## Defined title and breaks via automatic extraction of dates from data
## Scaled down plot to be more friendly for Gist
## Took out unused libraries
@maptracker
maptracker / RefClassHeadaches.R
Last active October 17, 2016 18:00
Unexpected behavior with R reference class objects
## It's a real pain to deal with deep-linking to Gists (eg sourcing raw code), so I'm working on this
## in GitHub proper:
## https://github.com/maptracker/RandomR/blob/master/RefClassHeadaches.R
## Quickly source this code:
## source("https://gist.github.com/maptracker/00f2c18383e844c3f4a0886b7897ceb7/raw/532410cf19b4df6f9949bdc9ec1dd0e9d13ef898/RefClassHeadaches.R")
# Fork of Scott's coin flip simulation
# https://gist.github.com/maptracker/e34a8596a9c2a7f261d6b892e5df42c5
flipCoin <- function( n = 50, pvals = 0.5, nosim = 100, file = NULL ) {
coverage <- lapply(pvals, function(p) {
phats <- (rbinom(nosim, prob = p, size = n) + 2)/(n + 4)
ll <- phats - qnorm(0.975) * sqrt(phats * (1 - phats)/n)
ul <- phats + qnorm(0.975) * sqrt(phats * (1 - phats)/n)
cbind(lower = ll, upper = ul)
})[[1]] # De-listify
@maptracker
maptracker / StudentListToGitHubWiki.R
Last active February 24, 2016 15:45
First pass at getting #random reviewer/reviewee assignments
## Generate a markdown table of permuted peer assignments
## Designed for putting into output into GitHub Wiki
## Permutation code
source("https://gist.github.com/maptracker/f0ec01bed4d1c1583bf6/raw/a9cbd9983703a293584e826d903f21a8556e41a3/StudentPeerReview.R")
makeMarkdownTable <- function
(file, peers = 2, out = paste(c(file,"md"), collapse = '.'),
subtitle = NULL,
@maptracker
maptracker / ColorCuts.R
Created January 22, 2016 18:19
Coloring the groups made by #cutree from an #R #hclust analysis (#base graphics)
## Subset of arrest data for murder and assault in the 50 US states
ma <- USArrests[, c("Murder", "Assault")]
## (FWIW, I don't think this represents a meaningful clustering of states)
## Cluster
mahc <- hclust(dist(ma), "ave")
## Cut into 6 groups
ct <- cutree(mahc, k = 6)
## Get the order of the states along the x axis
xord = mahc$labels[ mahc$order ];
## Plot the dendrogram
@maptracker
maptracker / PlayingWithKmeans.R
Last active January 22, 2016 15:44
Exploring #kmeans stability in #R
## Code was initially taken From Coursera lecture:
## https://www.coursera.org/learn/exploratory-data-analysis/lecture/6hOqi/k-means-clustering-part-2
set.seed(1234)
## Generate 30 points randomly assorted around 3 centroids: (1,1), (2,2), (3,1)
numPoints <- 30
x <- rnorm(numPoints, mean = rep(1:3, each = numPoints/3), sd = 0.2)
y <- rnorm(numPoints, mean = rep(c(1, 2, 1), each = numPoints/3), sd = 0.2)
## Organize as DF
dataFrame <- data.frame(x = x, y = y, row.names = 1:numPoints)
@maptracker
maptracker / PlotCharacters.R
Last active January 21, 2016 14:01
Visualize #R #pch (plot characters) used in #points
# From @Greg Snow https://stackoverflow.com/a/3740473
# Plots a grid with default tokens on bottom two rows, characters in others
png(filename="GregSnow.png", width = 600, height = 600)
plot( 0:15, 0:15, type='n', main = "pch values by Greg Snow" )
points( (0:255)%% 16, (0:255) %/% 16, pch=0:255, font=5, cex = 2 )
dev.off()
# From the R help for ?points
png(filename="PointsHelp.png", width = 600, height = 600)
pchShow <-
@maptracker
maptracker / rColors.R
Last active January 19, 2016 18:17
#color defaults used in #R
### Explore the colors used by R, as hex codes
## The default palette, a vector of strings (like "green2")
myPal <- palette()
## Find the RGB codes for the palette, a red/blue/green matrix of 0-255:
myRGB <- col2rgb( myPal )
## Convert to hex codes:
myHex <- apply( myRGB, 2, function (x) {
rgb(x[1], x[2], x[3], maxColorValue = 255)
} )
@maptracker
maptracker / ggplot_smooth_and_wrap.R
Created November 30, 2015 21:01
#R - UAH data set using #ggplot functions #geom_smooth and #facet_wrap
## Using UAH satellite data to demonstrate faceting and smoothing
library(ggplot2)
## There are a bunch of comments at the end of the file, take first
## 443 rows (up to Oct 2015)
numRow <- 443
url <- "http://vortex.nsstc.uah.edu/data/msu/t2lt/uahncdc_lt_5.6.txt"
uah <- read.delim(url, sep = "", nrows = numRow)
## Assign month names as a factor
@maptracker
maptracker / GraphicalBenchmarks.R
Created November 6, 2015 18:37
Graphical benchmarks with #microbenchmark in #R
# Code to make random matrices:
source("https://gist.github.com/maptracker/07390983253758614ecc/raw/5995a7c1112420b64ff33dcb1e7dac679d05dbf9/randomMatrix.R")
library("microbenchmark")
library("ggplot2")
# Iterate 1000 times over solving three sizes of matrices
bench <- microbenchmark( "10x10" = solve( randomMatrix( 10 ) ),
"20x20" = solve( randomMatrix( 20 ) ),
"50x50" = solve( randomMatrix( 50 ) ),
times = 1000 )
# Plot results: