@@ -33,24 +33,115 @@ add_data <- function(p, data = NULL) {
33
33
# ' @param xend "final" x position (in this context, x represents "start")
34
34
# ' @param yend "final" y position (in this context, y represents "start")
35
35
# ' @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/}
37
41
# ' @author Carson Sievert
38
42
# ' @export
39
43
# ' @rdname add_trace
40
44
# ' @examples
41
45
# '
46
+ # ' # the `plot_ly()` function initiates an object, and if no trace type
47
+ # ' # is specified, it sets a sensible default
42
48
# ' p <- plot_ly(economics, x = ~date, y = ~uempmed)
43
49
# ' p
44
- # ' p %>% add_markers()
45
- # ' p %>% add_lines()
46
- # ' p %>% add_text(text = ".")
47
50
# '
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) %>%
51
70
# ' 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
+ # ' }
54
145
# '
55
146
add_trace <- function (p , ... ,
56
147
data = NULL , inherit = TRUE ) {
@@ -143,11 +234,6 @@ add_paths <- function(p, x = NULL, y = NULL, z = NULL, ...,
143
234
# ' @inheritParams add_trace
144
235
# ' @rdname add_trace
145
236
# ' @export
146
- # ' @examples
147
- # ' txhousing %>%
148
- # ' group_by(city) %>%
149
- # ' plot_ly(x = ~date, y = ~median) %>%
150
- # ' add_lines(fill = "black")
151
237
add_lines <- function (p , x = NULL , y = NULL , z = NULL , ... ,
152
238
data = NULL , inherit = TRUE ) {
153
239
if (inherit ) {
@@ -192,15 +278,6 @@ add_segments <- function(p, x = NULL, y = NULL, xend = NULL, yend = NULL, ...,
192
278
# ' @inheritParams add_trace
193
279
# ' @rdname add_trace
194
280
# ' @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)
204
281
add_polygons <- function (p , x = NULL , y = NULL , ... ,
205
282
data = NULL , inherit = TRUE ) {
206
283
if (inherit ) {
@@ -222,12 +299,6 @@ add_polygons <- function(p, x = NULL, y = NULL, ...,
222
299
# ' @inheritParams add_trace
223
300
# ' @rdname add_trace
224
301
# ' @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
- # ' }
231
302
add_sf <- function (p , ... , x = ~ x , y = ~ y , data = NULL , inherit = TRUE ) {
232
303
try_library(" sf" , " add_sf" )
233
304
dat <- plotly_data(add_data(p , data ))
@@ -277,10 +348,6 @@ add_sf <- function(p, ..., x = ~x, y = ~y, data = NULL, inherit = TRUE) {
277
348
# ' @param rownames whether or not to display the rownames of `data`.
278
349
# ' @rdname add_trace
279
350
# ' @export
280
- # ' @examples
281
- # '
282
- # ' plot_ly(economics) %>%
283
- # ' add_table()
284
351
add_table <- function (p , ... , rownames = TRUE , data = NULL , inherit = TRUE ) {
285
352
attrs <- list (... )
286
353
dat <- plotly_data(add_data(p , data ))
@@ -308,11 +375,6 @@ add_table <- function(p, ..., rownames = TRUE, data = NULL, inherit = TRUE) {
308
375
# ' @inheritParams add_trace
309
376
# ' @rdname add_trace
310
377
# ' @export
311
- # ' @examples
312
- # '
313
- # ' plot_ly(economics, x = ~date) %>%
314
- # ' add_ribbons(ymin = ~pce - 1e3, ymax = ~pce + 1e3)
315
-
316
378
add_ribbons <- function (p , x = NULL , ymin = NULL , ymax = NULL , ... ,
317
379
data = NULL , inherit = TRUE ) {
318
380
if (inherit ) {
@@ -335,9 +397,6 @@ add_ribbons <- function(p, x = NULL, ymin = NULL, ymax = NULL, ...,
335
397
# ' @param r For polar chart only. Sets the radial coordinates.
336
398
# ' @param t For polar chart only. Sets the radial coordinates.
337
399
# ' @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)
341
400
add_area <- function (p , r = NULL , t = NULL , ... ,
342
401
data = NULL , inherit = TRUE ) {
343
402
if (inherit ) {
@@ -358,15 +417,6 @@ add_area <- function(p, r = NULL, t = NULL, ...,
358
417
# ' @param values the value to associated with each slice of the pie.
359
418
# ' @param labels the labels (categories) corresponding to `values`.
360
419
# ' @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")
370
420
add_pie <- function (p , values = NULL , labels = NULL , ... ,
371
421
data = NULL , inherit = TRUE ) {
372
422
if (inherit ) {
@@ -385,12 +435,6 @@ add_pie <- function(p, values = NULL, labels = NULL, ...,
385
435
# ' @inheritParams add_trace
386
436
# ' @rdname add_trace
387
437
# ' @export
388
- # ' @examples
389
- # ' library(dplyr)
390
- # ' mtcars %>%
391
- # ' count(vs) %>%
392
- # ' plot_ly(x = ~vs, y = ~n) %>%
393
- # ' add_bars()
394
438
add_bars <- function (p , x = NULL , y = NULL , ... ,
395
439
data = NULL , inherit = TRUE ) {
396
440
if (inherit ) {
@@ -411,9 +455,6 @@ add_bars <- function(p, x = NULL, y = NULL, ...,
411
455
# ' @inheritParams add_trace
412
456
# ' @rdname add_trace
413
457
# ' @export
414
- # ' @examples
415
- # '
416
- # ' plot_ly(x = ~rnorm(100)) %>% add_histogram()
417
458
add_histogram <- function (p , x = NULL , y = NULL , ... ,
418
459
data = NULL , inherit = TRUE ) {
419
460
if (inherit ) {
@@ -434,10 +475,6 @@ add_histogram <- function(p, x = NULL, y = NULL, ...,
434
475
# ' @inheritParams add_trace
435
476
# ' @rdname add_trace
436
477
# ' @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()
441
478
add_histogram2d <- function (p , x = NULL , y = NULL , z = NULL , ... ,
442
479
data = NULL , inherit = TRUE ) {
443
480
if (inherit ) {
@@ -460,9 +497,6 @@ add_histogram2d <- function(p, x = NULL, y = NULL, z = NULL, ...,
460
497
# ' @inheritParams add_trace
461
498
# ' @rdname add_trace
462
499
# ' @export
463
- # ' @examples
464
- # ' plot_ly(MASS::geyser, x = ~waiting, y = ~duration) %>%
465
- # ' add_histogram2dcontour()
466
500
add_histogram2dcontour <- function (p , x = NULL , y = NULL , z = NULL , ... ,
467
501
data = NULL , inherit = TRUE ) {
468
502
if (inherit ) {
@@ -487,8 +521,6 @@ add_histogram2dcontour <- function(p, x = NULL, y = NULL, z = NULL, ...,
487
521
# ' @inheritParams add_trace
488
522
# ' @rdname add_trace
489
523
# ' @export
490
- # ' @examples
491
- # ' plot_ly(z = ~volcano) %>% add_heatmap()
492
524
add_heatmap <- function (p , x = NULL , y = NULL , z = NULL , ... ,
493
525
data = NULL , inherit = TRUE ) {
494
526
if (inherit ) {
@@ -508,8 +540,6 @@ add_heatmap <- function(p, x = NULL, y = NULL, z = NULL, ...,
508
540
# ' @inheritParams add_trace
509
541
# ' @rdname add_trace
510
542
# ' @export
511
- # ' @examples
512
- # ' plot_ly(z = ~volcano) %>% add_contour()
513
543
add_contour <- function (p , z = NULL , ... , data = NULL , inherit = TRUE ) {
514
544
if (inherit ) {
515
545
z <- z %|| % p $ x $ attrs [[1 ]][[" z" ]]
@@ -527,8 +557,6 @@ add_contour <- function(p, z = NULL, ..., data = NULL, inherit = TRUE) {
527
557
# ' @inheritParams add_trace
528
558
# ' @rdname add_trace
529
559
# ' @export
530
- # ' @examples
531
- # ' plot_ly(mtcars, x = ~factor(vs), y = ~mpg) %>% add_boxplot()
532
560
add_boxplot <- function (p , x = NULL , y = NULL , ... , data = NULL , inherit = TRUE ) {
533
561
if (inherit ) {
534
562
x <- x %|| % p $ x $ attrs [[1 ]][[" x" ]]
@@ -547,8 +575,6 @@ add_boxplot <- function(p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE)
547
575
# ' @inheritParams add_trace
548
576
# ' @rdname add_trace
549
577
# ' @export
550
- # ' @examples
551
- # ' plot_ly(z = ~volcano) %>% add_surface()
552
578
add_surface <- function (p , z = NULL , ... , data = NULL , inherit = TRUE ) {
553
579
if (inherit ) {
554
580
z <- z %|| % p $ x $ attrs [[1 ]][[" z" ]]
@@ -565,8 +591,6 @@ add_surface <- function(p, z = NULL, ..., data = NULL, inherit = TRUE) {
565
591
# ' @inheritParams add_trace
566
592
# ' @rdname add_trace
567
593
# ' @export
568
- # ' @examples
569
- # ' plot_ly(x = c(0, 0, 1), y = c(0, 1, 0), z = c(0, 0, 0)) %>% add_mesh()
570
594
add_mesh <- function (p , x = NULL , y = NULL , z = NULL , ... ,
571
595
data = NULL , inherit = TRUE ) {
572
596
if (inherit ) {
@@ -654,30 +678,6 @@ special_attrs <- function(trace) {
654
678
# ' modified plotly object.
655
679
# ' @param ... arguments passed to `fun`.
656
680
# ' @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
- # '
681
681
add_fun <- function (p , fun , ... ) {
682
682
oldDat <- p $ x $ cur_data
683
683
p <- fun(p , ... )
@@ -699,19 +699,6 @@ add_fun <- function(p, fun, ...) {
699
699
# ' @param inherit inherit attributes from [plot_ly()]?
700
700
# ' @author Carson Sievert
701
701
# ' @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
-
715
702
add_annotations <- function (p , text = NULL , ... , data = NULL , inherit = TRUE ) {
716
703
p <- add_data(p , data )
717
704
attrs <- list (text = text , ... )
0 commit comments