@@ -82,6 +82,20 @@ add_trace <- function(p, ..., color, symbol, size, linetype,
8282 attrs <- modify_list(p $ x $ attrs [[1 ]], attrs )
8383 }
8484
85+ if (is_mapbox(p ) || is_geo(p )) {
86+ attrs [[" x" ]] <- attrs [[" x" ]] %|| % attrs [[" lat" ]]
87+ attrs [[" y" ]] <- attrs [[" y" ]] %|| % attrs [[" lon" ]]
88+ if (! grepl(" scatter" , attrs [[" type" ]])) {
89+ stop(" Cant add a '" , attrs [[" type" ]], " ' trace to a map object" , call. = FALSE )
90+ }
91+ if (is_mapbox(p )) {
92+ attrs [[" type" ]] <- " scattermapbox"
93+ }
94+ if (is_geo(p )) {
95+ attrs [[" type" ]] <- " scattergeo"
96+ }
97+ }
98+
8599 p $ x $ attrs <- c(
86100 p $ x $ attrs %|| % list (),
87101 setNames(list (attrs ), p $ x $ cur_data )
@@ -255,32 +269,57 @@ add_ribbons <- function(p, x = NULL, ymin = NULL, ymax = NULL, ...,
255269 )
256270}
257271
258-
259272# ' @inheritParams add_trace
260273# ' @rdname add_trace
274+ # ' @param r For polar chart only. Sets the radial coordinates.
275+ # ' @param t For polar chart only. Sets the radial coordinates.
261276# ' @export
262277# ' @examples
263- # ' huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron) )
264- # ' plot_ly(huron, x = ~year, ymax = ~level) %>% add_area( )
265- add_area <- function (p , x = NULL , ymax = NULL , ... ,
278+ # ' p <- plot_ly(plotly::wind, r = ~r, t = ~t) %>% add_area(color = ~nms )
279+ # ' layout(p, radialaxis = list(ticksuffix = "%"), orientation = 270 )
280+ add_area <- function (p , r = NULL , t = NULL , ... ,
266281 data = NULL , inherit = TRUE ) {
267-
268282 if (inherit ) {
269- x <- x %|| % p $ x $ attrs [[1 ]][[" x " ]]
270- ymax <- ymax %|| % p $ x $ attrs [[1 ]][[" ymax " ]]
283+ r <- t %|| % p $ x $ attrs [[1 ]][[" r " ]]
284+ t <- t %|| % p $ x $ attrs [[1 ]][[" t " ]]
271285 }
272- if (is.null(x ) || is.null(ymax )) {
273- stop(" Must supply `x `/`ymax ` attributes" , call. = FALSE )
286+ if (is.null(r ) || is.null(t )) {
287+ stop(" Must supply `r `/`t ` attributes" , call. = FALSE )
274288 }
275289 add_trace_classed(
276- p , class = c(" plotly_area" , " plotly_ribbon" , " plotly_polygon" ),
277- x = x , ymax = ymax ,
278- type = " scatter" , fill = " toself" , mode = " lines" , hoveron = " points" ,
290+ p , class = " plotly_area" , r = r , t = t , type = " area" ,
279291 ... , data = data , inherit = inherit
280292 )
281293}
282294
283-
295+ # ' @inheritParams add_trace
296+ # ' @rdname add_trace
297+ # ' @param values the value to associated with each slice of the pie.
298+ # ' @param labels the labels (categories) corresponding to \code{values}.
299+ # ' @export
300+ # ' @examples
301+ # ' ds <- data.frame(
302+ # ' labels = c("A", "B", "C"),
303+ # ' values = c(10, 40, 60)
304+ # ' )
305+ # '
306+ # ' plot_ly(ds, labels = ~labels, values = ~values) %>%
307+ # ' add_pie() %>%
308+ # ' layout(title = "Basic Pie Chart using Plotly")
309+ add_pie <- function (p , values = NULL , labels = NULL , ... ,
310+ data = NULL , inherit = TRUE ) {
311+ if (inherit ) {
312+ values <- values %|| % p $ x $ attrs [[1 ]][[" values" ]]
313+ labels <- labels %|| % p $ x $ attrs [[1 ]][[" labels" ]]
314+ }
315+ if (is.null(values )) {
316+ stop(" Must supply `values`" , call. = FALSE )
317+ }
318+ add_trace_classed(
319+ p , class = " plotly_pie" , values = values , labels = labels , type = " pie" ,
320+ ... , data = data , inherit = inherit
321+ )
322+ }
284323
285324# ' @inheritParams add_trace
286325# ' @rdname add_trace
@@ -464,24 +503,37 @@ add_surface <- function(p, z = NULL, ..., data = NULL, inherit = TRUE) {
464503 )
465504}
466505
467-
468506# ' @inheritParams add_trace
469507# ' @rdname add_trace
470- # ' @param geo anchor this trace on which geo object?
471508# ' @export
472509# ' @examples
473- # ' plot_ly() %>% add_scattergeo()
474- add_scattergeo <- function (p , geo = NULL , ... , data = NULL , inherit = TRUE ) {
510+ # ' plot_ly(x = c(0, 0, 1), y = c(0, 1, 0), z = c(0, 0, 0)) %>% add_mesh()
511+ add_mesh <- function (p , x = NULL , y = NULL , z = NULL , ... ,
512+ data = NULL , inherit = TRUE ) {
475513 if (inherit ) {
476- geo <- geo %|| % p $ x $ attrs [[1 ]][[" geo" ]] %|| % " geo"
514+ x <- x %|| % p $ x $ attrs [[1 ]][[" x" ]]
515+ y <- y %|| % p $ x $ attrs [[1 ]][[" y" ]]
516+ z <- z %|| % p $ x $ attrs [[1 ]][[" z" ]]
517+ }
518+ if (is.null(x ) || is.null(y ) || is.null(z )) {
519+ stop(" Must supply `x`/`y`/`z` attributes" , call. = FALSE )
477520 }
478521 add_trace_classed(
479- p , class = " plotly_scattergeo " , type = " scattergeo " , geo = geo ,
522+ p , class = " plotly_mesh " , x = x , y = y , z = z , type = " mesh3d " ,
480523 ... , data = data , inherit = inherit
481524 )
482525}
483526
484527
528+ # ' @inheritParams add_trace
529+ # ' @rdname add_trace
530+ # ' @export
531+ # '
532+ add_scattergeo <- function (p , ... ) {
533+ .Deprecated(" geo" )
534+ p
535+ }
536+
485537# ' @inheritParams add_trace
486538# ' @rdname add_trace
487539# ' @export
@@ -515,12 +567,16 @@ add_trace_classed <- function(p, class = "plotly_polygon", ...) {
515567
516568# retrieve the non-plotly.js attributes for a given trace
517569special_attrs <- function (trace ) {
518- switch (
570+ attrs <- switch (
519571 class(trace )[[1 ]],
520- plotly_area = c(" ymax" ),
521572 plotly_segment = c(" xend" , " yend" ),
522573 plotly_ribbon = c(" ymin" , " ymax" )
523574 )
575+ # for data training, we temporarily rename lat/lon as x/y
576+ if (isTRUE(trace [[" type" ]] %in% c(" scattermapbox" , " scattergeo" ))) {
577+ attrs <- c(attrs , c(" x" , " y" ))
578+ }
579+ attrs
524580}
525581
526582
0 commit comments