Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

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