blob: 67f06c3fbbb6de83082ec3957d241abb79924b78 [file] [log] [blame]
Marc Kupietz52144812020-12-14 23:41:56 +01001#' 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 Kupietz590efd02023-09-01 11:01:08 +020027theme_ids <- function(base_size = 10,
Marc Kupietz52144812020-12-14 23:41:56 +010028 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),
Marc Kupietz7c171302023-09-01 11:05:18 +020044 text = element_text(size = base_size * 0.8, family = base_family),
Marc Kupietz52144812020-12-14 23:41:56 +010045 plot.title = element_text(hjust = 0.5),
Marc Kupietz7c171302023-09-01 11:05:18 +020046 plot.subtitle = element_text(hjust = 0.5, size = base_size * 0.9, colour = mediumContrastColor),
Marc Kupietz52144812020-12-14 23:41:56 +010047 title = element_text(hjust = 0.5),
Marc Kupietz7c171302023-09-01 11:05:18 +020048 axis.text = element_text(size = base_size * 1.0, family = base_family),
49 axis.title = element_text(size = base_size * 1.0, family = base_family),
50 legend.title = element_text(size = base_size * 1.0, family = base_family, colour = mediumContrastColor),
51 legend.text = element_text(size = base_size * 1.0, family = base_family),
52 strip.text = element_text(size = base_size * 1.0, family = base_family),
53 axis.title.x = element_text(hjust = 0.5, size = base_size * 1.0,
Marc Kupietz43e20c72021-02-09 14:58:11 +010054 colour = mediumContrastColor),
Marc Kupietz7c171302023-09-01 11:05:18 +020055 axis.title.y = element_text(hjust = 0.5, size = base_size * 1.0,
Marc Kupietz43e20c72021-02-09 14:58:11 +010056 margin = margin(r = 10),
57 colour = mediumContrastColor),
Marc Kupietze8d0a8e2020-12-28 13:36:21 +010058 panel.grid.major.x = element_line(linetype = "dotted", colour = lowContrastColor),
Marc Kupietz52144812020-12-14 23:41:56 +010059 panel.grid.major.y = element_line(colour = lowContrastColor),
60 panel.grid.minor.y = element_blank(),
Marc Kupietz52144812020-12-14 23:41:56 +010061 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 Kupietz3a65ecb2023-09-01 11:02:54 +020079 update_geom_defaults("text", list(size = 3, family = base_family))
Marc Kupietz52144812020-12-14 23:41:56 +010080 ret
Marc Kupietz7c171302023-09-01 11:05:18 +020081
Marc Kupietz52144812020-12-14 23:41:56 +010082}
83
84
85#' IDS color and fill scales
86#'
87#' Colour and fill scales which use the palettes in
88#' \code{\link{ids_pal}()} and are meant for use with
89#' \code{\link{theme_ids}()}.
90#'
91#' @param palette A palette function that when called with a single integer
92#' argument (the number of levels in the scale) returns the values that they should take.
93#'
94#' @importFrom ggthemes gdocs_pal
95#' @importFrom ggplot2 discrete_scale
96#'
97#' @inheritDotParams ggthemes::scale_colour_gdocs
98#' @family colour ids
99#' @rdname scale_ids
100#' @export
101scale_colour_ids <- function(palette = "default", ...) {
Marc Kupietza371c672020-12-18 15:56:15 +0100102 discrete_scale("colour", "ids", ids_pal(palette=palette, ...))
Marc Kupietz52144812020-12-14 23:41:56 +0100103}
104
105#' @rdname scale_ids
106#' @inheritDotParams ggthemes::scale_colour_gdocs
107#' @importFrom ggplot2 discrete_scale
108#' @export
109scale_color_ids <- scale_colour_ids
110
111#' @rdname scale_ids
112#' @inheritParams ggthemes::scale_fill_gdocs
113#' @importFrom ggplot2 discrete_scale
114#' @export
115scale_fill_ids <- function(palette = "default", ...) {
Marc Kupietza371c672020-12-18 15:56:15 +0100116 discrete_scale("fill", "ids", ids_pal(palette=palette, ...))
Marc Kupietz52144812020-12-14 23:41:56 +0100117}