Skip to content

Instantly share code, notes, and snippets.

View emhart's full-sized avatar

Edmund Hart emhart

View GitHub Profile
@emhart
emhart / foodwebplot.R
Created December 20, 2011 21:44
A function to plot food webs with examples
###############Food web plotting function################
### Date: 12/13/2011
### Author: Edmund Hart (edmund.m.hart@gmail.com)
### Description: A function to create grahps of trophic networks using ggplot2
### Plots food webs in a circular graph
### requires ggplot2
#########################################################
library(ggplot2)
########## support function create.xy
@emhart
emhart / MCinte.R
Created April 27, 2012 20:20
Code for Monte Carlo integration of a normal PDF
x <- seq(-4,4,length=1000)
y <- dnorm(x,0,1)
plot(x,y,type='l',ylim=c(0,.5))
###Set the lower limits of interest
ll <- -1
ul <- 1
######Hit and miss MC integration#####
###First pick domain
x.dom <- seq(-4,4,length=1000)
y.dom <- seq(0,.5,length=1000)
@emhart
emhart / randttest.R
Created April 27, 2012 21:31
A randomization method for a t-test
###Example 1
# Simple test of difference of two means
####
#############Note I do some plots in regular and again in ggplot, so install ggplot if you want to try it.
#install.packages("ggplot2")
library(ggplot2)
###Generate two random data sets###
@emhart
emhart / ggplot2SEM.R
Created May 1, 2012 04:48
ggplot2 SEM model example
###Generate box coords
###A function to generat a blank box for use with geom_path()
box_coords <- function(x,y,size){
b.x <- c(rep(x,2) - (size[1] / 2),rep(x,2) + (size[1] / 2))
b.x[5] <- b.x[1]
b.y <- c(y-(size[2] / 2),rep(y,2)+(size[2] / 2),y-(size[2] / 2))
b.y[5] <- b.y[1]
rmat <- data.frame(cbind(b.x,b.y))
colnames(rmat) <- c("x","y")
@emhart
emhart / ggplotQ
Created June 12, 2012 21:10
Just to answer a ggplot Q
sm.q <- ddply(master.sm,.(noise,allee,cat),numcolwise(median),na.rm=T)
sm.names <- ddply(master.sm,.(allee,noise),numcolwise(median),na.rm=T)
sm.names <- sm.names[,1:2]
sm.names$aslope <- c(rep(-1.879652,5),rep(-1.584475,5))
sm.q$ltype <- as.factor(rep(2,dim(sm.q)[1]))
sm.slope <- ggplot(master.sm, aes(x=meanslope,group=cat,fill=cat)) +geom_density(alpha=.5)+xlim(-3,0)+facet_grid(noise~allee,scale="free_y")+opts(axis.ticks = theme_blank(), axis.text.y = theme_blank())+scale_fill_manual("Catastrophe \n size",values=c("red","blue","black"))+xlab("Mean Slope")+ylab("Density")
sm.slope <- sm.slope+ geom_vline(aes(xintercept=-1),color="red", linetype="dashed", size=.3)
sm.slope <- sm.slope + geom_vline(data=sm.q,aes(xintercept=meanslope,color=cat,linetype="dashed"))+facet_grid(noise~allee,scales="free_y")+scale_colour_manual(values=c("red","blue","black"),legend= F)
sm.slope <- sm.slope + geom_vline(data=sm.names,aes(xintercept=aslope,legend=F,linetype="dashed",size=.8))+facet_grid(noise~allee,scales="free_y")
@emhart
emhart / exponentialEcolog.R
Created June 26, 2012 02:55
Code from my curve fitting blog post about Ecolog.
###### The exponential growth of ecolog
###### Raw data can be found here
###### https://listserv.umd.edu/cgi-bin/wa?A2=ind1108e&L=ecolog-l&P=3401
#####Data from Aug 31. 2011 post
require(date)
subscribers <- c(100,6000,7000,8000,9000,10000,11000,12000,13000)
start.date <- mdy.date(1,1,92)
month <- c("01/01/1992","09/01/2006","11/01/2007","10/01/2008","03/01/2009","04/01/2010","09/01/2010","02/01/2011","09/01/2011")
days <- as.date(month)-start.date
@emhart
emhart / gist:3272216
Created August 6, 2012 08:10
Code for ESA text mining
#include libraries
require(twitteR)
require(ggplot2)
require(tm)
require(wordcloud)
require(RColorBrewer)
require(reshape)
# we can only access 1500 tweets without oauth
twit.data <- searchTwitter("#ESA2012",n=1500)
@emhart
emhart / lapply jump
Created September 11, 2012 18:05
How to exit a function after a certain time period in R
# The data in the list you want to use lapply with
my_dat <- as.list(sample(1:10000,10, replace=T))
foo_time <- function(dat,time_thresh = 2){
start_time <- proc.time()
end_time <- 0
@emhart
emhart / contcolorggplot.R
Created October 4, 2012 20:10
Add continuous like colors to a discrete variable
gs.pal <- colorRampPalette(c("red","blue"),bias=.1,space="rgb")
x <- rnorm(100)
dat <- data.frame(cbind(x))
dat$fac <- as.factor(sort(rep(1:5,20)))
dat$y <- x * as.numeric(dat$fac)
ggplot(dat,aes(x=x,y=y,colour=fac))+geom_point()+scale_colour_manual(values=gs.pal(5))
@emhart
emhart / ggplot-opts-ex.R
Created October 17, 2012 22:41
ggplot options
### Set-up theme
theme_popblk = function(size=12) {
o = list(
#axis.line=theme_blank(),
axis.text.x=theme_text(size=size*.9),
axis.text.y=theme_text(size=size*.9),
# axis.text.y=theme_blank(),
# axis.ticks.y=theme_blank(),
# axis.ticks=theme_blank(),
axis.ticks.length=unit(0.3, "lines"),