These are some functions I use to prettify GJAMTime output. I’ll add inline examples later but for now here are some screenshots.
basicPlotTheme <- function() {
# Some basic plot attributes to make ggplot more readable
th <- theme(axis.text.x = element_text(angle=45, vjust=0.5) ,
text = element_text(size=18),
title = element_text(size=24) )
return(th)
}
extractAlphasTall <- function(gjam.output) {
# returns a tall version of interaction values, each row being one
# species-by-species interaction value. dim S^2 x 3 with columns
# "to" and "from" species, and the estimated interaction coefficient.
a <- gjam.output$parameters$alphaTable
spp <- colnames(out$modelList$effort$values)
ns <- length(spp)
pairs.df <- data.frame(to=rep(spp, each=ns),
from=rep(spp, by=ns),
est=0, logest=NA, sign=NA)
pairs <- unlist(str_split(a[,1], ', '))
to <- pairs[seq(1, length(pairs), 2)]
from <- pairs[seq(2, length(pairs), 2)]
for (i in 1:nrow(a)) {
est <- a$Estimate[i]
inx <- (pairs.df$to==to[i]) + (pairs.df$from==from[i]) == 2
pairs.df[inx, 3:ncol(pairs.df)]<- c(est, log(abs(est)), est>=0)
}
return(pairs.df)
}
extractAlphasSquare <- function(gjam.output) {
# returns the S x S matrix of species interaction values
a <- gjam.output$parameters$alphaTable
spp <- colnames(out$modelList$effort$values)
ns <- length(spp)
square <- data.frame(matrix(0, nrow=ns, ncol=ns))
colnames(square) <- rownames(square) <- spp
pairs <- unlist(str_split(a[,1], ', '))
to <- pairs[seq(1, length(pairs), 2)]
from <- pairs[seq(2, length(pairs), 2)]
for (i in 1:nrow(a)) { square[ from[i], to[i] ] <- a$Estimate[i] }
return(square)
}
alphaPlot <- function(gjam.output, is.log=F) {
# Plots species-by-species interaction matrix, with option to
# take natural log of values.
pairs.df <- extractAlphasTall(gjam.output)
title <- 'species interactions'
if (is.log) {
title <- paste("log of", title)
alpha.plot <- pairs.df %>% ggplot() + geom_tile(aes(x=to, y=from, fill=logest))
} else {
alpha.plot <- pairs.df %>% ggplot() + geom_tile(aes(x=to, y=from, fill=est))
}
alpha.plot <- alpha.plot +
ggtitle(title) +
scale_fill_distiller(type='div', palette=4) +
basicPlotTheme() + theme(axis.text.x = element_text(hjust=1, vjust=1))
alpha.plot
}
corPlot <- function(ydata) {
# Plots species correlations directly from the ydata.
cor.plot <- round(cor(ydata), 4) %>%
melt() %>%
rename(Species1="Var1", Species2="Var2", correlation='value') %>%
ggplot(aes(x=Species1, y=Species2, fill=correlation)) +
geom_tile() +
ggtitle('species correlations') +
scale_fill_gradient2(low = "white", high = "orange", midpoint = 0.5) +
basicPlotTheme() + theme(axis.text.x = element_text(hjust=1, vjust=1))
cor.plot
}
sensPlot <- function(gjam.output, is.horiz=T, is.ordered=F) {
# Plots sensitivity output from GJAMTime; options to order by sensitivity
# value or to flip axes horizontally (default) or vertically.
sens.plot <- gjam.output$parameters$sensTable %>%
mutate(covariates = gsub(':', ':\n', rownames(.)),
interaction = ifelse(grepl(':', covariates), 'true', 'false'))
if (is.ordered) {
sens.plot <- sens.plot %>%
mutate(covariates = fct_reorder(covariates, Estimate, .fun='median'))
}
sens.plot <- sens.plot %>%
ggplot(aes(x=reorder(covariates, Estimate), color=interaction)) +
geom_point(aes(y=Estimate)) +
geom_errorbar(aes(ymax=Estimate+SE, ymin=Estimate-SE), position="dodge") +
ggtitle('community sensitivity to betas') + xlab('covariate') + ylab('sensitivity estimate') +
scale_color_brewer(type='qual', palette=2) + basicPlotTheme()
if (is.horiz) adjust <- coord_flip()
else adjust <- theme(axis.text.x = element_text(hjust=1, vjust=1))
sens.plot + adjust
}