Skip to content

Commit

Permalink
updated plot.variance.decomposition and plot.sensitivity to use new g…
Browse files Browse the repository at this point in the history
…gplot2 v >= 0.9.1 syntax. refs redmine #1293, #1376
  • Loading branch information
dlebauer committed Mar 21, 2013
1 parent c47cc44 commit 0dde7f0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
70 changes: 34 additions & 36 deletions modules/uncertainty/R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
##' do.call(grid.arrange, c(plot.variance.decomposition(x), ncol = 4))
plot.variance.decomposition <- function(plot.inputs,
fontsize = list(title = 18, axis = 14)){
require(ggmap)
theme_set(theme_classic() +
theme(axis.text.x = element_text(size=fontsize$axis, vjust = -1),
axis.text.y = element_blank(), axis.ticks = element_blank(), axis.line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()))

traits <- names(plot.inputs$variances)
units <- as.character(trait.lookup(traits)$units)
trait.labels <- as.character(trait.lookup(traits)$figid)
Expand All @@ -51,38 +60,28 @@ plot.variance.decomposition <- function(plot.inputs,
decreasing = FALSE), ]

base.plot <- ggplot(plot.data) +
coord_flip() +
theme_bw() +
opts(axis.text.x = theme_text(size=fontsize$axis, vjust = -1),
axis.text.y = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank())

trait.plot <- base.plot +
opts(title = 'Parameter',
plot.title = theme_text(hjust = 0.96, size = fontsize$title),
axis.text.x = theme_text(colour='white'),
axis.line.x = theme_blank()) +
geom_text(aes(y = 1, x = points,
label=trait.labels, hjust = 1),
size = fontsize$axis/3) +
scale_y_continuous( breaks = c(0,0), limits = c(0,1))

cv.plot <- base.plot +
opts(title = 'CV (%)', plot.title = theme_text(size = fontsize$title)) +
geom_pointrange(aes(x = points, y = coef.vars, ymin = 0, ymax = coef.vars),
size = 1.25)
coord_flip()


trait.plot <- base.plot + ggtitle("Parameter") +
geom_text(aes(y = 1, x = points,
label=trait.labels, hjust = 1),
size = fontsize$axis/3) +
scale_y_continuous( breaks = c(0,0), limits = c(0,1)) +
theme(axis.text.x = element_blank())
cv.plot <- base.plot + ggtitle("CV (%)") +
geom_pointrange(aes(x = points, y = coef.vars, ymin = 0, ymax = coef.vars),
size = 1.25) +
theme(plot.title = element_text(size = fontsize$title))


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

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

Expand Down Expand Up @@ -146,14 +145,13 @@ plot.sensitivity <- function(sa.sample, sa.spline, trait,
#indicate median with larger point
geom_point(aes(x,y), data = data.frame(x = sa.sample[median.i], y = sa.spline(sa.sample[median.i])), size = dotsize * 1.3) +
scale_y_continuous(limits = range(pretty(y.range)), breaks = pretty(y.range, n = 3)[1:3]) +
theme_bw() +
opts(title= trait.lookup(trait)$figid,
axis.text.x = theme_text(size = fontsize$axis),
axis.text.y = theme_text(size = fontsize$axis),
axis.title.x = theme_text(size = fontsize$axis),
axis.title.y = theme_blank(),
plot.title = theme_text(size = fontsize$title),
panel.border = theme_blank())
theme_bw() + ggtitle(trait.lookup(trait)$figid)
theme(axis.text.x = element_text(size = fontsize$axis),
axis.text.y = element_text(size = fontsize$axis),
axis.title.x = element_text(size = fontsize$axis),
axis.title.y = element_blank(),
plot.title = element_text(size = fontsize$title),
panel.border = element_blank())
## Following conditional can be removed to only plot posterior sa
prior.x <- post.x
if(!is.null(prior.sa.sample) & !is.null(prior.sa.spline)){
Expand Down
8 changes: 8 additions & 0 deletions modules/uncertainty/inst/tests/test.plot.sensitivity.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("plot.sensitivity.analysis works",{
sa.sample <- 1:7
sa.splinefun <- splinefun(1:10,10:1)
trait <- "foo"
sa.plot <- plot.sensitivity(sa.sample, sa.splinefun, trait)
expect_true("ggplot" %in% class(sa.plot))

})
11 changes: 11 additions & 0 deletions modules/uncertainty/inst/tests/test.plot.variance.decomposition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("plot.variance.decomposition runs without returning errors", {
traits <- c("mort2", "fineroot2leaf", "root_turnover_rate")
sa.results <- structure(list(coef.vars = structure(c(0.1, 0.97, 2), .Names = traits),
elasticities = structure(c(-1, 0, 1), .Names = traits),
variances = structure(c(1, 2, 3), .Names = traits),
.Names = c("coef.vars", "elasticities", "variances")))
plots <- plot.variance.decomposition(sa.results)
expect_equal(names(plots), c("trait.plot", "cv.plot", "el.plot", "pv.plot"))
expect_true(all(grepl("ggplot", lapply(plots, class))))
})

1 comment on commit 0dde7f0

@dlebauer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.