forked from plotly/plotly.R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.R
More file actions
69 lines (63 loc) · 1.91 KB
/
layout.R
File metadata and controls
69 lines (63 loc) · 1.91 KB
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
#' Modify the layout of a plotly visualization
#'
#' @param p A plotly object.
#' @param ... Arguments to the layout object. For documentation,
#' see \url{https://plot.ly/r/reference/#Layout_and_layout_style_objects}
#' @param data A data frame to associate with this layout (optional). If not
#' provided, arguments are evaluated using the data frame in \code{\link{plot_ly}()}.
#' @author Carson Sievert
#' @export
layout <- function(p, ..., data = NULL) {
UseMethod("layout")
}
#' @export
layout.matrix <- function(p, ..., data = NULL) {
# workaround for the popular graphics::layout() function
# https://github.com/ropensci/plotly/issues/464
graphics::layout(p, ...)
}
#' @export
layout.plotly <- function(p, ..., data = NULL) {
p <- add_data(p, data)
attrs <- list(...)
# similar to add_trace()
p$x$layoutAttrs <- c(
p$x$layoutAttrs %||% list(),
setNames(list(attrs), p$x$cur_data)
)
p
}
#' Add a range slider to the x-axis
#'
#' @param p plotly object.
#' @param ... these arguments are documented here
#' \url{https://plot.ly/r/reference/#layout-xaxis-rangeslider}
#' @export
#' @author Carson Sievert
#' @examples
#'
#' plot_ly(x = time(USAccDeaths), y = USAccDeaths) %>%
#' add_lines() %>%
#' rangeslider()
#'
rangeslider <- function(p, ...) {
if (sum(grepl("^xaxis", names(p$x$layout))) > 1) {
stop("Can only add a rangeslider to a plot with one x-axis", call. = FALSE)
}
p$x$layout$xaxis$rangeslider <- list(visible = TRUE, ...)
p
}
#' Set the default configuration for plotly
#'
#' @param p a plotly object
#' @param ... these arguments are documented at
#' \url{https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js}
#' @author Carson Sievert
#' @export
#' @examples \dontrun{
#' config(plot_ly(), displaylogo = FALSE, modeBarButtonsToRemove = list('sendDataToCloud'))
#' }
config <- function(p, ...) {
p$x$config <- modify_list(p$x$config, list(...))
p
}