@@ -33,24 +33,115 @@ add_data <- function(p, data = NULL) {
3333# ' @param xend "final" x position (in this context, x represents "start")
3434# ' @param yend "final" y position (in this context, y represents "start")
3535# ' @seealso [plot_ly()]
36- # ' @references \url{https://plot.ly/r/reference/}
36+ # ' @references \url{http://plotly-book.cpsievert.me/the-plotly-cookbook.html}
37+ # '
38+ # ' \url{https://plot.ly/r}
39+ # '
40+ # ' \url{https://plot.ly/r/reference/}
3741# ' @author Carson Sievert
3842# ' @export
3943# ' @rdname add_trace
4044# ' @examples
4145# '
46+ # ' # the `plot_ly()` function initiates an object, and if no trace type
47+ # ' # is specified, it sets a sensible default
4248# ' p <- plot_ly(economics, x = ~date, y = ~uempmed)
4349# ' p
44- # ' p %>% add_markers()
45- # ' p %>% add_lines()
46- # ' p %>% add_text(text = ".")
4750# '
48- # ' # attributes declared in plot_ly() carry over to downstream traces,
49- # ' # but can be overwritten
50- # ' plot_ly(economics, x = ~date, y = ~uempmed, color = I("red")) %>%
51+ # ' # some `add_*()` functions are a specific case of a trace type
52+ # ' # for example, `add_markers()` is a scatter trace with mode of markers
53+ # ' add_markers(p)
54+ # '
55+ # ' # scatter trace with mode of text
56+ # ' add_text(p, text = "%")
57+ # '
58+ # ' # scatter trace with mode of lines
59+ # ' add_paths(p)
60+ # '
61+ # ' # like `add_paths()`, but ensures points are connected according to `x`
62+ # ' add_lines(p)
63+ # '
64+ # ' # if you prefer to work with plotly.js more directly, can always
65+ # ' # use `add_trace()` and specify the type yourself
66+ # ' add_trace(p, type = "scatter", mode = "markers+lines")
67+ # '
68+ # ' # mappings provided to `plot_ly()` are "global", but can be overwritten
69+ # ' plot_ly(economics, x = ~date, y = ~uempmed, color = I("red"), showlegend = FALSE) %>%
5170# ' add_lines() %>%
52- # ' add_markers(color = ~pop) %>%
53- # ' layout(showlegend = FALSE)
71+ # ' add_markers(color = ~pop)
72+ # '
73+ # ' # a number of `add_*()` functions are special cases of the scatter trace
74+ # ' plot_ly(economics, x = ~date) %>%
75+ # ' add_ribbons(ymin = ~pce - 1e3, ymax = ~pce + 1e3)
76+ # '
77+ # ' # use `group_by()` (or `group2NA()`) to apply visual mapping
78+ # ' # once per group (e.g. one line per group)
79+ # ' txhousing %>%
80+ # ' group_by(city) %>%
81+ # ' plot_ly(x = ~date, y = ~median) %>%
82+ # ' add_lines(color = I("black"))
83+ # '
84+ # ' \dontrun{
85+ # ' # use `add_sf()` or `add_polygons()` to create geo-spatial maps
86+ # ' # http://blog.cpsievert.me/2018/03/30/visualizing-geo-spatial-data-with-sf-and-plotly/
87+ # ' if (requireNamespace("sf", quietly = TRUE)) {
88+ # ' nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
89+ # ' plot_ly() %>% add_sf(data = nc)
90+ # ' }
91+ # '
92+ # ' # univariate summary statistics
93+ # ' plot_ly(mtcars, x = ~factor(vs), y = ~mpg) %>%
94+ # ' add_boxplot()
95+ # ' plot_ly(mtcars, x = ~factor(vs), y = ~mpg) %>%
96+ # ' add_trace(type = "violin")
97+ # '
98+ # ' # `add_histogram()` does binning for you...
99+ # ' mtcars %>%
100+ # ' plot_ly(x = ~factor(vs)) %>%
101+ # ' add_histogram()
102+ # '
103+ # ' # ...but you can 'pre-compute' bar heights in R
104+ # ' mtcars %>%
105+ # ' dplyr::count(vs) %>%
106+ # ' plot_ly(x = ~vs, y = ~n) %>%
107+ # ' add_bars()
108+ # '
109+ # ' # the 2d analogy of add_histogram() is add_histogram2d()/add_histogram2dcontour()
110+ # ' library(MASS)
111+ # ' (p <- plot_ly(geyser, x = ~waiting, y = ~duration))
112+ # ' add_histogram2d(p)
113+ # ' add_histogram2dcontour(p)
114+ # '
115+ # ' # the 2d analogy of add_bars() is add_heatmap()/add_contour()
116+ # ' # (i.e., bin counts must be pre-specified)
117+ # ' den <- kde2d(geyser$waiting, geyser$duration)
118+ # ' p <- plot_ly(x = den$x, y = den$y, z = den$z)
119+ # ' add_heatmap(p)
120+ # ' add_contour(p)
121+ # '
122+ # ' # `add_table()` makes it easy to map a data frame to the table trace type
123+ # ' plot_ly(economics) %>%
124+ # ' add_table()
125+ # '
126+ # ' # pie charts!
127+ # ' ds <- data.frame(labels = c("A", "B", "C"), values = c(10, 40, 60))
128+ # ' plot_ly(ds, labels = ~labels, values = ~values) %>%
129+ # ' add_pie() %>%
130+ # ' layout(title = "Basic Pie Chart using Plotly")
131+ # '
132+ # ' data(wind)
133+ # ' plot_ly(wind, r = ~r, t = ~t) %>%
134+ # ' add_area(color = ~nms) %>%
135+ # ' layout(radialaxis = list(ticksuffix = "%"), orientation = 270)
136+ # '
137+ # ' # ------------------------------------------------------------
138+ # ' # 3D chart types
139+ # ' # ------------------------------------------------------------
140+ # ' plot_ly(z = ~volcano) %>%
141+ # ' add_surface()
142+ # ' plot_ly(x = c(0, 0, 1), y = c(0, 1, 0), z = c(0, 0, 0)) %>%
143+ # ' add_mesh()
144+ # ' }
54145# '
55146add_trace <- function (p , ... ,
56147 data = NULL , inherit = TRUE ) {
@@ -143,11 +234,6 @@ add_paths <- function(p, x = NULL, y = NULL, z = NULL, ...,
143234# ' @inheritParams add_trace
144235# ' @rdname add_trace
145236# ' @export
146- # ' @examples
147- # ' txhousing %>%
148- # ' group_by(city) %>%
149- # ' plot_ly(x = ~date, y = ~median) %>%
150- # ' add_lines(fill = "black")
151237add_lines <- function (p , x = NULL , y = NULL , z = NULL , ... ,
152238 data = NULL , inherit = TRUE ) {
153239 if (inherit ) {
@@ -192,15 +278,6 @@ add_segments <- function(p, x = NULL, y = NULL, xend = NULL, yend = NULL, ...,
192278# ' @inheritParams add_trace
193279# ' @rdname add_trace
194280# ' @export
195- # ' @examples
196- # '
197- # ' ggplot2::map_data("world", "canada") %>%
198- # ' group_by(group) %>%
199- # ' plot_ly(x = ~long, y = ~lat) %>%
200- # ' add_polygons(hoverinfo = "none") %>%
201- # ' add_markers(text = ~paste(name, "<br />", pop), hoverinfo = "text",
202- # ' data = maps::canada.cities) %>%
203- # ' layout(showlegend = FALSE)
204281add_polygons <- function (p , x = NULL , y = NULL , ... ,
205282 data = NULL , inherit = TRUE ) {
206283 if (inherit ) {
@@ -222,12 +299,6 @@ add_polygons <- function(p, x = NULL, y = NULL, ...,
222299# ' @inheritParams add_trace
223300# ' @rdname add_trace
224301# ' @export
225- # ' @examples
226- # '
227- # ' if (requireNamespace("sf", quietly = TRUE)) {
228- # ' nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
229- # ' plot_ly() %>% add_sf(data = nc)
230- # ' }
231302add_sf <- function (p , ... , x = ~ x , y = ~ y , data = NULL , inherit = TRUE ) {
232303 try_library(" sf" , " add_sf" )
233304 dat <- plotly_data(add_data(p , data ))
@@ -277,10 +348,6 @@ add_sf <- function(p, ..., x = ~x, y = ~y, data = NULL, inherit = TRUE) {
277348# ' @param rownames whether or not to display the rownames of `data`.
278349# ' @rdname add_trace
279350# ' @export
280- # ' @examples
281- # '
282- # ' plot_ly(economics) %>%
283- # ' add_table()
284351add_table <- function (p , ... , rownames = TRUE , data = NULL , inherit = TRUE ) {
285352 attrs <- list (... )
286353 dat <- plotly_data(add_data(p , data ))
@@ -308,11 +375,6 @@ add_table <- function(p, ..., rownames = TRUE, data = NULL, inherit = TRUE) {
308375# ' @inheritParams add_trace
309376# ' @rdname add_trace
310377# ' @export
311- # ' @examples
312- # '
313- # ' plot_ly(economics, x = ~date) %>%
314- # ' add_ribbons(ymin = ~pce - 1e3, ymax = ~pce + 1e3)
315-
316378add_ribbons <- function (p , x = NULL , ymin = NULL , ymax = NULL , ... ,
317379 data = NULL , inherit = TRUE ) {
318380 if (inherit ) {
@@ -335,9 +397,6 @@ add_ribbons <- function(p, x = NULL, ymin = NULL, ymax = NULL, ...,
335397# ' @param r For polar chart only. Sets the radial coordinates.
336398# ' @param t For polar chart only. Sets the radial coordinates.
337399# ' @export
338- # ' @examples
339- # ' p <- plot_ly(plotly::wind, r = ~r, t = ~t) %>% add_area(color = ~nms)
340- # ' layout(p, radialaxis = list(ticksuffix = "%"), orientation = 270)
341400add_area <- function (p , r = NULL , t = NULL , ... ,
342401 data = NULL , inherit = TRUE ) {
343402 if (inherit ) {
@@ -358,15 +417,6 @@ add_area <- function(p, r = NULL, t = NULL, ...,
358417# ' @param values the value to associated with each slice of the pie.
359418# ' @param labels the labels (categories) corresponding to `values`.
360419# ' @export
361- # ' @examples
362- # ' ds <- data.frame(
363- # ' labels = c("A", "B", "C"),
364- # ' values = c(10, 40, 60)
365- # ' )
366- # '
367- # ' plot_ly(ds, labels = ~labels, values = ~values) %>%
368- # ' add_pie() %>%
369- # ' layout(title = "Basic Pie Chart using Plotly")
370420add_pie <- function (p , values = NULL , labels = NULL , ... ,
371421 data = NULL , inherit = TRUE ) {
372422 if (inherit ) {
@@ -385,12 +435,6 @@ add_pie <- function(p, values = NULL, labels = NULL, ...,
385435# ' @inheritParams add_trace
386436# ' @rdname add_trace
387437# ' @export
388- # ' @examples
389- # ' library(dplyr)
390- # ' mtcars %>%
391- # ' count(vs) %>%
392- # ' plot_ly(x = ~vs, y = ~n) %>%
393- # ' add_bars()
394438add_bars <- function (p , x = NULL , y = NULL , ... ,
395439 data = NULL , inherit = TRUE ) {
396440 if (inherit ) {
@@ -411,9 +455,6 @@ add_bars <- function(p, x = NULL, y = NULL, ...,
411455# ' @inheritParams add_trace
412456# ' @rdname add_trace
413457# ' @export
414- # ' @examples
415- # '
416- # ' plot_ly(x = ~rnorm(100)) %>% add_histogram()
417458add_histogram <- function (p , x = NULL , y = NULL , ... ,
418459 data = NULL , inherit = TRUE ) {
419460 if (inherit ) {
@@ -434,10 +475,6 @@ add_histogram <- function(p, x = NULL, y = NULL, ...,
434475# ' @inheritParams add_trace
435476# ' @rdname add_trace
436477# ' @export
437- # ' @examples
438- # ' plot_ly(x = ~LETTERS, y = ~LETTERS) %>% add_histogram2d()
439- # ' z <- as.matrix(table(LETTERS, LETTERS))
440- # ' plot_ly(x = ~LETTERS, y = ~LETTERS, z = ~z) %>% add_histogram2d()
441478add_histogram2d <- function (p , x = NULL , y = NULL , z = NULL , ... ,
442479 data = NULL , inherit = TRUE ) {
443480 if (inherit ) {
@@ -460,9 +497,6 @@ add_histogram2d <- function(p, x = NULL, y = NULL, z = NULL, ...,
460497# ' @inheritParams add_trace
461498# ' @rdname add_trace
462499# ' @export
463- # ' @examples
464- # ' plot_ly(MASS::geyser, x = ~waiting, y = ~duration) %>%
465- # ' add_histogram2dcontour()
466500add_histogram2dcontour <- function (p , x = NULL , y = NULL , z = NULL , ... ,
467501 data = NULL , inherit = TRUE ) {
468502 if (inherit ) {
@@ -487,8 +521,6 @@ add_histogram2dcontour <- function(p, x = NULL, y = NULL, z = NULL, ...,
487521# ' @inheritParams add_trace
488522# ' @rdname add_trace
489523# ' @export
490- # ' @examples
491- # ' plot_ly(z = ~volcano) %>% add_heatmap()
492524add_heatmap <- function (p , x = NULL , y = NULL , z = NULL , ... ,
493525 data = NULL , inherit = TRUE ) {
494526 if (inherit ) {
@@ -508,8 +540,6 @@ add_heatmap <- function(p, x = NULL, y = NULL, z = NULL, ...,
508540# ' @inheritParams add_trace
509541# ' @rdname add_trace
510542# ' @export
511- # ' @examples
512- # ' plot_ly(z = ~volcano) %>% add_contour()
513543add_contour <- function (p , z = NULL , ... , data = NULL , inherit = TRUE ) {
514544 if (inherit ) {
515545 z <- z %|| % p $ x $ attrs [[1 ]][[" z" ]]
@@ -527,8 +557,6 @@ add_contour <- function(p, z = NULL, ..., data = NULL, inherit = TRUE) {
527557# ' @inheritParams add_trace
528558# ' @rdname add_trace
529559# ' @export
530- # ' @examples
531- # ' plot_ly(mtcars, x = ~factor(vs), y = ~mpg) %>% add_boxplot()
532560add_boxplot <- function (p , x = NULL , y = NULL , ... , data = NULL , inherit = TRUE ) {
533561 if (inherit ) {
534562 x <- x %|| % p $ x $ attrs [[1 ]][[" x" ]]
@@ -547,8 +575,6 @@ add_boxplot <- function(p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE)
547575# ' @inheritParams add_trace
548576# ' @rdname add_trace
549577# ' @export
550- # ' @examples
551- # ' plot_ly(z = ~volcano) %>% add_surface()
552578add_surface <- function (p , z = NULL , ... , data = NULL , inherit = TRUE ) {
553579 if (inherit ) {
554580 z <- z %|| % p $ x $ attrs [[1 ]][[" z" ]]
@@ -565,8 +591,6 @@ add_surface <- function(p, z = NULL, ..., data = NULL, inherit = TRUE) {
565591# ' @inheritParams add_trace
566592# ' @rdname add_trace
567593# ' @export
568- # ' @examples
569- # ' plot_ly(x = c(0, 0, 1), y = c(0, 1, 0), z = c(0, 0, 0)) %>% add_mesh()
570594add_mesh <- function (p , x = NULL , y = NULL , z = NULL , ... ,
571595 data = NULL , inherit = TRUE ) {
572596 if (inherit ) {
@@ -654,30 +678,6 @@ special_attrs <- function(trace) {
654678# ' modified plotly object.
655679# ' @param ... arguments passed to `fun`.
656680# ' @export
657- # ' @examples
658- # '
659- # ' txhousing %>%
660- # ' group_by(city) %>%
661- # ' plot_ly(x = ~date, y = ~median) %>%
662- # ' add_lines(alpha = 0.2, name = "Texan Cities") %>%
663- # ' add_fun(function(plot) {
664- # ' plot %>% filter(city == "Houston") %>% add_lines(name = "Houston")
665- # ' }) %>%
666- # ' add_fun(function(plot) {
667- # ' plot %>% filter(city == "San Antonio") %>% add_lines(name = "San Antonio")
668- # ' })
669- # '
670- # ' plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
671- # ' add_markers() %>%
672- # ' add_fun(function(p) {
673- # ' p %>% slice(which.max(mpg)) %>%
674- # ' add_annotations("Good mileage")
675- # ' }) %>%
676- # ' add_fun(function(p) {
677- # ' p %>% slice(which.min(mpg)) %>%
678- # ' add_annotations(text = "Bad mileage")
679- # ' })
680- # '
681681add_fun <- function (p , fun , ... ) {
682682 oldDat <- p $ x $ cur_data
683683 p <- fun(p , ... )
@@ -699,19 +699,6 @@ add_fun <- function(p, fun, ...) {
699699# ' @param inherit inherit attributes from [plot_ly()]?
700700# ' @author Carson Sievert
701701# ' @export
702- # ' @examples
703- # '
704- # ' # single annotation
705- # ' plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
706- # ' slice(which.max(mpg)) %>%
707- # ' add_annotations(text = "Good mileage")
708- # '
709- # ' # multiple annotations
710- # ' plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
711- # ' filter(gear == 5) %>%
712- # ' add_annotations("five cylinder", ax = 40)
713- # '
714-
715702add_annotations <- function (p , text = NULL , ... , data = NULL , inherit = TRUE ) {
716703 p <- add_data(p , data )
717704 attrs <- list (text = text , ... )
0 commit comments