Advertisement
Guest User

Untitled

a guest
May 8th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. ##=====================================================
  2. ##=========== FOREST INVENTORY SCRIPT =================
  3. ##=====================================================
  4. ##====== NORTHWOODS NATURAL RESOURCES, LLC ============
  5. ##====== WWW.NORTHWOODSRESOURCES.COM ============
  6. ##====== PREPARED BY: LEE MUELLER ============
  7. ##=====================================================
  8. library('reshape')
  9.  
  10. ## This script is designed to take options for various
  11. ## forest inventory systems to analyze and generate
  12. ## inventory reports for various forestry purposes.
  13. ## configure the file accordingly and pay attention to
  14. ## report generated locations.
  15.  
  16. ## ===================================================== CONFIGURE
  17. ## Set directory in which the file is housed.
  18. setwd("/home/lee/Documents/Northwoods/Inventories/")
  19. ## Your data will need to have the following variables:
  20. name <- "West Fork Tree Farm" #Project Name
  21. proj <- "McKarns" #Unique project ID
  22. date <- "" #Inventory Date
  23. inv <- read.csv("mckarn_inventory.csv") #File Name
  24.  
  25. ## ===================================================== CODE
  26. attach(inv) #format data
  27. names(inv)
  28.  
  29. uniquestands <- unique(stand) #return unique stands
  30. numstands <- length(uniquestands) #get number of stand
  31.  
  32. for ( i in 1:numstands) #open loop 1-number of stands
  33. {
  34.  
  35. ## Separate trees by stand
  36. standsub <- subset(inv, stand== i) #set subsample based on plot number. Note loop variable.
  37. colnames(standsub) <- names(inv)
  38.  
  39. #Standard mathematics for samples. Based on 10 BAF point sample.
  40. standsub$numplots <- apply(standsub,1,function(row) length(unique(standsub$plot))) #add numplot column for number of plots per stand for later adjustment
  41. standsub$tpa <- (1/( (((standsub$dbh * 2.75)^2) * 3.14159)/43560))/standsub$numplots #add column for TPA of each sample
  42.  
  43. #Need to begin creating a table which can be graphed. We need one which is TPA by species in each size class. Standard stand table.
  44. #Create appropriate dataframe and organize to be pivoted correctly.
  45. graph <- data.frame(standsub$species, standsub$dbh, standsub$tpa)
  46. names(graph) <- c("species", "dbh", "tpa")
  47. attach(graph)
  48. names(graph)
  49.  
  50. #We want ACTUAL species names to show up in the graph. Let's do some replacing now.
  51. codes <- read.csv("speciescodes.csv") #Load the species codes file
  52. attach(codes)
  53. graph$species <- codes$text[match(graph$species, codes$code)] #match and replace
  54.  
  55. # Make pivot table. TPA by DBH and species. TPA is the variable of interest being summated.
  56. pivot <- cast(graph, dbh ~ species, sum)
  57.  
  58. #set dynamic filename
  59. filename <- paste( "plots/", proj, "-stand-", i, ".jpg", sep="") #if it doesn't work, this might be a problem.
  60.  
  61. #open plot save function
  62. jpeg(filename)
  63.  
  64. #Make a stacked barplot. X axis = DBH Y axis = TPA and stacks = species.
  65. par(xpd=T, mar=par()$mar+c(0,0,0,4))
  66. barplot(t(pivot), ylab="Trees Per Acre", xlab="Diameter at Breast Height (inches)", space=0.1, col=rainbow(12))
  67.  
  68. #For some reason, "DBH" variable shows up as a label in the plot. This bit removes it.
  69. labels <- names(pivot)
  70. labels <- labels[2:length(labels)]
  71.  
  72. #set legend with changed labels
  73. legend("topright", labels, fill=rainbow(12))
  74.  
  75. # Restore default clipping rect
  76. par(mar=c(5, 4, 4, 2) + 0.1)
  77.  
  78. #close save
  79. dev.off()
  80. } #close loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement