Skip to content

Instantly share code, notes, and snippets.

View fawda123's full-sized avatar

Marcus W Beck fawda123

View GitHub Profile
@fawda123
fawda123 / meuse exercise
Created May 6, 2018 16:51
meuse exercise
# load libraries
library(sp) # for meuse
library(sf) # for sf objects
library(ggmap) # for base maps
library(ggplot2) # for mapping
# import dataset, check structure
data(meuse)
str(meuse)
#import relevant packages
library(gstat)
library(sp)
library(nlme)
#create simulated data, lat and lon from uniformly distributed variable, exp1 and exp2 from random normal
set.seed(2)
samp.sz<-400
lat<-runif(samp.sz,-4,4)
lon<-runif(samp.sz,-4,4)
@fawda123
fawda123 / mc_int_ex.r
Created April 29, 2013 20:15
mc_int_ex
mc.int.ex<-function(x.range=c(-3,3),int=c(-1.96,1.96),n=10000,plot=T,
plot.pts=T,plot.poly=T,cols=list('black','green','blue'),round.val=2,val=F){
require(scales)
#create data for model fit
x.fit<-seq(x.range[1],x.range[2],length=n)
y.fit<-dnorm(x.fit)
dat.fit<-data.frame(x.fit,y.fit)
@fawda123
fawda123 / ggplot2_ex.r
Created September 17, 2013 16:16
ggplot2_ex
#credit http://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots
require(ggplot2)
require(reshape)
require(gridExtra)
p.dat<-data.frame(step=row.names(grp.dat),grp.dat,stringsAsFactors=F)
p.dat<-melt(p.dat,id='step')
p.dat$step<-as.numeric(p.dat$step)
@fawda123
fawda123 / moth_form.R
Created October 25, 2016 15:05
moth_form
#' Format a text file for mothur
#'
#' @param fl chr string for input file
#' @param rev_cmp logical if file needs reverse complemeNT
#' @param savefl logical if output file is saved, otherwise the output is returned in the console
moth_form <- function(fl, rev_cmp = FALSE, savefl = TRUE, flnm = 'myfile.txt'){
# import file
dat <- read.table(fl, sep = '\t')
@fawda123
fawda123 / plotFlowConc.r
Created August 29, 2016 15:22
plotFlowConc for OWI blog
plotFlowConc <- function(eList, month = c(1:12), years = NULL, col_vec = c('red', 'green', 'blue'), ylabel = NULL, xlabel = NULL, alpha = 1, size = 1, allflo = FALSE, ncol = NULL, grids = TRUE, scales = NULL, interp = 4, pretty = TRUE, use_bw = TRUE, fac_nms = NULL, ymin = 0){
localDaily <- getDaily(eList)
localINFO <- getInfo(eList)
localsurfaces <- getSurfaces(eList)
# plot title
toplab <- with(eList$INFO, paste(shortName, paramShortName, sep = ', '))
# flow, date info for interpolation surface
@fawda123
fawda123 / plot_qual.r
Last active October 24, 2016 14:04
plot_qual
plot.qual<-function(x,x.locs=c(0.01,0.99),y.locs=c(0,1),steps=NULL,sp.names=NULL,dt.tx=T,rsc=T,
ln.st=NULL,rs.ln=c(3,15),ln.cl='RdYlGn',alpha=0.7,leg=T,rnks=FALSE,...){
require(RColorBrewer)
require(scales)
if(length(x.locs) != 2 | length(y.locs) != 2)
stop('x and y dimensions must be two-element vectors')
if(x.locs[1]<0 | x.locs[2]>1 | y.locs[1]<0 | y.locs[2]>1)
@fawda123
fawda123 / PPOTM.r
Created January 19, 2016 14:52
practical programming ex
# PPOTM: Practical-Programming Of The Month (2016 January)
# iMAINloop: Continue asking users "what file to load" until they type a zero.
iMAINloop <- 0;
while (iMAINloop < 1 ) {
# Below creates a list of all files (in the current working directory) of pattern *.csv
# Note that you can set path="xxxx" to look in a different folder area
filenames <- list.files(pattern = ".csv")
@fawda123
fawda123 / lek_fun.r
Last active December 24, 2015 20:49
lek_fun
lek.fun<-function(mod.in,var.sens=NULL,resp.name=NULL,exp.in=NULL,steps=100,split.vals=seq(0,1,by=0.2),val.out=F){
require(ggplot2)
require(reshape)
##
#sort out exp and resp names based on object type of call to mod.in
#get matrix for exp vars
#for nnet
@fawda123
fawda123 / plot_area.r
Last active December 23, 2015 05:49
plot_area
plot.area<-function(x,col=NULL,horiz=F,prop=T,stp.ln=T,grp.ln=T,axs.cex=1,axs.lab=T,lab.cex=1,
names=c('Group','Step','Value'),...){
#sort out color fector
if(!is.null(col)){
if(sum(col %in% colors()) != length(col)) stop('col vector must be in "colors()"')
col<-colorRampPalette(col)(ncol(x))
}
else col<-colorRampPalette(c('lightblue','green'))(ncol(x))