Subversion Repositories public iLand

Rev

Rev 814 | Rev 1104 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
814 werner 2
## Example for DBH-Distribution barcharts
3
 
4
library(RSQLite)
5
library(ggplot2)
6
library(plyr)
7
library(reshape)
8
library(scales)
9
 
10
cols.species <- c("abal"="#088A29",
11
                  "acca"="#F3F781",
12
                  "acpl"="#86B404",
13
                  "acps"="#58FAD0",
14
                  "algl"="#61210B",
15
                  "alin"="#A4A4A4",
16
                  "alvi"="#0B3B17",
17
                  "bepe"="#2E64FE",
18
                  "cabe"= "#F7BE81",
19
                  "casa"="#A9F5A9",
20
                  "coav"="#58D3F7",
21
                  "fasy"="#2EFE2E",
22
                  "frex"="#FF0000",
23
                  "lade"="#8A4B08",
24
                  "piab"="#FFFF00",
25
                  "pice"="#FF8000",
26
                  "pini"="#610B21",
27
                  "pisy"="#B18904",
28
                  "poni"="#000000",
29
                  "potr"="#610B5E",
30
                  "psme"="#DF0174",
31
                  "qupe"="#F6CED8",
32
                  "qupu"="#FA5882",
33
                  "quro"="#B40431",
34
                  "rops"="#F781D8",
35
                  "saca"="#F5ECCE",
36
                  "soar"="#E6E0F8",
37
                  "soau"="#B40404",
38
                  "tico"="#9F81F7",
39
                  "tipl"="#8000FF",
40
                  "ulgl"="#DF7401")
41
 
42
### custom function for creating the dbh-distribution charts
43
### the data is generated by the "Dynamic stand" output of iLand.
44
barchart_graphs <-function(data, label) {
45
  ## extract dbh classes
46
  count_ha.sp=data.frame(species=data$species, dbh10=data$if_dbh_10_1_0_sum/100, dbh20=data$if_dbh_10_and_dbh_20_1_0_sum/100, dbh30=data$if_dbh_20_and_dbh_30_1_0_sum/100, dbh40=data$if_dbh_30_and_dbh_40_1_0_sum/100, dbh50=data$if_dbh_40_and_dbh_50_1_0_sum/100, dbh60=data$if_dbh_50_and_dbh_60_1_0_sum/100, dbh70=data$if_dbh_60_and_dbh_70_1_0_sum/100, dbh80=data$if_dbh_70_and_dbh_80_1_0_sum/100, dbh90=data$if_dbh_80_and_dbh_90_1_0_sum/100, dbh100=data$if_dbh_90_and_dbh_100_1_0_sum/100, dbh100plus=data$if_dbh_100_1_0_sum/100)
47
  # reshape (melt)
48
  count_ha.sp.melted <- melt.data.frame(count_ha.sp, id.vars="species", na.rm=FALSE)
49
  # and calculate a total
50
  count_ha.total <- colSums(count_ha.sp[,2:12])
51
  # now the plotting:
52
  barplot(count_ha.total+1, log="y", main=paste("Stemnumbers", label), ylab="N (+1)")
53
  ## the ggplot
54
  ggplot(count_ha.sp.melted, aes(x=variable,y=value,group=variable,fill=species)) +
55
    geom_bar(stat="identity")  + scale_fill_manual(values=cols.species)+
56
    labs(xlab="dbh classes", ylab="Stem number", title=paste("dbh distribution", label))
57
}
58
 
59
#### load data from the database ######
60
conn<-dbConnect("SQLite", dbname = paste("C:/Users/werner/Desktop/bare_soil_afi413_pnv_buffer.sqlite",sep=""))
61
dyn.out=dbReadTable(conn,"dynamicstand")
62
dbDisconnect(conn)
63
 
64
summary(dyn.out)
65
 
66
 
67
 
68
### create charts for several timesteps ########
69
#data <- dyn.out[dyn.out$year == 50,]
70
 
71
barchart_graphs(dyn.out[dyn.out$year == 50,], "50")
72
barchart_graphs(dyn.out[dyn.out$year == 100,], "100")
73
barchart_graphs(dyn.out[dyn.out$year == 500,], "500")
74
barchart_graphs(dyn.out[dyn.out$year == 1000,], "1000")
75
 
76