Subversion Repositories public iLand

Rev

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

Rev Author Line No. Line
1
 
791 werner 2
###
3
### load some useful tools/functions
4
#source("sql_tools.R")
5
library(RSQLite)
6
 
7
# connect to an existing database
1054 werner 8
db.conn <<- dbConnect(RSQLite::SQLite(), dbname="e:/Daten/iLand/projects/HJA_WS12_v2/output/HJA_WS12.sqlite" )
791 werner 9
 
10
### load data from the iLand output table
11
stand <- dbReadTable(db.conn, "stand")
12
# alternatively (using sql_tools): stand <- query("select * from stand")
13
 
14
summary(stand)
15
 
16
## stacked bar chart, e.g. for basal area
17
library(ggplot2)
18
 
19
head(stand)
20
# aggregate to values for each species and year (i.e. averaging over all the resource units)
21
stand.avg <- aggregate(stand, list(year=stand$year, species=stand$species), FUN="mean")
22
head(stand.avg)
23
 
816 werner 24
#### provide fixed colors for species (in form of key-value pairs; the key is the species code, value is the color)
25
cols.species <- c("Tshe"="#101010",
26
                  "Abam"="#202020",
27
                  "Alru" =  "#303030",
28
                  "Thpl" = "#404040",
29
                  "Psme" = "#505050",
30
                  "Acma" = "#606060",
31
                  "var2" = "#707070",
32
                  "var3" = "#808080")
33
 
791 werner 34
# now we can plot them:
816 werner 35
ggplot(stand.avg, aes(x=year,y=basal_area_m2,group=species,fill=species)) + geom_area() + labs(xlab="year", ylab="basal area m2", title="average basal area") + scale_fill_manual(values=cols.species)
791 werner 36
## stem numbers
37
ggplot(stand.avg, aes(x=year,y=count_ha,group=species,fill=species)) + geom_area()
38
ggplot(stand.avg, aes(x=year,y=cohortCount_ha,group=species,fill=species)) + geom_area()
39
 
816 werner 40
ggplot(stand.avg, aes(x=year,y=LAI,group=species,fill=species)) + geom_area()
41
 
42
cols <- rainbow(nrow(mtcars))
43
mtcars$car <- rownames(mtcars)
44
ggplot(mtcars, aes(mpg, disp, colour = car)) + geom_point() +
45
  scale_colour_manual(limits = mtcars$car, values = cols) +
46
  guides(colour = guide_legend(ncol = 3))
47
 
48
 
49
### e.g. LAI per resource unit in the first three years
50
par(mfrow=c(1,3))
51
for (y in c(1,2,3))
52
  boxplot(tapply(stand$LAI[stand$year == y], stand$ru[stand$year == y], sum), ylim=c(0,10), main=paste("Year", y))