Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 1 | #' IDS Theme for ggplot2 |
| 2 | #' |
| 3 | #' Based on Highcharts ggtheme \link[ggthemes]{theme_hc} |
| 4 | #' which again is based on the plots in \url{Highcharts JS}. |
| 5 | #' |
| 6 | #' @note |
| 7 | #' |
| 8 | #' Note that here, unlike with the highcharter theme, you have to set the scale |
| 9 | #' explicitly. |
| 10 | #' |
| 11 | #' |
| 12 | #' @references |
| 13 | #' |
| 14 | #' \link[ggthemes]{theme_hc} |
| 15 | #' |
| 16 | #' \url{http://www.highcharts.com/demo/line-basic} |
| 17 | #' |
| 18 | #' \url{https://github.com/highslide-software/highcharts.com/tree/master/js/themes} |
| 19 | #' |
| 20 | #' @inheritParams ggplot2::theme_bw |
| 21 | #' @param style \code{'light'}, \code{'dark'}. |
| 22 | #' @param bgcolor Deprecated |
| 23 | #' @example inst/examples/ex-ggplot.R |
| 24 | #' @family themes ids |
| 25 | #' @importFrom ggplot2 theme element_rect element_text element_line element_blank |
| 26 | #' @export |
Marc Kupietz | 590efd0 | 2023-09-01 11:01:08 +0200 | [diff] [blame] | 27 | theme_ids <- function(base_size = 10, |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 28 | base_family = idsBaseFontFamily, |
| 29 | style = c("default", "light", "dark"), |
| 30 | bgcolor = NULL) { |
| 31 | |
| 32 | if (!is.null(bgcolor)) { |
| 33 | warning("`bgcolor` is deprecated. Use `style` instead.") |
| 34 | style <- bgcolor |
| 35 | } |
| 36 | style <- match.arg(style) |
| 37 | bgcolor <- switch(style, |
| 38 | default = backgroundColor, |
| 39 | "light" = backgroundColor, |
| 40 | "mono" = backgroundColor, |
| 41 | "dark" = backgroundColorDark) |
| 42 | |
| 43 | ret <- theme(rect = element_rect(fill = bgcolor, linetype = 0, colour = NA), |
| 44 | text = element_text(size = base_size, family = base_family), |
| 45 | plot.title = element_text(hjust = 0.5), |
| 46 | plot.subtitle = element_text(hjust = 0.5, size = 13, colour = mediumContrastColor), |
| 47 | title = element_text(hjust = 0.5), |
Marc Kupietz | 9a0a2a5 | 2021-04-27 11:46:01 +0200 | [diff] [blame] | 48 | axis.text = element_text(size = 11, family = base_family), |
| 49 | axis.title = element_text(size = 11, family = base_family), |
| 50 | legend.text = element_text(size = 11, family = base_family), |
| 51 | strip.text = element_text(size = 11, family = base_family), |
Marc Kupietz | 43e20c7 | 2021-02-09 14:58:11 +0100 | [diff] [blame] | 52 | axis.title.x = element_text(hjust = 0.5, size = 11, |
| 53 | colour = mediumContrastColor), |
| 54 | axis.title.y = element_text(hjust = 0.5, size = 11, |
| 55 | margin = margin(r = 10), |
| 56 | colour = mediumContrastColor), |
| 57 | legend.title = element_text(size=11, colour = mediumContrastColor), |
Marc Kupietz | e8d0a8e | 2020-12-28 13:36:21 +0100 | [diff] [blame] | 58 | panel.grid.major.x = element_line(linetype = "dotted", colour = lowContrastColor), |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 59 | panel.grid.major.y = element_line(colour = lowContrastColor), |
| 60 | panel.grid.minor.y = element_blank(), |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 61 | panel.grid.minor.x = element_blank(), |
| 62 | panel.border = element_blank(), |
| 63 | panel.background = element_blank(), |
| 64 | legend.position = "right", |
| 65 | legend.key = element_rect(fill = paste0(bgcolor,"00"))) |
| 66 | |
| 67 | if (style == "dark") { |
| 68 | ret <- (ret + theme(rect = element_rect(fill = bgcolor), |
| 69 | text = element_text(colour = textColorDark), |
| 70 | plot.title = element_text(colour = highContrastColorDark), |
| 71 | plot.subtitle = element_text(colour = mediumContrastColorDark), |
| 72 | axis.title.x = element_text(colour = textColorDark), |
| 73 | axis.title.y = element_text(colour = textColorDark), |
| 74 | axis.text.x = element_text(color=textColorDark), |
| 75 | axis.text.y = element_text(color=textColorDark), |
| 76 | panel.grid.major.y = element_line(colour = lowContrastColorDark), |
| 77 | legend.title = element_text(colour = textColorDark))) |
| 78 | } |
Marc Kupietz | 3a65ecb | 2023-09-01 11:02:54 +0200 | [diff] [blame^] | 79 | update_geom_defaults("text", list(size = 3, family = base_family)) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 80 | ret |
| 81 | } |
| 82 | |
| 83 | |
| 84 | #' IDS color and fill scales |
| 85 | #' |
| 86 | #' Colour and fill scales which use the palettes in |
| 87 | #' \code{\link{ids_pal}()} and are meant for use with |
| 88 | #' \code{\link{theme_ids}()}. |
| 89 | #' |
| 90 | #' @param palette A palette function that when called with a single integer |
| 91 | #' argument (the number of levels in the scale) returns the values that they should take. |
| 92 | #' |
| 93 | #' @importFrom ggthemes gdocs_pal |
| 94 | #' @importFrom ggplot2 discrete_scale |
| 95 | #' |
| 96 | #' @inheritDotParams ggthemes::scale_colour_gdocs |
| 97 | #' @family colour ids |
| 98 | #' @rdname scale_ids |
| 99 | #' @export |
| 100 | scale_colour_ids <- function(palette = "default", ...) { |
Marc Kupietz | a371c67 | 2020-12-18 15:56:15 +0100 | [diff] [blame] | 101 | discrete_scale("colour", "ids", ids_pal(palette=palette, ...)) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #' @rdname scale_ids |
| 105 | #' @inheritDotParams ggthemes::scale_colour_gdocs |
| 106 | #' @importFrom ggplot2 discrete_scale |
| 107 | #' @export |
| 108 | scale_color_ids <- scale_colour_ids |
| 109 | |
| 110 | #' @rdname scale_ids |
| 111 | #' @inheritParams ggthemes::scale_fill_gdocs |
| 112 | #' @importFrom ggplot2 discrete_scale |
| 113 | #' @export |
| 114 | scale_fill_ids <- function(palette = "default", ...) { |
Marc Kupietz | a371c67 | 2020-12-18 15:56:15 +0100 | [diff] [blame] | 115 | discrete_scale("fill", "ids", ids_pal(palette=palette, ...)) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 116 | } |