#Randomization scheme for the Nutnet experiment. # nutnet_rand 1.0, 29 Apr 07, Brett Melbourne. # R: www.r-project.org # #Hierarchical randomization for a single site. # One constraint is allowed: # The number of blocks (1-k; standard is 3) #Set this constraints for each site by changing these values: nblocks <- 3 #How many blocks? sitename <- "SITENAME_HERE" #To run the randomization program: # 1) Use a text editor to modify the constraints above to match your site # and SAVE THE FILE. # 2) Change the R working directory to the one containing the saved # file (nutnet_rand10.r). # 3) Enter 'source("nutnetrand10.r")' at the R prompt. # #The following files are created in the current working directory: # RandDesign_SITENAME_HERE.csv - a comma separated values spreadsheet that can be opened # in Excel. # DRAGNet-plan-SITENAME_HERE #----Program begins---- # DRAGNet treatments trt_labelsd<-c("Control", "Disturbance", "NPK", "NPK+Disturbance", "NPK_Cessation") # Dist = Disturbance NPKud <- c(0, 0, 1, 1, 1) Disturbanced <- c(0, 1, 0, 1, 0) Cessationd <- c(0, 0, 0, 0, 1) #Function to expand labels for all 4 subplots subplotrep <- function(x) { rep(x,rep(4,length(x))) } numerics <- NULL #To hold numerical labels chars <- NULL #To hold character labels n <- 1 for (Block in 1:nblocks) { # number of plots: np <- length(trt_labelsd) # number of plots (np) # Randomize treatments among plots within blocks block <- subplotrep( rep(Block,np) ) plot <- subplotrep( n:(n+np-1) ) #This is the random draw for plots rand<-sample(1:np) plot_trt <- subplotrep( rand ) NPKu <- subplotrep( NPKud[rand] ) Disturbance <- subplotrep( Disturbanced[rand] ) Cessation <- subplotrep( Cessationd[rand] ) trt_labels <- subplotrep(trt_labelsd[rand] ) n <- n + np # Randomize subplots nsubplots <- length(plot) subplot <- rep(1:4,nsubplots/4) subplot_lab <- rep(c("A","B","C","D"),nsubplots/4) subplot_trt <- integer(nsubplots) subplot_trt_lab <- character(nsubplots) for (i in seq(1,nsubplots-3,by=4)) { rand <- sample(1:4) #This is the random draw for subplots subplot_trt[i:(i+3)] <- rand subplot_trt_lab[i:(i+3)] <- c("Core","Future1","Future2","Site")[rand] } numerics <- rbind(numerics,cbind(block,plot,plot_trt,NPKu,Disturbance,Cessation,subplot,subplot_trt)) chars <- rbind(chars,cbind(trt_labels,subplot_lab,subplot_trt_lab)) } remove(block,plot,plot_trt,NPKu,Disturbance,Cessation, subplot,subplot_trt,trt_labels,subplot_lab,subplot_trt_lab) design <- cbind(as.data.frame(numerics),as.data.frame(chars)) design$plot_trt<-NULL design$subplot<-NULL design <- design[,c("block", "plot", "trt_labels", "NPKu", "Disturbance", "Cessation", "subplot_lab","subplot_trt_lab", "subplot_trt")] #reorder columns write.csv(design[,!names(design) %in% c("subplot_trt")],file=paste0("~/Desktop/RandDesign_", sitename, ".csv")) #Write to comma separated values file; can open in Excel #----Plot the design---- library(grid) #Low level graphics utilities plotspecs drawplot <- function(plotspecs, row, col){ pushViewport(viewport(layout.pos.row=row,layout.pos.col=col)) fillcol <- ifelse(plotspecs$trt_labels == "NPK", "#E2D859", ifelse(plotspecs$trt_labels == "NPK+Disturbance", "#7EA359", ifelse(plotspecs$trt_labels == "Disturbance", "#7DA9BC", ifelse(plotspecs$trt_labels == "NPK_Cessation", "#DD542F", "gray80")))) grid.rect(width=0.75,height=0.75, gp=gpar(fill=fillcol)) #Annotate the plot grid.text(x=0.5,y=1,paste("Plot",plotspecs$plot[1],sep=" ")) grid.text(x=0.5,y=.93,plotspecs$trt_labels[1]) #Draw subplots subplotspecs<-plotspecs[,c("subplot_trt_lab", "subplot_lab", "subplot_trt")] drawsubplots(subplotspecs) popViewport() } #Function to draw subplots drawsubplots <- function(subplotspecs){ plot.vp <- viewport(width=0.75,height=0.75,layout=grid.layout(2,2)) pushViewport(plot.vp) pos <- cbind(row=c(1,1,2,2),col=c(1,2,1,2)) for (i in 1:4) { pushViewport(viewport(layout.pos.row=pos[i,"row"],layout.pos.col=pos[i,"col"])) fillcol <- switch(subplotspecs[i,"subplot_trt"], alpha('white', 0.8), alpha('white', 0.1), alpha('white', 0.1), alpha('white', 0.1)) grid.rect(width=0.9,height=0.9,gp=gpar(fill=fillcol)) #Annotate the plot grid.text(x=0.2,y=0.8,subplotspecs[i,"subplot_lab"],gp=gpar(cex = 0.6)) grid.text(subplotspecs[i,"subplot_trt_lab"],gp=gpar(cex = 0.8)) popViewport() } popViewport() } #Draw the diagrams dev.off() pdf(paste0('~/Desktop/DRAGNet-plan-', sitename, ".pdf"), height = 8.5, width = 11) plot.new() nplots <- nrow(design)/4 ntrt <- length(unique(design$trt_labels)) pos <- cbind(row=rep(1:max(design$block), each = ntrt), col=1:ntrt) site.vp <- viewport(y=0.5, x=0.5, height=0.8, width=1, layout=grid.layout(nrow = max(design$block), # blocks in rows ncol = length(unique(design$trt_labels)))) # treatments in columns pushViewport(site.vp) for (i in 1:nplots) { plotspecs <- subset(design,plot==i) drawplot(plotspecs,pos[i,"row"],pos[i,"col"]) } grid.text(x=0.5,y=1.05,sitename) popViewport() dev.off()