Skip to content

Commit 7372574

Browse files
committed
update news; clean up sf-mapbox-data demo
1 parent 5a7acc4 commit 7372574

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
* Upgraded to plotly.js v1.35.2. A _huge_ amount of features and improvements have been made since v1.29.2 (i.e., the version included in the last CRAN release of the R package - v4.7.1). Highlights include a complete re-write of `scattergl` to make it nearly feature complete with `scatter`, localization of text rendering (i.e., international translations), and two new trace types (`violin` & `table`). Read more about the v1.32.0 release [here](https://codeburst.io/notes-from-the-latest-plotly-js-release-b035a5b43e21) and the complete list of changes [here](https://github.com/plotly/plotly.js/releases).
66
* Support for **sf** (simple feature) data structures was added to `plot_ly()`, `plot_mapbox()`, and `plot_geo()` (via the new `add_sf()` function). See [this blog post](https://blog.cpsievert.me/2018/03/30/visualizing-geo-spatial-data-with-sf-and-plotly) for an overview.
7-
* New "special arguments" `stroke`, `strokes`, `alpha_stroke`, `span`, and `spans` were added for easier control over the stroke (i.e., outline) appearance of various (filled) graphical marks. For an overview, see the **sf** blog post linked to in the bullet point above.
7+
* New "special arguments" `stroke`, `strokes`, `alpha_stroke`, `span`, and `spans` were added for easier control over the stroke (i.e., outline) appearance of various (filled) graphical marks. For an overview, see the **sf** blog post linked to in the bullet point above and the new package demos (list all demos with `demo(package = "plotly")`).
88
* The selection (i.e., linked-brushing) mode can now switch from 'transient' to 'persistent' by holding the 'shift' key. It's still possible to _force_ persistent selection by setting `persistent = TRUE` in `highlight()`, but `persistent = FALSE` (the default) is now recommended since it allows one to switch between [persistent/transient selection](https://plotly-book.cpsievert.me/linking-views-without-shiny.html#transient-versus-persistent-selection) in the browser, rather than at the command line.
99
* Instead of an error, `ggplotly(NULL, "message")` and `plotly_build(NULL, "message")` now returns `htmltools::div("message")`, making it easier to relay messages in shiny when data isn't yet ready to plot (see #1116)
1010
* The `animation_button()` function gains a `label` argument, making it easier to control the label of an animation button generated through the `frame` API (see #1205).
1111

1212
## CHANGES
1313

1414
* 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"))`.
15-
* 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")`)
15+
* 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")`).
16+
* `layout.showlegend` now defaults to `TRUE` for a *single* pie trace. This is a more sensible default and matches pure plotly.js behavior.
1617
* The `elementId` field is no longer populated, which fixes the "Ignoring explicitly provided widget ID" warning in shiny applications (see #985).
1718

1819
## BUG FIXES

demo/sf-mapbox-data.R

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,35 @@
1-
library(sf)
2-
3-
plot_mapbox(res_mn)
4-
plot_mapbox(res_mn, split = ~INDRESNAME)
5-
6-
7-
library(sf)
81
library(plotly)
2+
library(crosstalk)
93

10-
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
11-
12-
# can set multiple bounding boxes and overwrite attributes
13-
subplot(
14-
plot_mapbox(nc),
15-
plot_mapbox(nc, fillcolor = "gray", line = list(size = 0.01, color = "black"))
4+
# one trace
5+
plot_mapbox(res_mn)
6+
plot_mapbox(res_mn, stroke = I("#119dff"), span = I(1), color = I("#00cc96"))
7+
8+
# multiple traces
9+
plot_mapbox(res_mn, split = ~INDRESNAME, span = I(1))
10+
plot_mapbox(res_mn, split = ~INDRESNAME, color = ~AREA, stroke = ~PERIMETER, span = I(1))
11+
12+
# linking with DT
13+
mn <- crosstalk::SharedData$new(res_mn)
14+
bscols(
15+
plot_mapbox(mn, split = ~INDRESNAME, text = ~INDRESNAME, hoverinfo = "text", hoveron = "fill") %>%
16+
layout(title = "Click a reservation", showlegend = FALSE),
17+
DT::datatable(mn)
1618
)
1719

18-
# map custom hover text to each point
19-
# (unfortunately, scattermapbox does not yet support hoveron='fill')
20-
plot_mapbox(nc, text = ~paste0(NAME, ": ", AREA), hoverinfo = "text")
21-
22-
23-
col_scale <- scales::col_numeric("Blues", range(nc$AREA))
24-
plot_ly(nc, fillcolor = ~col_scale(AREA), text = ~AREA, hoveron = "fill") %>%
25-
layout(showlegend = FALSE)
26-
27-
# TODO: animation
20+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
21+
ncsd <- SharedData$new(nc)
2822

2923

30-
# TODO: click to highlight
31-
ncsd <- crosstalk::SharedData$new(nc)
32-
plot_mapbox(ncsd)
33-
34-
35-
# TODO: perhaps during verification, if hoveron = 'fill' for a given trace,
36-
# we could check if text is unique or not...if it is, just take first element
37-
plot_mapbox(nc, split = ~AREA, text = ~NAME, hoveron = "fill")
38-
39-
# TODO: how to best control hoverinfo?
40-
41-
42-
43-
# non-standard crs
44-
library(mapview)
45-
plot_mapbox(trails)
46-
24+
# note that brushing counties is currently possible with plot_ly(), but isn't quite working
25+
# yet with plot_mapbox() -- https://github.com/plotly/plotly.js/issues/2512
26+
bscols(
27+
plot_mapbox(ncsd) %>%
28+
highlight(dynamic = TRUE, persistent = TRUE),
29+
plot_ly(ncsd, x = ~AREA) %>%
30+
add_histogram(xbins = list(start = 0, end = 0.3, size = 0.02)) %>%
31+
layout(barmode = "overlay") %>%
32+
highlight("plotly_selected", persistent = TRUE)
33+
)
4734

4835

tests/testthat/test-plotly-sf.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,22 @@ test_that("altogether now", {
317317
})
318318

319319

320+
321+
test_that("color and stroke scales can be set independently", {
322+
323+
n <- length(unique(res_mn$INDRESNAME))
324+
p <- plot_mapbox(res_mn, split = ~INDRESNAME, color = ~AREA, stroke = ~PERIMETER, span = I(2)) %>%
325+
plotly_build()
326+
327+
# two colorbars
328+
d <- p$x$data
329+
expect_true(length(d) == n + 2)
330+
331+
colorbars <- d[vapply(d, is.colorbar, logical(1))]
332+
333+
expect_true(colorbars[[1]]$marker$colorbar$title == "AREA")
334+
expect_true(colorbars[[2]]$marker$colorbar$title == "PERIMETER")
335+
expect_true(all(colorbars[[1]]$marker$color == range(res_mn$AREA)))
336+
expect_true(all(colorbars[[2]]$marker$color == range(res_mn$PERIMETER)))
337+
338+
})

0 commit comments

Comments
 (0)