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