This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
/
theme_axis.R
154 lines (150 loc) · 8.05 KB
/
theme_axis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#' Override theme parameters for axis attributes
#'
#' @param fig figure to modify
#' @param which which grids to apply attributes to ("x" and/or "y")
#' @param num_minor_ticks number of minor ticks
#' @param axis_label_standoff (integer) The distance in pixels that the axis labels should be offset from the tick labels.
#' @param axis_label_text_align ('left', 'right', 'center') The text align of the axis label.
#' @param axis_label_text_alpha (numeric) The text alpha of the axis label.
#' @param axis_label_text_baseline ('top', 'middle', 'bottom', 'alphabetic', 'hanging') The text baseline of the axis label.
#' @param axis_label_text_color (color) The text color of the axis label.
#' @param axis_label_text_font (string) The text font of the axis label.
#' @param axis_label_text_font_size (string - e.g. '12pt') The text font size of the axis label.
#' @param axis_label_text_font_style ('normal', 'italic', 'bold') The text font style of the axis label.
#' @param axis_line_alpha (numeric) The line alpha of the axis line.
#' @param axis_line_cap ('butt', 'round', 'square') The line cap of the axis line.
#' @param axis_line_color (color) The line color of the axis line.
#' @param axis_line_dash The line dash of the axis line.
#' @param axis_line_dash_offset (integer) The line dash offset of the axis line.
#' @param axis_line_join ('miter', 'round', 'bevel') The line join of the axis line.
#' @param axis_line_width (integer) The line width of the axis line.
#' @param major_label_orientation ('horizontal', 'vertical', or angle in degrees) What direction the major label text should be oriented. If a number is supplied, the angle of the text is measured from horizontal.
#' @param major_label_standoff (integer) The distance in pixels that the major tick labels should be offset from the associated ticks.
#' @param major_label_text_align ('left', 'right', 'center') The text align of the major tick labels.
#' @param major_label_text_alpha (numeric) The text alpha of the major tick labels.
#' @param major_label_text_baseline ('top', 'middle', 'bottom', 'alphabetic', 'hanging') The text baseline of the major tick labels.
#' @param major_label_text_color (color) The text color of the major tick labels.
#' @param major_label_text_font (string - 'Helvetica') The text font of the major tick labels.
#' @param major_label_text_font_size (string - e.g. '12pt') The text font size of the major tick labels.
#' @param major_label_text_font_style ('normal', 'italic', 'bold') The text font style of the major tick labels.
#' @param major_tick_in (integer) The distance in pixels that major ticks should extend into the main plot area.
#' @param major_tick_line_alpha (numeric) The line alpha of the major ticks.
#' @param major_tick_line_cap ('butt', 'round', 'square') The line cap of the major ticks.
#' @param major_tick_line_color (color) The line color of the major ticks.
#' @param major_tick_line_dash The line dash of the major ticks.
#' @param major_tick_line_dash_offset (integer) The line dash offset of the major ticks.
#' @param major_tick_line_join ('miter', 'round', 'bevel') The line join of the major ticks.
#' @param major_tick_line_width (integer) The line width of the major ticks.
#' @param major_tick_out (integer) The distance in pixels that major ticks should extend out of the main plot area.
#' @param minor_tick_in (integer) The distance in pixels that minor ticks should extend into the main plot area.
#' @param minor_tick_line_alpha (numeric) The line alpha of the minor ticks.
#' @param minor_tick_line_cap ('butt', 'round', 'square') The line cap of the minor ticks.
#' @param minor_tick_line_color (color) The line color of the minor ticks.
#' @param minor_tick_line_dash The line dash of the minor ticks.
#' @param minor_tick_line_dash_offset (integer) The line dash offset of the minor ticks.
#' @param minor_tick_line_join ('miter', 'round', 'bevel') The line join of the minor ticks.
#' @param minor_tick_line_width (integer) The line width of the minor ticks.
#' @param minor_tick_out (integer) The distance in pixels that major ticks should extend out of the main plot area.
#' @param pars optionally specify a named list of all parameters - useful when dealing with theme lists
#' @example man-roxygen/ex-theme.R
#' @export
theme_axis <- function(fig,
which = c("x", "y"),
num_minor_ticks = 5,
axis_label_standoff = NULL,
axis_label_text_align = "left",
axis_label_text_alpha = 1,
axis_label_text_baseline = "bottom",
axis_label_text_color = "#444444",
axis_label_text_font = "Helvetica",
axis_label_text_font_size = "12pt",
axis_label_text_font_style = "normal",
axis_line_alpha = 1,
axis_line_cap = "butt",
axis_line_color = "black",
axis_line_dash = NULL,
axis_line_dash_offset = 0,
axis_line_join = "miter",
axis_line_width = 1,
major_label_orientation = "horizontal",
major_label_standoff = NULL,
major_label_text_align = "left",
major_label_text_alpha = 1,
major_label_text_baseline = "bottom",
major_label_text_color = "#444444",
major_label_text_font = "Helvetica",
major_label_text_font_size = "12pt",
major_label_text_font_style = "normal",
major_tick_in = NULL,
major_tick_line_alpha = 1,
major_tick_line_cap = "butt",
major_tick_line_color = "black",
major_tick_line_dash = NULL,
major_tick_line_dash_offset = 0,
major_tick_line_join = "miter",
major_tick_line_width = 1,
major_tick_out = NULL,
minor_tick_in = NULL,
minor_tick_line_alpha = 1,
minor_tick_line_cap = "butt",
minor_tick_line_color = "black",
minor_tick_line_dash = NULL,
minor_tick_line_dash_offset = 0,
minor_tick_line_join = "miter",
minor_tick_line_width = 1,
minor_tick_out = NULL,
pars = NULL
) {
# this will provide a list of all user-specified arguments
# (can ignore the defaults for the ones they don't specify
# because they are defaults if not specified in bokeh)
if (is.null(pars)) {
specified <- names(as.list(match.call())[-1])
pars <- as.list(environment())[specified]
}
num_minor_ticks <- pars$num_minor_ticks
pars <- pars[names(pars) %in% names(axis_par_validator_map)]
pars <- handle_extra_pars(pars, axis_par_validator_map)
parnames <- names(pars)
## if an axis hasn't been created yet (usually done in prepare_figure)
## then create it here and apply attributes
## could alternatively save attributes and apply in prepare_figure
if (!is.null(fig$x$modeltype) && fig$x$modeltype == "GridPlot") {
for (ii in seq_along(fig$x$spec$figs)) {
if ("x" %in% which && fig$x$spec$figs[[ii]]$x$spec$xaxes != FALSE) {
if (is.null(fig$x$spec$figs[[ii]]$x$spec$model[["x_axis"]]))
fig$x$spec$figs[[ii]] <- fig$x$spec$figs[[ii]] %>% x_axis()
for (nm in parnames)
fig$x$spec$figs[[ii]]$x$spec$model[["x_axis"]]$attributes[[nm]] <- pars[[nm]]
if (!is.null(num_minor_ticks))
fig$x$spec$figs[[ii]]$x$spec$model$x_tickformatter$attributes$num_minor_ticks <- num_minor_ticks
}
if ("y" %in% which && fig$x$spec$figs[[ii]]$x$spec$yaxes != FALSE) {
if (is.null(fig$x$spec$figs[[ii]]$x$spec$model[["y_axis"]]))
fig$x$spec$figs[[ii]] <- fig$x$spec$figs[[ii]] %>% y_axis()
for (nm in parnames)
fig$x$spec$figs[[ii]]$x$spec$model[["y_axis"]]$attributes[[nm]] <- pars[[nm]]
if (!is.null(num_minor_ticks))
fig$x$spec$figs[[ii]]$x$spec$model$y_tickformatter$attributes$num_minor_ticks <- num_minor_ticks
}
}
} else {
if ("x" %in% which && fig$x$spec$xaxes != FALSE) {
if (is.null(fig$x$spec$model[["x_axis"]]))
fig <- fig %>% x_axis()
for (nm in parnames)
fig$x$spec$model[["x_axis"]]$attributes[[nm]] <- pars[[nm]]
if (!is.null(num_minor_ticks))
fig$x$spec$model$x_tickformatter$attributes$num_minor_ticks <- num_minor_ticks
}
if ("y" %in% which && fig$x$spec$yaxes != FALSE) {
if (is.null(fig$x$spec$model[["y_axis"]]))
fig <- fig %>% y_axis()
for (nm in parnames)
fig$x$spec$model[["y_axis"]]$attributes[[nm]] <- pars[[nm]]
if (!is.null(num_minor_ticks))
fig$x$spec$model$y_tickformatter$attributes$num_minor_ticks <- num_minor_ticks
}
}
fig
}