#' Plot rates by age and sex structure, faceted by financial year.
#'
#' Each collection is plotted by age and sex. This function provides a quick way of doing so.
#' Although the function takes a single argument (a data frame containing rates by age and sex, by financial year),
#' it relies on the presence of the following variables to that data frame, which must be precisely named:
#' \describe{
#' \item{\code{age_group_new}}{A variable giving the age group generated by \code{\link{mandatory_age_group}}}
#' \item{\code{rate}}{The rate per 100,000 population}
#' \item{\code{sex}}{Sex variable, to allow side-by-side bars}
#' \item{\code{fyear6}}{Financial year}
#' }
#' @param x A dataframe as described above.
#' @param collection An optional parameter. Specify "cdi" for CDI age and sex structure, otherwise omit.
#' @return A ggplot object
#' @examples
#' \dontrun{
#' # This isn't pretty but does show how the plot is expected to work.
#' dat <- structure(list(fyear6 = c(201011L, 201011L, 201011L, 201011L,
#' 201011L, 201011L, 201011L, 201011L, 201011L, 201011L, 201011L,
#' 201011L, 201011L, 201011L, 201516L, 201516L, 201516L, 201516L,
#' 201516L, 201516L, 201516L, 201516L, 201516L, 201516L, 201516L,
#' 201516L, 201516L, 201516L),
#' age_group_new = c("<1", "1-14", "15-44", "45-64", "65-74", "75-54", ">=85",
#' "<1", "1-14", "15-44", "45-64", "65-74", "75-54", ">=85",
#' "<1", "1-14", "15-44", "45-64", "65-74", "75-54", ">=85",
#' "<1", "1-14", "15-44", "45-64", "65-74", "75-54", ">=85"),
#' sex = c("Female", "Female", "Female", "Female", "Female", "Female", "Female",
#' "Male", "Male", "Male", "Male", "Male", "Male", "Male", "Female",
#' "Female", "Female", "Female", "Female", "Female", "Female",
#' "Male", "Male", "Male", "Male", "Male", "Male", "Male"),
#' rate = c(0.633245773, 0.885480457, 0.064219518, 0.287795718, 0.804223429,
#' 0.679864585, 0.358524629, 0.08131503, 0.092019692, 0.594467264,
#' 0.487193358, 0.870376164, 0.32395407, 0.466408567, 0.396977702,
#' 0.130634269, 0.228873901, 0.408729264, 0.526498187, 0.787985839,
#' 0.420864088, 0.33889444, 0.592512904, 0.794665159, 0.376888279,
#' 0.189044191, 0.574355996, 0.500617646)),
#' .Names = c("fyear6", "age_group_new", "sex", "rate"), class = "data.frame",
#' row.names = c(NA, -28L))
#'
#' p <- aec_age_sex_plot(dat)
#' p
#' dat <- dat[dat$age_group_new != "<1", ]
#' p <- aec_age_sex_plot(dat, "cdi")
#' p
#' }
#'
#' @export
aec_age_sex_plot <- function(x, collection = NULL){
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 needed for this function to work. Please install it.",
call. = FALSE)
}
if(missing(collection) == TRUE){
ggplot2::ggplot(data = x,
ggplot2::aes(x = age_group_new, y = rate, group = sex)) +
ggplot2::geom_bar(ggplot2::aes(fill = sex), stat = "identity", position = "dodge") +
ggplot2::facet_wrap( ~ fyear6) +
ggplot2::scale_x_discrete("Age group (years)",
labels = c("<1", "1-14", "15-44", "45-64", "65-74", "75-84",
expression(phantom(x) >=85)
)
) +
ggplot2::scale_y_continuous("Rate, per 100,000 population",
labels = scales::comma) +
ggplot2::scale_fill_manual("Sex", values = c("#9ECAE1", "#3182BD")) +
ggplot2::theme(#legend.position = c(0.9, 1), legend.justification = c(1,1),
legend.position = "bottom",
strip.background = ggplot2::element_rect(fill="white"),
strip.text.x = ggplot2::element_text(face = "bold"),
panel.margin = ggplot2::unit(1.5, "lines")
) +
ggplot2::guides(fill = ggplot2::guide_legend(nrow = 1)) +
cowplot::theme_cowplot()
}else{
if(collection == "cdi"){
ggplot2::ggplot(data = x,
ggplot2::aes(x = age_group_new, y = rate, group = sex)) +
ggplot2::geom_bar(ggplot2::aes(fill = sex), stat = "identity", position = "dodge") +
ggplot2::facet_wrap( ~ fyear6) +
ggplot2::scale_x_discrete("Age group (years)",
labels = c("2-14", "15-44", "45-64", "65-74", "75-84",
expression(phantom(x) >=85)
)
) +
ggplot2::scale_y_continuous("Rate, per 100,000 population",
labels = scales::comma) +
ggplot2::scale_fill_manual("Sex", values = c("#9ECAE1", "#3182BD")) +
ggplot2::theme(#legend.position = c(0.9,1),
legend.position = "bottom",
#legend.justification = c(1,1),
strip.background = ggplot2::element_rect(fill="white"),
strip.text.x = ggplot2::element_text(face = "bold"),
panel.spacing = ggplot2::unit(1.5, "lines")
) +
ggplot2::guides(fill = ggplot2::guide_legend(nrow = 1)) +
cowplot::theme_cowplot()
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.