Skip to content

Instantly share code, notes, and snippets.

@kevinushey
kevinushey / position_jitterdodge.R
Last active August 29, 2015 13:57
Combine position_jitter and position_dodge so points can align with boxplots in ggplot2
#' Jitter-dodge points to align them with a boxplot including fill aesthetic
#'
#' @family position adjustments
#' @param width degree of jitter in x direction. Defaults to 40\% of the
#' resolution of the data.
#' @param height degree of jitter in y direction. Defaults to 40\% of the
#' resolution of the data
#' @export
#' @examples
#' dsub <- diamonds[ sample(1:nrow(diamonds), 1000), ]
@klmr
klmr / xrange.r
Last active August 29, 2015 14:01
Python-like range operator in R, allowing to specify step size
`:` = function (a, b) {
if (inherits(a, 'xrange'))
do.call(seq, as.list(c(range(a), by = b)))
else if (inherits(a, 'factor'))
interaction(a, b, sep = ':')
else
structure(seq(a, b), class = 'xrange')
}
print.xrange = function (x)
## Correlation matrix with p-values. See http://goo.gl/nahmV for documentation of this function
cor.prob <- function (X, dfr = nrow(X) - 2) {
R <- cor(X, use="pairwise.complete.obs")
above <- row(R) < col(R)
r2 <- R[above]^2
Fstat <- r2 * dfr/(1 - r2)
R[above] <- 1 - pf(Fstat, 1, dfr)
R[row(R) == col(R)] <- NA
R
}
@shawngraham
shawngraham / getting a History Machine
Last active August 29, 2015 14:14
setting up a history research machine. Follow the instructions.
# By Ben Marwick, from: https://gist.github.com/benmarwick/11204658 with modifications by S. Graham
Short instructions to setup a Lubuntu Virtual Machine with
R & RStudio:
1. Download these:
http://lubuntu.net/ (Intel x86 desktop cd)
https://www.virtualbox.org/wiki/Downloads (Oracle VM VirtualBox)
2. Install Oracle VM VirtualBox, open it (if using windows,
@sfsheath
sfsheath / oa.R
Created February 27, 2015 21:59
(Not counting calls to library) 2 lines of R code that map data from Open Access
# install.packages("plotGoogleMaps")
# install.packages("jsonlite")
# install.packages("rgdal")
library(jsonlite)
library(rgdal)
library(plotGoogleMaps)
readOGR(dsn = "http://146.148.79.138/sets/Cyprus.json?q=african&response=geo-record&rows=10000", layer = "OGRGeoJSON") -> oa.json
m1<-plotGoogleMaps(oa.json,zcol = "early.bce.ce", filename="oa.html")
@petermeissner
petermeissner / to_char_bars.md
Last active August 29, 2015 14:16
How to get numbers transformed to character bar charts ... to_char_bars()
@Sharpie
Sharpie / grainAnalysis.R
Created January 31, 2010 17:44
R code related to graphical analysis of a sieved soil sample.
# Load Data
grainData <- read.csv('grainSize.csv', check.names=F, na.strings='--' )
# Calculate Derived Sample Values
grainData[['Phi Diameter']] <- -log2( grainData[['Grain Diameter']] )
totalWeight <- sum( grainData[['Sample Weight']] )
grainData[["Percent Retained"]] <- grainData[['Sample Weight']] / totalWeight
grainData[["Cumulative Percent"]] <- cumsum( grainData[["Percent Retained"]] )
grainData[['Percent Finer']] <- 1 - grainData[['Cumulative Percent']]
@benmarwick
benmarwick / grainAnalysis.R
Created December 28, 2011 00:16 — forked from Sharpie/grainAnalysis.R
R code related to graphical analysis of a sieved soil sample.
# Load Data
grainData <- read.csv('grainSize.csv', check.names=F, na.strings='--' )
# Calculate Derived Sample Values
grainData[['Phi Diameter']] <- -log2( grainData[['Grain Diameter']] )
totalWeight <- sum( grainData[['Sample Weight']] )
grainData[["Percent Retained"]] <- grainData[['Sample Weight']] / totalWeight
grainData[["Cumulative Percent"]] <- cumsum( grainData[["Percent Retained"]] )
grainData[['Percent Finer']] <- 1 - grainData[['Cumulative Percent']]
@rmflight
rmflight / tableFigureFuncs.r
Created October 9, 2012 13:54
knitr table and figure functions for Rmd
pasteLabel <- function(preText, inObj, objName, insLink=TRUE){
objNum <- inObj[objName]
useText <- paste(preText, objNum, sep=" ")
if (insLink){
useText <- paste("[", useText, "](#", objName, ")", sep="")
}
useText
}