-
Notifications
You must be signed in to change notification settings - Fork 6
/
ggla_plotly_layout.R
186 lines (176 loc) · 5.59 KB
/
ggla_plotly_layout.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#' @title ggla_plotly_layout
#' @description Add default ggla layout options to a plotly plot
#' @param plot A plotly plot, created either using plot_ly or ggplotly.
#' @param gla_theme Either "default" or "inverse", Default: 'default'
#' @param x_axis_title boolean, If TRUE an x-axis label/title can be added, Default: FALSE
#' @param y_axis_title boolean, If TRUE a y-axis label/title can be added, Default: FALSE
#' @param xgridlines boolean, If TRUE, theme will include gridlines on the x-axis, Default: FALSE
#' @param legend boolean, If TRUE, theme will include a legend at the top left of the plot, Default: TRUE
#' @param base_size base font size, Default: 14
#' @param annotations Annotations settings passed onto plotly::layout(annotations = ...), Default: NULL
#' @param ... Other parameters passed to plotly::layout
#' @return A plotly plot
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @rdname ggla_plotly_layout
#' @export
#' @importFrom plotly layout
#' @import utils
ggla_plotly_layout <- function(plot, gla_theme = "default",
x_axis_title = FALSE, y_axis_title = FALSE,
xgridlines = FALSE, legend = TRUE,
base_size = 14,
annotations = NULL, ...) {
colours <- get(paste0("gla_", gla_theme))
# Text size
title_size <- base_size * (18 / 14)
label_size <- base_size * (12 / 14)
axis_size <- base_size * (12 / 14)
plot_margin <- base_size * (5 / 14)
has_title <- !is.null(plot$x$layout$title$text)
has_geom <- "geometry" %in% names(unname(plot$x$attrs)[[1]])
ggla_plotly <- plot %>%
plotly::layout(
margin = list(t = plot_margin, b = plot_margin,
r = plot_margin, l = plot_margin,
pad = 0),
plot_bgcolor = colours$background,
paper_bgcolor = colours$background,
font = list(
color = colours$`body text`,
family = "Arial",
size = base_size),
title = list(
font = list(
color = colours$headlines,
family = "Arial",
size = title_size),
x = 0,
xref = "container"),
xaxis = list(
domain = c(0, 1),
automargin = TRUE,
ticks = "",
tickcolor = NA,
ticklen = plot_margin,
tickwidth = 0,
showticklabels = TRUE,
tickfont = list(
color = colours$`axis text & labels`,
family = "Arial",
size = axis_size),
tickangle = 0,
showline = FALSE,
linecolor = colours$`light grid`,
linewidth = 2 / 3,
showgrid = xgridlines,
gridcolor = colours$`light grid`,
gridwidth = 2 / 3,
zeroline = FALSE,
anchor = "y",
title = list(
font = list(
color = colours$`axis text & labels`,
family = "Arial",
size = axis_size)),
hoverformat = ".2f"),
yaxis = list(
domain = c(0, 1),
automargin = TRUE,
type = "linear",
autorange = FALSE,
tickmode = "array",
nticks = NA,
ticks = "outside",
tickcolor = NA,
ticklen = 0,
tickwidth = 0,
showticklabels = TRUE,
tickfont = list(
color = colours$`axis text & labels`,
family = "Arial",
size = axis_size),
tickangle = 0,
showline = FALSE,
linecolor = NA,
linewidth = 0,
showgrid = TRUE,
gridcolor = colours$`light grid`,
gridwidth = 0.6224831,
zeroline = FALSE,
anchor = "x",
title = list(
font = list(
color = colours$`axis text & labels`,
family = "Arial",
size = axis_size)),
hoverformat = ".2f"),
legend = list(
bgcolor = colours$background,
bordercolor = "transparent",
borderwidth = 0,
x = 0,
y = 100,
orientation = "h",
xanchor = "left",
yanchor = "bottom",
font = list(
color = colours$`body text`,
family = "Arial",
size = label_size)),
showlegend = legend)
if (has_title) {
ggla_plotly <- ggla_plotly %>%
plotly::layout(margin = list(t = 5 * title_size))
}
if (!x_axis_title) {
ggla_plotly <- ggla_plotly %>%
plotly::layout(xaxis = list(title = list(text = NULL)))
}
if (!y_axis_title) {
ggla_plotly <- ggla_plotly %>%
plotly::layout(yaxis = list(title = list(text = NULL)))
}
if (has_geom) {
ggla_plotly <- ggla_plotly %>%
plotly::layout(
xaxis = list(
showticklabels = FALSE,
showgrid = FALSE),
yaxis = list(
showticklabels = FALSE,
showgrid = FALSE)
)
}
if (!is.null(annotations)) {
default_annotations <- list(
arrowhead = 0,
bgcolor = colours$background,
bordercolor = colours$background,
borderpad = 0,
borderwidth = 0,
showarrow = TRUE,
arrowcolor = colours$`axis text & labels`,
startarrowhead = 0,
arrowwidth = 1
)
default_font <- list(size = label_size,
family = "Arial",
color = colours$`axis text & labels`)
annotations <- utils::modifyList(default_annotations, annotations)
if ("font" %in% names(annotations)) {
annotations$font <- utils::modifyList(default_font, annotations$font)
} else {
annotations$font <- default_font
}
ggla_plotly <- ggla_plotly %>%
plotly::layout(annotations = annotations)
}
ggla_plotly <- ggla_plotly %>%
plotly::layout(...)
return(ggla_plotly)
}