Skip to content

Commit 4704ce3

Browse files
authored
Merge pull request #1278 from ropensci/name
make name a special argument
2 parents 34541df + 60c5364 commit 4704ce3

File tree

6 files changed

+70
-10
lines changed

6 files changed

+70
-10
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
### `plot_ly()` specific changes
2929

30+
* The `name` attribute is now a "special `plot_ly()` argument" and behaves similar to `split` (it ensures a different trace for every unique value supplied). Although this leads to a breaking change (`name` was previously appended to an automatically generated trace name), it leads to a more flexible and transparent API. Those that wish to have the old behavior back should provide relevant mappings to the `name` attributes (e.g. `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = "a")` should become `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = ~paste(vs, "\na"))`)
3031
* The `color` argument now maps to `fillcolor`, making it much easier to use polygon fills to encode data values (e.g., choropleth maps). For backwards-compatibilty reasons, when `color` maps to `fillcolor`, `alpha` defaults to 0.5 (instead of 1). For an example, `plot_mapbox(mn_res, color = ~INDRESNAME)` or `plot_mapbox(mn_res, split = ~INDRESNAME, color = ~AREA, showlegend = FALSE, stroke = I("black"))`.
3132
* The `color` argument no longer automatically add `"markers"` to the `mode` attribute for scatter/scattergl trace types. Those who wish to have the old behavior back, should add `"markers"` to the `mode` explicity (e.g., change `plot_ly(economics, x = ~pce, y = ~pop, color = ~as.numeric(date), mode = "lines")` to `plot_ly(economics, x = ~pce, y = ~pop, color = ~as.numeric(date), mode = "lines+markers")`).
3233
* The `size` argument now informs a default [error_[x/y].width](https://plot.ly/r/reference/#scatter-error_x-width) (and `span` informs [error_[x/y].thickness](https://plot.ly/r/reference/#scatter-error_x-thickness)). Note you can override the default by specifying directly (e.g. `plot_ly(x = 1:10, y = 1:10, size = I(10), error_x = list(value = 5, width = 0))`).

R/plotly.R

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#' (e.g. `plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))`).
2424
#' @param type A character string specifying the trace type (e.g. `"scatter"`, `"bar"`, `"box"`, etc).
2525
#' If specified, it *always* creates a trace, otherwise
26+
#' @param name Values mapped to the trace's name attribute. Since a trace can
27+
#' only have one name, this argument acts very much like `split` in that it
28+
#' creates one trace for every unique value.
2629
#' @param color Values mapped to relevant 'fill-color' attribute(s)
2730
#' (e.g. [fillcolor](https://plot.ly/r/reference#scatter-fillcolor),
2831
#' [marker.color](https://plot.ly/r/reference#scatter-marker-color),
@@ -124,7 +127,7 @@
124127
#'
125128
#' }
126129
#'
127-
plot_ly <- function(data = data.frame(), ..., type = NULL,
130+
plot_ly <- function(data = data.frame(), ..., type = NULL, name,
128131
color, colors = NULL, alpha = NULL,
129132
stroke, strokes = NULL, alpha_stroke = 1,
130133
size, sizes = c(10, 100),
@@ -160,6 +163,7 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
160163
}
161164

162165
# tack on variable mappings
166+
attrs$name <- if (!missing(name)) name
163167
attrs$color <- if (!missing(color)) color
164168
attrs$stroke <- if (!missing(stroke)) stroke
165169
attrs$size <- if (!missing(size)) size
@@ -193,10 +197,10 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
193197
# we always deal with a _list_ of traces and _list_ of layouts
194198
# since they can each have different data
195199
layout = list(
196-
width = width,
197-
height = height,
198-
# sane margin defaults (mainly for RStudio)
199-
margin = list(b = 40, l = 60, t = 25, r = 10)
200+
width = width,
201+
height = height,
202+
# sane margin defaults (mainly for RStudio)
203+
margin = list(b = 40, l = 60, t = 25, r = 10)
200204
),
201205
source = source
202206
)

R/plotly_build.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
189189
dataArrayAttrs, special_attrs(trace), npscales(), "frame",
190190
# for some reason, text isn't listed as a data array in some traces
191191
# I'm looking at you scattergeo...
192-
".plotlyGroupIndex", "text", "key", "fillcolor"
192+
".plotlyGroupIndex", "text", "key", "fillcolor", "name"
193193
)
194194
tr <- trace[names(trace) %in% allAttrs]
195195
# TODO: does it make sense to "train" matrices/2D-tables (e.g. z)?
@@ -205,7 +205,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
205205
isAsIs <- vapply(builtData, function(x) inherits(x, "AsIs"), logical(1))
206206
isDiscrete <- vapply(builtData, is.discrete, logical(1))
207207
# note: can only have one linetype per trace
208-
isSplit <- names(builtData) %in% c("split", "linetype", "frame", "fillcolor") |
208+
isSplit <- names(builtData) %in% c("split", "linetype", "frame", "fillcolor", "name") |
209209
!isAsIs & isDiscrete & names(builtData) %in% c("symbol", "color")
210210
if (any(isSplit)) {
211211
paste2 <- function(x, y) if (identical(x, y)) x else paste(x, y, sep = br())
@@ -980,7 +980,7 @@ traceify <- function(dat, x = NULL) {
980980
new_dat <- list()
981981
for (j in seq_along(lvls)) {
982982
new_dat[[j]] <- lapply(dat, function(y) recurse(y, n, x %in% lvls[j]))
983-
new_dat[[j]]$name <- lvls[j]
983+
new_dat[[j]]$name <- new_dat[[j]]$name %||% lvls[j]
984984
}
985985
return(new_dat)
986986
}

R/utils.R

+4
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ verify_attr <- function(proposed, schema) {
492492
proposed[[attr]] <- structure(proposed[[attr]], apiSrc = TRUE)
493493
}
494494

495+
if (length(proposed$name) > 0) {
496+
proposed$name <- uniq(proposed$name)
497+
}
498+
495499
# do the same for "sub-attributes"
496500
if (identical(role, "object")) {
497501
proposed[[attr]] <- verify_attr(proposed[[attr]], schema[[attr]])

tests/testthat/test-animate-highlight.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,16 @@ test_that("animation button can be customized", {
373373
test_that("sf works with crosstalk", {
374374
skip_if_not_installed("sf")
375375

376-
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
376+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
377377
# shared data will make the polygons "query-able"
378-
ncsd <- SharedData$new(nc)
378+
ncsd <- crosstalk::SharedData$new(nc)
379379
p <- ggplot(ncsd) +
380380
geom_sf(aes(fill = AREA, text = paste0(NAME, "\n", "FIPS: ", FIPS))) +
381381
ggthemes::theme_map()
382382
gg <- ggplotly(p, tooltip = "text")
383383
d <- gg$x$data
384384
for (i in seq_along(d)) {
385+
if (!isTRUE(d[["_isGraticule"]])) next
385386
expect_false(is.null(d[[i]]$key))
386387
expect_false(is.null(d[[i]]$set))
387388
}

tests/testthat/test-plotly-name.R

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
context("name-mapping")
2+
3+
test_that("can create multiple traces from name argument", {
4+
l <- plot_ly() %>%
5+
add_markers(x = 1:10, y = 1:10, name = rep(c("a", "b"), 5)) %>%
6+
plotly_build()
7+
8+
expect_length(l$x$data, 2)
9+
expect_equal(l$x$data[[1]]$name, "a")
10+
expect_equal(l$x$data[[2]]$name, "b")
11+
})
12+
13+
14+
test_that("can override name argument", {
15+
l <- plot_ly() %>%
16+
add_markers(x = 1:10, y = 1:10, split = rep(c("a", "b"), 5), name = "z") %>%
17+
plotly_build()
18+
19+
expect_length(l$x$data, 2)
20+
expect_equal(l$x$data[[1]]$name, "z")
21+
expect_equal(l$x$data[[2]]$name, "z")
22+
23+
# can get back old behvaior
24+
l2 <- plot_ly() %>%
25+
add_markers(x = 1:10, y = 1:10, split = rep(c("a", "b"), 5), name = paste0(rep(c("a", "b"), 5), "<br>z")) %>%
26+
plotly_build()
27+
28+
expect_length(l2$x$data, 2)
29+
expect_equal(l2$x$data[[1]]$name, "a<br>z")
30+
expect_equal(l2$x$data[[2]]$name, "b<br>z")
31+
32+
})
33+
34+
35+
test_that("doesn't break old behavior", {
36+
# from https://community.plot.ly/t/manual-color-bug/10479
37+
density1 <- density(diamonds[diamonds$cut %in% "Fair", ]$carat)
38+
density2 <- density(diamonds[diamonds$cut %in% "Ideal",]$carat)
39+
40+
l <- plot_ly(x = ~density1$x, y = ~density1$y, type = 'scatter', mode = 'lines', name = 'Fair cut', fill = 'tozeroy',
41+
fillcolor = 'rgba(168, 216, 234, 0.5)',
42+
line = list(width = 0.5)) %>%
43+
add_trace(x = ~density2$x, y = ~density2$y, name = 'Ideal cut', fill = 'tozeroy',
44+
fillcolor = 'rgba(255, 212, 96, 0.5)') %>%
45+
plotly_build()
46+
47+
48+
expect_equal(l$x$data[[1]]$name, "Fair cut")
49+
expect_equal(l$x$data[[2]]$name, "Ideal cut")
50+
})

0 commit comments

Comments
 (0)