Skip to content

Instantly share code, notes, and snippets.

@friendly
friendly / minard-plot.R
Last active April 7, 2022 11:30
Reproduce Minard's March on Moscow Graphic using ggplot2
#' # Reproduce Minard's March on Moscow Graphic using ggplot2
#' This gist extends the examples given in the HistData package. It explicitly
#' shows the construction of the plot as layers, and adds the plot of temperature
#' vs. longitude below the plot of troop strength.
#' ## Load data and required packages
data(Minard.troops, package="HistData")
data(Minard.cities, package="HistData")
data(Minard.temp, package="HistData")
# Taken from: http://janhove.github.io/teaching/2016/11/21/what-correlations-look-like
# source: http://janhove.github.io/RCode/plot_r.R
#
# plot_r
#
# Given a correlation coefficient and a sample size,
# this function draws 16 scatterplots
# whose shapes differ greatly
# but all of which are consistent with the correlation coefficient.
#
@friendly
friendly / wavelength_to_rgb.R
Created October 24, 2016 19:50
Convert wavelength of color to RGB
#' Wavelength to RGB
#'
#' This function converts a given wavelength of light to an
#' approximate RGB color value.
#'
#' @param wavelength A wavelength value, in nanometers, in the human visual range from 380 nm through 750 nm.
#' These correspond to frequencies in the range from 789 THz through 400 THz.
#' @param gamma The \eqn{\gamma} correction for a given display device. The linear RGB values will require
#' gamma correction if the display device has nonlinear response.
#' @return a color string, corresponding to the result of \code{\link[grDevices]{rgb}} on the
@friendly
friendly / 3d-demo.R
Created September 30, 2016 14:58
3D demonstrations of linear transformations and matrix inverse
#' ---
#' title: "3D demonstrations of linear transformations and matrix inverse"
#' author: "Michael Friendly"
#' date: "30 Sep 2016"
#' ---
#' Start with a unit cube, representing the identity matrix. Show its transformation
#' by a matrix $A$ as the corresponding transformation of the cube.
#'
#' This also illustrates the determinant, det(A), as the volume of the transformed
@friendly
friendly / boxM.R
Created February 10, 2016 15:12
Box's M-test for Homogeneity of Covariance Matrices
# Box's M-test for Homogeneity of Covariance Matrices
boxM <-
function(mod, ...) UseMethod("boxM")
boxM.default <- function(Y, group)
{
dname <- deparse(substitute(Y))
if (!inherits(Y, c("data.frame", "matrix")))
@friendly
friendly / odds.R
Created August 10, 2015 20:18
calculate log odds
odds <- function(x, log=FALSE, ...)
lodds(x, log=log, ...)
lodds <- function(x, ...)
UseMethod("lodds")
lodds.formula <-
function(formula, data = NULL, ..., subset = NULL, na.action = NULL)
{
@friendly
friendly / migrate-svn-git.md
Last active September 18, 2017 21:00
Migration path for R packages from svn/R-Forge to git/github

Migrating SVN/R-Forge packages to git/github

Migration \Mi*gra"tion, n. [L. migratio: cf. F. migration]

1: The movement of persons or groups from one country or locality to another.

2: The passage of software developers from one platform, language or environment to another for the purpose of feeding, breeding or enhanced health of their offspring.

Context