R/figure3.1.R

Defines functions figure3.1

Documented in figure3.1

#' @title Create Figure 3.1
#'
#' @description Creates Figure 3.1 from
#'   (\url{https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/544103/DCMS_Sectors_Economic_Estimates_-_August_2016.pdf})
#'   using \code{ggplot2}.
#'
#' @details \code{figure3.1} takes as input a standardised long format GVA data
#'   frame, and uses \code{ggplot} and \code{govstyle} to format a pretty(ish) plot.
#' @param ... Passes arguments to \code{ggplot}.
#'
#' @param x Object of class \code{year_sector_data()}.
#'
#' @return Figure 3.1
#'
#' @examples
#'
#' library(eesectors)
#'
#' gva <- year_sector_data(GVA_by_sector_2016)
#' figure3.1(gva)
#'
#' @export

# Define as a method
figure3.1 <- function(x, ...) {

  out <- tryCatch(
    expr = {

      # Extract the UK GVA
      sectors_set <- x$sectors_set
      x <- dplyr::filter_(x$df, ~sector != 'UK')
      x <- dplyr::mutate_(x, year = ~factor(year, levels=c(2016:2010)))

      # Convert to long form of sector, and arrange factor levels for plot

      x$sector <- factor(
        x = unname(sectors_set[as.character(x$sector)]),
        levels = rev(as.character(unname(sectors_set[levels(x$sector)])))
        )

      p <- ggplot2::ggplot(x) +
        ggplot2::aes_(
          y = ~GVA,
          x = ~sector,
          fill = ~year
          ) +
        ggplot2::geom_bar(
          colour = 'slategray',
          position = 'dodge',
          stat = 'identity'
          ) +
        ggplot2::coord_flip() +
        govstyle::theme_gov(base_colour = 'black') +
        ggplot2::scale_fill_brewer(
          palette = 'Blues'
          ) +
        ggplot2::ylab('Gross Value Added (\u00a3bn)') +
        ggplot2::theme(
	  legend.position = 'right',
	  legend.key = ggplot2::element_blank()
	  ) +
        ggplot2::scale_y_continuous(labels = scales::comma)

      return(p)

      })
}
ukgovdatascience/eesectors documentation built on Sept. 11, 2020, 12:19 p.m.