Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0dde7f0

Browse files
committedMar 21, 2013
updated plot.variance.decomposition and plot.sensitivity to use new ggplot2 v >= 0.9.1 syntax. refs redmine #1293, #1376
1 parent c47cc44 commit 0dde7f0

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed
 

‎modules/uncertainty/R/plots.R

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
##' do.call(grid.arrange, c(plot.variance.decomposition(x), ncol = 4))
3737
plot.variance.decomposition <- function(plot.inputs,
3838
fontsize = list(title = 18, axis = 14)){
39+
require(ggmap)
40+
theme_set(theme_classic() +
41+
theme(axis.text.x = element_text(size=fontsize$axis, vjust = -1),
42+
axis.text.y = element_blank(), axis.ticks = element_blank(), axis.line = element_blank(),
43+
axis.title.x = element_blank(),
44+
axis.title.y = element_blank(),
45+
panel.grid.minor = element_blank(),
46+
panel.border = element_blank()))
47+
3948
traits <- names(plot.inputs$variances)
4049
units <- as.character(trait.lookup(traits)$units)
4150
trait.labels <- as.character(trait.lookup(traits)$figid)
@@ -51,38 +60,28 @@ plot.variance.decomposition <- function(plot.inputs,
5160
decreasing = FALSE), ]
5261

5362
base.plot <- ggplot(plot.data) +
54-
coord_flip() +
55-
theme_bw() +
56-
opts(axis.text.x = theme_text(size=fontsize$axis, vjust = -1),
57-
axis.text.y = theme_blank(),
58-
axis.title.x = theme_blank(),
59-
axis.title.y = theme_blank(),
60-
panel.grid.minor = theme_blank(),
61-
panel.border = theme_blank())
62-
63-
trait.plot <- base.plot +
64-
opts(title = 'Parameter',
65-
plot.title = theme_text(hjust = 0.96, size = fontsize$title),
66-
axis.text.x = theme_text(colour='white'),
67-
axis.line.x = theme_blank()) +
68-
geom_text(aes(y = 1, x = points,
69-
label=trait.labels, hjust = 1),
70-
size = fontsize$axis/3) +
71-
scale_y_continuous( breaks = c(0,0), limits = c(0,1))
72-
73-
cv.plot <- base.plot +
74-
opts(title = 'CV (%)', plot.title = theme_text(size = fontsize$title)) +
75-
geom_pointrange(aes(x = points, y = coef.vars, ymin = 0, ymax = coef.vars),
76-
size = 1.25)
63+
coord_flip()
64+
65+
66+
trait.plot <- base.plot + ggtitle("Parameter") +
67+
geom_text(aes(y = 1, x = points,
68+
label=trait.labels, hjust = 1),
69+
size = fontsize$axis/3) +
70+
scale_y_continuous( breaks = c(0,0), limits = c(0,1)) +
71+
theme(axis.text.x = element_blank())
72+
cv.plot <- base.plot + ggtitle("CV (%)") +
73+
geom_pointrange(aes(x = points, y = coef.vars, ymin = 0, ymax = coef.vars),
74+
size = 1.25) +
75+
theme(plot.title = element_text(size = fontsize$title))
76+
7777

78-
el.plot <- base.plot +
79-
opts(title = 'Elasticity', plot.title = theme_text(size = fontsize$title)) +
78+
el.plot <- base.plot + ggtitle("Elasticity") +
79+
theme(plot.title = element_text(size = fontsize$title)) +
8080
geom_pointrange(aes(x = points, y = elasticities, ymin = 0, ymax = elasticities),
8181
size = 1.25)
8282

83-
pv.plot <- base.plot+
84-
opts(title = 'Root Variance (Mg/ha)',
85-
plot.title = theme_text(size = fontsize$title)) +
83+
pv.plot <- base.plot + ggtitle("SD Explained (Mg/ha)") +
84+
theme(plot.title = element_text(size = fontsize$title)) +
8685
geom_pointrange(aes(x = points, sqrt(variances),
8786
ymin = 0, ymax = sqrt(variances)), size = 1.25)
8887

@@ -146,14 +145,13 @@ plot.sensitivity <- function(sa.sample, sa.spline, trait,
146145
#indicate median with larger point
147146
geom_point(aes(x,y), data = data.frame(x = sa.sample[median.i], y = sa.spline(sa.sample[median.i])), size = dotsize * 1.3) +
148147
scale_y_continuous(limits = range(pretty(y.range)), breaks = pretty(y.range, n = 3)[1:3]) +
149-
theme_bw() +
150-
opts(title= trait.lookup(trait)$figid,
151-
axis.text.x = theme_text(size = fontsize$axis),
152-
axis.text.y = theme_text(size = fontsize$axis),
153-
axis.title.x = theme_text(size = fontsize$axis),
154-
axis.title.y = theme_blank(),
155-
plot.title = theme_text(size = fontsize$title),
156-
panel.border = theme_blank())
148+
theme_bw() + ggtitle(trait.lookup(trait)$figid)
149+
theme(axis.text.x = element_text(size = fontsize$axis),
150+
axis.text.y = element_text(size = fontsize$axis),
151+
axis.title.x = element_text(size = fontsize$axis),
152+
axis.title.y = element_blank(),
153+
plot.title = element_text(size = fontsize$title),
154+
panel.border = element_blank())
157155
## Following conditional can be removed to only plot posterior sa
158156
prior.x <- post.x
159157
if(!is.null(prior.sa.sample) & !is.null(prior.sa.spline)){
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test_that("plot.sensitivity.analysis works",{
2+
sa.sample <- 1:7
3+
sa.splinefun <- splinefun(1:10,10:1)
4+
trait <- "foo"
5+
sa.plot <- plot.sensitivity(sa.sample, sa.splinefun, trait)
6+
expect_true("ggplot" %in% class(sa.plot))
7+
8+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test_that("plot.variance.decomposition runs without returning errors", {
2+
traits <- c("mort2", "fineroot2leaf", "root_turnover_rate")
3+
sa.results <- structure(list(coef.vars = structure(c(0.1, 0.97, 2), .Names = traits),
4+
elasticities = structure(c(-1, 0, 1), .Names = traits),
5+
variances = structure(c(1, 2, 3), .Names = traits),
6+
.Names = c("coef.vars", "elasticities", "variances")))
7+
plots <- plot.variance.decomposition(sa.results)
8+
expect_equal(names(plots), c("trait.plot", "cv.plot", "el.plot", "pv.plot"))
9+
expect_true(all(grepl("ggplot", lapply(plots, class))))
10+
})
11+

1 commit comments

Comments
 (1)

dlebauer commented on Aug 14, 2014

@dlebauer
MemberAuthor

Instructions for updating ggplot functions http://stackoverflow.com/a/21173744/199217

Please sign in to comment.