Subversion Repositories public iLand

Rev

Rev 1054 | Rev 1217 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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