#' @title Plots a stacked barchart
#' @description TODO: this
#'
#' @param by X-variable, either 'section' or 'ftype'
#' @param mort_df data to plot, can be NULL for package data
#' @param drop_no_mort Flag to drop 'no mortality' plots
#' @param split Split plot into background/outlier
#' @param clear_plots Flag to clear previous plots
#' @param excl_dom_ftype Flag to drop 'dominant' ftypes, see details
#'
#' @details
#'
#' Dominant ftypes (n plots > 400):
#'
#' Douglas-fir, Ponderosa pine, Lodgepole pine, Pinyon / juniper woodland,
#' California mixed conifer, Engelmann spruce / subalpine fir, Aspen
#'
#'
#' @export
#' @examples PlotStackedBarChart()
PlotStackedBarChart <- function(by, mort_df = NULL,
drop_no_mort = F, split = F, clear_plots = F,
excl_dom_ftype = F) {
if (!(by %in% c('ftype', 'section'))) stop ('by must be ftype or section')
# Setup
if (is.null(mort_df)) {
pkg_df <- PrepPlotEnvir(ret = 'plots', clear = clear_plots)
} else {
pkg_df <- mort_df
PrepPlotEnvir(ret = F, clear = clear_plots)
}
ytag <- '10 Year Mortality Rate'
if (by == 'section') {
by_var <- 'Section'
FUN_df <- pkg_df[, c('Cleland_section', 'dominant_AGENTCD', 'percent_AGENTCD',
'mort_outlier')]
FUN_df[, 5] <- as.factor(KeyClelandCode(FUN_df$Cleland_section, lvl = 'section'))
if (excl_dom_ftype) warning('Ignoring excl_dom_ftype flag')
} else if (by == 'ftype') {
by_var <- 'Forest_Type'
mod_df <- BinUncommonForestType(mort_df = pkg_df)
message('Forest types binned into Other:')
print(names(mod_df[[2]]))
cat('\n')
message('Total counts:')
cat(sum(mod_df[[2]]), '\n')
pkg_df <- mod_df[[1]]
FUN_df <- pkg_df[, c('forest_type', 'dominant_AGENTCD', 'percent_AGENTCD',
'mort_outlier')]
#FUN_df <- SubDominantForestType(mort_df = FUN_df, sub = F, n_plot_cutoff = 400)
FUN_df[, 5] <- FUN_df[, 1]
}
colnames(FUN_df)[5] <- c(by_var)
if (drop_no_mort) {
FUN_df <- FUN_df[-which(FUN_df$dominant_AGENTCD == 'No Mortality'), ]
}
full_df <- FUN_df
high_df <- FUN_df[FUN_df$mort_outlier, ]
back_df <- FUN_df[!FUN_df$mort_outlier, ]
# Return code:
if (split) {
lab1 <- "Dominant\nBackground\nMortality\nAgent"
lab2 <- "Dominant\nOutlier\nMortality\nAgent"
s1 <- PlotStacked(back_df, by = by_var,
llab = lab1, bar_fill = 'dominant_AGENTCD',
yylab = '')
s2 <- PlotStacked(high_df, by = by_var,
llab = lab2, bar_fill = 'dominant_AGENTCD',
yylab = 'Number of plots')
Multiplot(s1, s2)
} else {
lab0 <- "Dominant\nMortality\nAgent"
s0 <- PlotStacked(full_df, by = by_var,
llab = lab0, bar_fill = 'dominant_AGENTCD',
yylab = 'Number of plots')
print(s0)
}
invisible()
}
#' @describeIn PlotStackedBarChart Wrapper for dominant ftype stacked bar
#' @family plot_wrappers
#' @export
PlotStackedDomForType <- function() {
mort_df <- SubDominantForestType(sub = T)
PlotStackedBarChart(by = 'ftype', mort_df = mort_df,
drop_no_mort = T, split = T, clear_plots = T)
invisible()
}
#' @describeIn PlotStackedBarChart Wrapper for non-dominant ftype stacked bar
#' @family plot_wrappers
#' @export
PlotStackedUncommForType <- function() {
mort_df <- SubDominantForestType(sub = F)
PlotStackedBarChart(mort_df = mort_df, by = 'ftype',
drop_no_mort = T, split = T, clear_plots = T)
invisible()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.