specific R tricks

Barcharts (lattice)

precip <- barchart(pct_precip_diff ~ date_chunk | emission, groups=model, data=x, horizontal=FALSE, auto.key=list(rectangles=TRUE,columns=3,points=FALSE,cex=0.6),origin=0,ylab=”Precipitation difference (%)”,
panel=function(x,y,…){
panel.barchart(x,y,…);
panel.abline(h=0,lty=2)}

)

Reordering factors using transform

# merge the 2 dataframes which have same headers
threatdata=merge(x.threat,x.people,all=T)

#rename column headers
colnames(threatdata)=c(“Threat”,”Counts”,”ThreatClass”,”Percentage”)
###### —- customize from here —————

threatdata <- transform(threatdata, Threat=factor(Threat, levels=c(‘Extreme threat’, ‘Very High threat’, ‘High threat’, ‘Moderate threat’, ‘Little or no threat’)))

p <- barchart(Threat~Percentage,groups=ThreatClass, data=threatdata,auto.key=TRUE,xlab=”Percentage of Grid Cell”)

time series plots using ggplot2

##R commands using ggplot2 library
# temp and precip ts all 6 projections individually marked
temp.ts=qplot(date,mean_temp,data=x,color=emission,lty=model,geom=c(“path”),xlab=””,ylab=expression(paste(“Avg Annual Temperature (“,degree,”C)”)))
precip.ts=qplot(date,sum_precip,data=x,color=emission,lty=model,geom=c(“path”),xlab=””,ylab=”Annual Precipitation (mm)”)
##
##How to get a ribbon plot?temperature
p <- ggplot(x, aes(x = date, y = mean_temp, colour = emission,fill=emission))
ppp=p + stat_summary(fun = “range”, geom=”ribbon”)
temp.tsribbon=ppp + opts(axis.title.x=theme_blank()) + scale_y_continuous(expression(paste(“Avg Annual Temperature (“,degree,”C)”)))
####removes x axis label and renames y axis

Leave a comment