Skip to content

Instantly share code, notes, and snippets.

View epijim's full-sized avatar

James Black epijim

View GitHub Profile
@epijim
epijim / Correlagram _scatterplot matrix.R
Last active December 22, 2015 17:39
The corrgram package in R can produce a scattergraph in the lower diamond, with smoothed lines and confidence ellipse in the upper diamond. Great for visualising correlations in the dataset.
library(corrgram)
# Create correlagram of scatter against lines with conficence
# lines are loess smoothed curve
# eclipse is bivariate 68% concentration ellipse (68% of data inside!)
corrgram(Dataframe[,VECTORofCOLUMNSofINTEREST], order=FALSE, lower.panel=panel.pts,
upper.panel=panel.ellipse, text.panel=panel.txt,
diag.panel=panel.minmax,
main="Correlations between variables within the vector VECTORofCOLUMNSofINTEREST")
@epijim
epijim / 2way.r
Created September 10, 2013 13:56
2 categories
#--The following was used to define the colours, but since it's a non-standard
# package, I've used the hexadecimal colour codes explicity below to avoid the
# need to install "RColorBrewer". See http://colorbrewer2.org/ for more info.
#require(RColorBrewer)
#colset <- brewer.pal(3, "Pastel2")
colset <- c("#B3E2CD", "#FDCDAC", "#CBD5E8")
path <- "http://www.sr.bham.ac.uk/~ajrs/R/datasets"
#--Data file as taken from XMM website, but with "#" added to comment lines
# HOW TO BELOW
# http://www.sr.bham.ac.uk/~ajrs/R/r-tutorials.html#NEDplot
#--Read in data file:
url <- "http://www.sr.bham.ac.uk/~ajrs"
file <- "R/datasets/a85_extended_NEDsearch.txt"
A <- read.table(paste(url, file, sep="/"), sep="|", skip=20, header=TRUE)
close(url(paste(url, file, sep="/"))) # close connection after use
#--Specfify output graphics device:
@epijim
epijim / contour.r
Last active December 22, 2015 18:39
Contour plot in R
#Example Four Panel Contour Plot with One Legend
#Author: Carey R McGilliard
#September 2010
#http://wiki.cbr.washington.edu/qerm/index.php/R/Contour_Plots
#This code uses a modified version of filled.contour called filled.contour3 (created by Carey McGilliard, Ian Taylor, and Bridget Ferris)
#to make an example figure of four contour plots sharing a legend (to the right).
#The example demonstrates how to use various color schemes for the contour plots and legend, but the user will want to
#pick one color scheme for all four plots such that the legend matches the plots.
filled.contour3 <-
function (x = seq(0, 1, length.out = nrow(z)),
y = seq(0, 1, length.out = ncol(z)), z, xlim = range(x, finite = TRUE),
ylim = range(y, finite = TRUE), zlim = range(z, finite = TRUE),
levels = pretty(zlim, nlevels), nlevels = 20, color.palette = cm.colors,
col = color.palette(length(levels) - 1), plot.title, plot.axes,
key.title, key.axes, asp = NA, xaxs = "i", yaxs = "i", las = 1,
axes = TRUE, frame.plot = axes,mar, ...)
{
# modification by Ian Taylor of the filled.contour function
filled.legend <-
function (x = seq(0, 1, length.out = nrow(z)), y = seq(0, 1,
length.out = ncol(z)), z, xlim = range(x, finite = TRUE),
ylim = range(y, finite = TRUE), zlim = range(z, finite = TRUE),
levels = pretty(zlim, nlevels), nlevels = 20, color.palette = cm.colors,
col = color.palette(length(levels) - 1), plot.title, plot.axes,
key.title, key.axes, asp = NA, xaxs = "i", yaxs = "i", las = 1,
axes = TRUE, frame.plot = axes, ...)
{
# modification of filled.contour by Carey McGilliard and Bridget Ferris
@epijim
epijim / Proportional twoway (or more, in a mosiac plot).r
Last active December 23, 2015 03:39
Simple example - a two way table of categorical variables, with size proportional to number
library(vcd)
# create a matrix with two categorical variables.
VECTOR <- xtabs(~ Var1 + Var2, data = Dataframe)
# create mosiac
mosaic(VECTOR, gp = shading_max, split_vertical = TRUE)
@epijim
epijim / swarmplot.r
Created October 1, 2013 13:26
swarmplot, visualise continuous distribution by a catergorical stratification variable.
# data
set.seed(1234)
bimodal <- c(rnorm(250, -2, 0.6), rnorm(250, 2, 0.6))
uniform <- runif(500, -4, 4)
normal <- rnorm(500, 0, 1.5)
dataf <- data.frame (group = rep(c("bimodal","uniform", "normal"), each = 500), xv = c(bimodal, uniform, normal), cg = rep( c("A","B"), 750))
require(beeswarm)
# hexagon
@epijim
epijim / histwithboxplot.r
Created October 1, 2013 13:43
a histograph with a bar chart overlaid
# data
set.seed(4566)
data <- rnorm(100)
# Added to the plot:
par(mar=c(3.1, 3.1, 1.1, 2.1))
hist(data,xlim=c(-4,4), col = "pink")
boxplot(data, horizontal=TRUE, outline=TRUE, ylim=c(-4,4), frame=F, col = "green1", add = TRUE)
@epijim
epijim / stacked_histogram.r
Created October 1, 2013 13:58
stacked histogram
plot using ggplot2
require(ggplot2)
#data
set.seed(1233)
data1 <- data.frame(pop =c(rep("A x B", 200), rep("A x C", 200), rep("B x C", 200) ) , var1 = c(rnorm(1000, 90,10), rnorm(1000, 50, 10), rnorm(1000, 20, 30)))
qplot( var1, data = data1, geom = "histogram" , group = pop, fill = pop, alpha=.3) + theme_bw( )