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 | 5edb9c5 | 2025-10-05 16:41:01 +0200 | [diff] [blame^] | 28 | base_family = idsBaseFontFamily, |
| 29 | style = c("default", "light", "dark", "mono"), |
| 30 | bgcolor = NULL) { |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 31 | if (!is.null(bgcolor)) { |
| 32 | warning("`bgcolor` is deprecated. Use `style` instead.") |
| 33 | style <- bgcolor |
| 34 | } |
| 35 | style <- match.arg(style) |
| 36 | bgcolor <- switch(style, |
Marc Kupietz | 5edb9c5 | 2025-10-05 16:41:01 +0200 | [diff] [blame^] | 37 | default = backgroundColor, |
| 38 | "light" = backgroundColor, |
| 39 | "mono" = backgroundColor, |
| 40 | "dark" = backgroundColorDark |
| 41 | ) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 42 | |
Marc Kupietz | 5edb9c5 | 2025-10-05 16:41:01 +0200 | [diff] [blame^] | 43 | base_family <- ids_resolve_font_family(base_family) |
| 44 | |
| 45 | ret <- theme( |
| 46 | rect = element_rect(fill = bgcolor, linetype = 0, colour = NA), |
| 47 | text = element_text(size = base_size * 0.8, family = base_family), |
| 48 | plot.title = element_text(hjust = 0.5), |
| 49 | plot.subtitle = element_text(hjust = 0.5, size = base_size * 0.9, colour = mediumContrastColor), |
| 50 | title = element_text(hjust = 0.5), |
| 51 | axis.text = element_text(size = base_size * 1.0, family = base_family), |
| 52 | axis.title = element_text(size = base_size * 1.0, family = base_family), |
| 53 | legend.title = element_text(size = base_size * 1.0, family = base_family, colour = mediumContrastColor), |
| 54 | legend.text = element_text(size = base_size * 1.0, family = base_family), |
| 55 | strip.text = element_text(size = base_size * 1.0, family = base_family), |
| 56 | axis.title.x = element_text( |
| 57 | hjust = 0.5, size = base_size * 1.0, |
| 58 | colour = mediumContrastColor |
| 59 | ), |
| 60 | axis.title.y = element_text( |
| 61 | hjust = 0.5, size = base_size * 1.0, |
| 62 | margin = margin(r = 10), |
| 63 | colour = mediumContrastColor |
| 64 | ), |
| 65 | panel.grid.major.x = element_line(linetype = "dotted", colour = lowContrastColor), |
| 66 | panel.grid.major.y = element_line(colour = lowContrastColor), |
| 67 | panel.grid.minor.y = element_blank(), |
| 68 | panel.grid.minor.x = element_blank(), |
| 69 | panel.border = element_blank(), |
| 70 | panel.background = element_blank(), |
| 71 | legend.position = "right", |
| 72 | legend.key = element_rect(fill = paste0(bgcolor, "00")) |
| 73 | ) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 74 | |
| 75 | if (style == "dark") { |
Marc Kupietz | 5edb9c5 | 2025-10-05 16:41:01 +0200 | [diff] [blame^] | 76 | ret <- (ret + theme( |
| 77 | rect = element_rect(fill = bgcolor), |
| 78 | text = element_text(colour = textColorDark), |
| 79 | plot.title = element_text(colour = highContrastColorDark), |
| 80 | plot.subtitle = element_text(colour = mediumContrastColorDark), |
| 81 | axis.title.x = element_text(colour = textColorDark), |
| 82 | axis.title.y = element_text(colour = textColorDark), |
| 83 | axis.text.x = element_text(color = textColorDark), |
| 84 | axis.text.y = element_text(color = textColorDark), |
| 85 | panel.grid.major.y = element_line(colour = lowContrastColorDark), |
| 86 | legend.title = element_text(colour = textColorDark) |
| 87 | )) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 88 | } |
Marc Kupietz | 3a65ecb | 2023-09-01 11:02:54 +0200 | [diff] [blame] | 89 | update_geom_defaults("text", list(size = 3, family = base_family)) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 90 | ret |
| 91 | } |
| 92 | |
| 93 | |
| 94 | #' IDS color and fill scales |
| 95 | #' |
| 96 | #' Colour and fill scales which use the palettes in |
| 97 | #' \code{\link{ids_pal}()} and are meant for use with |
| 98 | #' \code{\link{theme_ids}()}. |
| 99 | #' |
| 100 | #' @param palette A palette function that when called with a single integer |
| 101 | #' argument (the number of levels in the scale) returns the values that they should take. |
| 102 | #' |
| 103 | #' @importFrom ggthemes gdocs_pal |
| 104 | #' @importFrom ggplot2 discrete_scale |
| 105 | #' |
| 106 | #' @inheritDotParams ggthemes::scale_colour_gdocs |
| 107 | #' @family colour ids |
| 108 | #' @rdname scale_ids |
| 109 | #' @export |
| 110 | scale_colour_ids <- function(palette = "default", ...) { |
Marc Kupietz | 5edb9c5 | 2025-10-05 16:41:01 +0200 | [diff] [blame^] | 111 | discrete_scale("colour", "ids", ids_pal(palette = palette, ...)) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | #' @rdname scale_ids |
| 115 | #' @inheritDotParams ggthemes::scale_colour_gdocs |
| 116 | #' @importFrom ggplot2 discrete_scale |
| 117 | #' @export |
| 118 | scale_color_ids <- scale_colour_ids |
| 119 | |
| 120 | #' @rdname scale_ids |
| 121 | #' @inheritParams ggthemes::scale_fill_gdocs |
| 122 | #' @importFrom ggplot2 discrete_scale |
| 123 | #' @export |
| 124 | scale_fill_ids <- function(palette = "default", ...) { |
Marc Kupietz | 5edb9c5 | 2025-10-05 16:41:01 +0200 | [diff] [blame^] | 125 | discrete_scale("fill", "ids", ids_pal(palette = palette, ...)) |
Marc Kupietz | 5214481 | 2020-12-14 23:41:56 +0100 | [diff] [blame] | 126 | } |