R/palette-funs.R

Defines functions scale_fill_ger ger_pal ger_cols

# RGER palette colors and functions

## Colors

# approach based on: 
# https://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2

fullRGB <- c("blue-dark" = "28 71 126", 
             "green-light" = "0 133 124", 
             "red-med" = "180 17 43", 
             "purple-light" = "155 89 182", 
             "gray-med" = "99 95 79", 
             "blue-med" = "0 93 164", 
             "brown-light" = "186 91 13", 
             "green-dark" = "12 83 84", 
             "purple-dark" = "115 47 111", 
             "brown-dark" = "104 51 8")

full_palette2 <- fullRGB %>%
  strsplit(., " ") %>%
  purrr::map_chr(~rgb(.x[1], .x[2], .x[3], maxColorValue=255))

full_palette <- unname(full_palette2)

ger_RGB <- c(
  "blue-light" = "62 117 206",
  "blue-med" = "0 93 164", 
  "blue-dark" = "28 71 126", 
  
  "green-light" = "0 133 124", 
  "green-med" = "24 110 113",
  "green-dark" = "12 83 84", 
  
  "red-light" = "229 11 94",
  "red-med" = "180 17 43", 
  "red-dark" = "138 17 43",
  
  "purple-light" = "155 89 182", 
  "purple-med" = "135 61 146",
  "purple-dark" = "115 47 111", 
  
  "gray-light" = "122 119 98",
  "gray-med" = "99 95 79", 
  "gray-dark" = "78 72 65",
  
  "brown-light" = "186 91 13", 
  "brown-med" = "136 68 12",
  "brown-dark" = "104 51 8")

#' Select Global Non-CO2 Report Colors
#' 
#'  @import purrr
#'  @import stringr
ger_colors <- ger_RGB %>%
  strsplit(., " ") %>%
  purrr::map_chr(~rgb(.x[1], .x[2], .x[3], maxColorValue=255))

ger_cols <- function(...) {
  cols <- c(...)
  
  if (is.null(cols))
    return (ger_colors)
  
  pal <- ger_colors[cols]
  if (!is.null(names(cols))) {names(pal) <- names(cols)}
  pal
}
             
ger_palettes <- list(
  `gases` = ger_cols(
    "CH4" = "blue-light",
    "N2O" = "green-med",
    "F-GHGs" = "red-dark",
    "HFCs" = "purple-med",
    "PFCs" = "gray-dark",
    "SF6" = "brown-med",
    "NF3" = "red-light"
  ),
  
  `sectors` = ger_cols(
    "Energy" = "blue-dark",
    "Industrial Processes" = "green-light",
    "Agriculture" = "red-med",
    "Waste" = "purple-light"
  ),
  
  `countries` = ger_cols(
    "United States" = "blue-med",
    "China" = "gray-dark",
    "Russia" = "red-dark",
    "Brazil" = "green-light",
    "India" = "brown-med",
    "Indonesia" = "purple-dark",
    "Saudi Arabia" = "brown-light",
    "Rest of World" = "gray-light"
  ),
  
  `countries2` = unname(full_palette),
  
  `sources` = unname(full_palette),
  
  `full` = full_palette
)

ger_pal <- function(palette = "full", reverse = FALSE, ...) {
  pal <- ger_palettes[[palette]]
  
  if (reverse) pal <- rev(pal)
  
  colorRampPalette(pal, ...)
}

scale_fill_ger <- function(palette = "full", discrete = TRUE, reverse = FALSE, ...) {
  fill_pal <- scale_fill_manual(
    palette,
    values = ger_palettes[[palette]],
    ...
    )
}
MollieCarroll/NonCO2-Figs documentation built on April 19, 2020, 6:05 p.m.