-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathadd.R
169 lines (150 loc) · 4.32 KB
/
add.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
#' Add data to a plotly visualization
#'
#' @param p a plotly visualization
#' @param data a data frame.
#' @export
#' @examples
#'
#' NULL %>% plot_ly() %>% add_data(economics) %>% add_trace(x = ~date, y = ~pce)
add_data <- function(p, data = NULL) {
if (is.null(data)) return(p)
if (!is.plotly(p)) {
stop("Don't know how to add traces to an object of class: ",
class(p), call. = FALSE)
}
id <- new_id()
p$x$visdat[[id]] <- function() data
p$x$cur_data <- id
# TODO: should this also override the data used for the most recent trace?
p
}
#' Add trace(s) to a plotly visualization
#'
#' @param p a plotly or ggplot object.
#' @param ... These arguments are documented in the references section below.
#' Note that acceptable arguments depend on the trace type.
#' @param color Either a variable name or a vector to use for color mapping.
#' @param symbol Either a variable name or a (discrete) vector to use for symbol encoding.
#' @param size A variable name or numeric vector to encode the size of markers.
#' @param linetype Either a variable name or a (discrete) vector to use for linetype encoding.
#' @param data A data frame to associate with this trace (optional). If not
#' provided, arguments are evaluated using the data frame in \code{\link{plot_ly}()}.
#' @seealso \code{\link{plot_ly}()}
#' @references \url{https://plot.ly/r/reference/}
#' @author Carson Sievert
#' @export
#' @examples
#'
#' p <- plot_ly(economics, x = ~date, y = ~uempmed)
#' p
#' p %>% add_points()
#' p %>% add_lines()
#' p %>% add_text(text = ".")
#'
#' # attributes declared in plot_ly() carry over to downstream traces
#' plot_ly(economics, x = ~date, y = ~uempmed) %>%
#' add_points(color = ~pop) %>%
#' add_lines(line = list(color = "red"))
#'
#'
add_trace <- function(p, ...,
color, symbol, size, linetype, data = NULL) {
# "native" plotly arguments
attrs <- list(...)
# tack on "special" arguments
attrs$color <- verify_arg(color)
attrs$symbol <- verify_arg(symbol)
attrs$size <- verify_arg(size)
attrs$colors <- colors
attrs$symbols <- symbols
if (!is.null(attrs[["group"]])) {
warning("The group argument has been deprecated. Use group_by() instead.")
}
p <- add_data(p, data)
# inherit attributes from the "first layer"
new_attrs <- modify_list(p$x$attrs[[1]], attrs)
p$x$attrs <- c(
p$x$attrs %||% list(),
setNames(list(new_attrs), p$x$cur_data)
)
p
}
#' Add points to a plotly vis
#'
#' @export
add_points <- function(p, ...) {
add_trace(p, type = "scatter", mode = "markers", ...)
}
#' Add lines to a plotly vis
#'
#' @export
add_lines <- function(p, ...) {
add_trace(p, type = "scatter", mode = "lines", ...)
}
#' Add text to a plotly vis
#'
#' @export
add_text <- function(p, ...) {
# TODO: throw error if no text attribute is found
add_trace(p, type = "scatter", mode = "text", ...)
}
#' Add polygons to a plotly vis
#'
#' @export
#' @examples
#'
#' library(dplyr)
#' data(canada.cities, package = "maps")
#'
#' ggplot2::map_data("world", "canada") %>%
#' group_by(group) %>%
#' plot_ly(x = ~long, y = ~lat, hoverinfo = "none") %>%
#' add_points(text = ~paste(name, "<br />", pop), hoverinfo = "text",
#' data = canada.cities) %>%
#' layout(showlegend = FALSE)
add_polygons <- function(p, ...) {
# TODO: Should mode='markers+lines'? If so, retrace first points?
add_trace(p, type = "scatter", mode = "lines", fill = "toself", ...)
}
#' Add ribbons to a plotly vis
#'
#' Ribbons are a special case of polygons.
#'
#' @export
add_ribbons <- function(p, ...) {
# TODO: add ymin, ymax arguments?
add_polygons(...)
}
# #'
# #'
# #' @export
# #' @examples
# #'
# #' x <- rnorm(10)
# #' plot_ly(x = ~x) %>%
# #' add_chull()
# add_chull <- function(p, ...) {
# stop("not yet implemented")
# ch <- chull(x, y = NULL)
# # TODO: Should mode='markers+lines'? If so, retrace first points?
# add_polygons(...)
# }
## ------------------------------------------------------------------------
## Non-trace addition
## ------------------------------------------------------------------------
#
##' @export
#add_transform <- function(p, ...) {
# stop("not yet implemented")
#}
#
#
##' @export
#add_shape <- function(p, ...) {
# stop("not yet implemented")
#}
#
##' @export
#add_annotation <- function(p, ...) {
# stop("not yet implemented")
#}