# plot_funs.R
# Reusable functions to generate plots for the Global Emissions Projections Report
#library(RColorBrewer)
#' General GER theme for all plots
#'
#' @import ggplot2
#' @export
GER_theme <- function(){
GER_theme_components <- list(
theme_classic(base_size = 12),
scale_y_continuous(labels = scales::comma,breaks = scales::pretty_breaks(n=10), expand = expand_scale(mult = c(0, 0.1))), #note expand_scale only works with v3 of ggplot
ylab("Emissions (MMT CO2e)"),
#scale_fill_brewer(palette="Set1"), # maybe this should be plot-specific?
theme(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
axis.title = element_text(face = "bold"),
axis.title.x = element_blank(), # not sure this is always true...
legend.title = element_blank(),
legend.text=element_text(size=8),
plot.title = element_text(hjust = 0.5),
#axis.line.y = element_line(color="grey", size = .1),
#axis.line.x = element_line(color="grey", size = .1),
axis.text = element_text(face = "bold"),
panel.background = element_blank()
)
)
return(GER_theme_components)
}
#' Bar by-gas time series plot
#'
plot_bar_by_var_time_series <- function(data, byvar = gas) {
data <- data_annex_sep2019
byvar <- enquo(byvar)
scale_lab <- switch(
as_label(byvar),
"gas" = "gases",
"sector" = "sectors")
plot_dat <- data %>%
filter(year %in% c("1990", "2000", "2015", "2030", "2050")) %>%
mutate(year = as.character(year)) %>%
group_by(year, {{byvar}}) %>%
summarize(value = sum(value, na.rm = TRUE)) %>%
ungroup()
plot_labs <- plot_dat %>%
group_by(year) %>%
summarize(value = sum(value, na.rm = TRUE))
plot <- plot_dat %>%
ggplot(aes(x=year, y=value)) +
geom_col(mapping = aes(fill = {{byvar}}),
position = "stack",
width = 0.7,
linetype = 1,
size = 0.5,
colour = "white") +
geom_text(
mapping = aes(label = scales::comma(value)),
fontface = c("plain", "plain", "bold", "bold", "plain"),
data = plot_labs,
vjust = -0.5) +
GER_theme() +
theme(axis.ticks = element_blank()) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(
face = c("plain", "plain", "bold", "bold", "plain"))) +
plot()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.