Rev 1221 | Blame | Compare with Previous | Last modification | View Log | RSS feed
## Example for DBH-Distribution barcharts
library(RSQLite)
library(ggplot2)
library(plyr)
library(reshape)
library(scales)
cols.species <- c("abal"="#088A29",
"acca"="#F3F781",
"acpl"="#86B404",
"acps"="#58FAD0",
"algl"="#61210B",
"alin"="#A4A4A4",
"alvi"="#0B3B17",
"bepe"="#2E64FE",
"cabe"= "#F7BE81",
"casa"="#A9F5A9",
"coav"="#58D3F7",
"fasy"="#2EFE2E",
"frex"="#FF0000",
"lade"="#8A4B08",
"piab"="#FFFF00",
"pice"="#FF8000",
"pini"="#610B21",
"pisy"="#B18904",
"poni"="#000000",
"potr"="#610B5E",
"psme"="#DF0174",
"qupe"="#F6CED8",
"qupu"="#FA5882",
"quro"="#B40431",
"rops"="#F781D8",
"saca"="#F5ECCE",
"soar"="#E6E0F8",
"soau"="#B40404",
"tico"="#9F81F7",
"tipl"="#8000FF",
"ulgl"="#DF7401")
### custom function for creating the dbh-distribution charts
### the data is generated by the "Dynamic stand" output of iLand.
barchart_graphs <-function(data, label) {
## extract dbh classes
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)
# reshape (melt)
count_ha.sp.melted <- melt.data.frame(count_ha.sp, id.vars="species", na.rm=FALSE)
# and calculate a total
count_ha.total <- colSums(count_ha.sp[,2:12])
# now the plotting:
barplot(count_ha.total+1, log="y", main=paste("Stemnumbers", label), ylab="N (+1)")
## the ggplot
ggplot(count_ha.sp.melted, aes(x=variable,y=value,group=variable,fill=species)) +
geom_bar(stat="identity") + scale_fill_manual(values=cols.species)+
labs(xlab="dbh classes", ylab="Stem number", title=paste("dbh distribution", label))
}
#### load data from the database ######
conn<-dbConnect("SQLite", dbname = paste("C:/Users/werner/Desktop/bare_soil_afi413_pnv_buffer.sqlite",sep=""))
dyn.out=dbReadTable(conn,"dynamicstand")
dbDisconnect(conn)
summary(dyn.out)
### create charts for several timesteps ########
#data <- dyn.out[dyn.out$year == 50,]
barchart_graphs(dyn.out[dyn.out$year == 50,], "50")
barchart_graphs(dyn.out[dyn.out$year == 100,], "100")
barchart_graphs(dyn.out[dyn.out$year == 500,], "500")
barchart_graphs(dyn.out[dyn.out$year == 1000,], "1000")