blob: fc562e37167048d1c77efb4d29b25fe4b8e3a82b [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 Kupietz5edb9c52025-10-05 16:41:01 +020028 base_family = idsBaseFontFamily,
29 style = c("default", "light", "dark", "mono"),
30 bgcolor = NULL) {
Marc Kupietz52144812020-12-14 23:41:56 +010031 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 Kupietz5edb9c52025-10-05 16:41:01 +020037 default = backgroundColor,
38 "light" = backgroundColor,
39 "mono" = backgroundColor,
40 "dark" = backgroundColorDark
41 )
Marc Kupietz52144812020-12-14 23:41:56 +010042
Marc Kupietz5edb9c52025-10-05 16:41:01 +020043 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 Kupietz52144812020-12-14 23:41:56 +010074
75 if (style == "dark") {
Marc Kupietz5edb9c52025-10-05 16:41:01 +020076 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 Kupietz52144812020-12-14 23:41:56 +010088 }
Marc Kupietz3a65ecb2023-09-01 11:02:54 +020089 update_geom_defaults("text", list(size = 3, family = base_family))
Marc Kupietz52144812020-12-14 23:41:56 +010090 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
110scale_colour_ids <- function(palette = "default", ...) {
Marc Kupietz5edb9c52025-10-05 16:41:01 +0200111 discrete_scale("colour", "ids", ids_pal(palette = palette, ...))
Marc Kupietz52144812020-12-14 23:41:56 +0100112}
113
114#' @rdname scale_ids
115#' @inheritDotParams ggthemes::scale_colour_gdocs
116#' @importFrom ggplot2 discrete_scale
117#' @export
118scale_color_ids <- scale_colour_ids
119
120#' @rdname scale_ids
121#' @inheritParams ggthemes::scale_fill_gdocs
122#' @importFrom ggplot2 discrete_scale
123#' @export
124scale_fill_ids <- function(palette = "default", ...) {
Marc Kupietz5edb9c52025-10-05 16:41:01 +0200125 discrete_scale("fill", "ids", ids_pal(palette = palette, ...))
Marc Kupietz52144812020-12-14 23:41:56 +0100126}