Black Lives Matter. Please consider donating to Black Girls Code today.

Announcing Plotly.R - Shape Drawing, Unified Hover, and Rangebreaks

I’m excited to announce that Plotly.R is now available for download via devtools. For up-to-date installation instructions, please see our Getting Started documentation page.

What’s New in Plotly.R

Powered by Plotly.js 1.54, perfect for DashR 0.6.2

This release of Plotly.R inherits all of the improvements to the underlying JavaScript library that powers it. The full Plotly.js 1.54.x changelog contains more details about what changed under the hood. The version of Plotly.js that powers this version of Plotly.R is the same as the one that powers DashR v0.6.2.

Shape Drawing

We have added new layout dragmode s for users to be able to draw shapes such as rectangles, lines, open or closed paths etc. These shapes can also be modified or deleted when activated. In particular, this feature should make it easy to annotate images for measuring objects properties or building training sets for neural nets.

The documentation on shapes has been updated to illustrate this shape-drawing mode. In order to use the shape-drawing tools, optional buttons need to be explicitly added to the modebar .


df <- diamonds[sample(nrow(diamonds), 1000), ]

fig <- plot_ly(df, x = ~carat, y = ~price, text = ~paste("Clarity: ", clarity),
             mode = "markers", color = ~carat, size = ~carat)

fig <- layout(fig, dragmode="drawrect", xaxis = list(title = 'Click and drag inside the figure to draw a rectangle or select another shape in the modebar'))

fig <- fig %>%
  config(modeBarButtonsToAdd = list("drawine", "drawopenpath", "drawclosedpath", "drawcircle", "drawrect", "eraseshape" ) )



Unified Hover

Until today, depending on the hovermode , hovering on data resulted in either one hoverlabel on the closest point to the cursor, or one hoverlabel per point at the same x- or y-coordinate as the cursor. This release introduces two new values for layout.hovermode , namely "x unified" and "y unified" , which results in a single hover label that refers to multiple points at a single X or Y value (on 2D cartesian subplots). Here’s what this looks like:


trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.frame(x, trace_0, trace_1, trace_2)

fig <- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines') 
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') 
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
fig <- fig %>%
  layout(hovermode = "x unified")

Excluding Weekends and Holidays from Date Axes

A long-requested feature has been to add the ability to remove certain time periods from charts with date axes , for example removing weekends from charts that detail business processes or financial transactions. This is now implemented for date axes using the new rangebreaks attribute (but unfortunately does not work with the scattergl trace type yet).

Using rangebreaks you can exclude weekends with bounds=c("sat", "mon") and individual dates with values=c("2015-12-25", "2016-01-01"). Check out our Time Series and Date Axes documentation page for information on how to use this feature. In the screenshots below you can see the same data plotted without and with rangebreaks. The data is for weekdays only, and has additional gaps on holidays, but these gaps are hidden in the second plot.

This feature was anonymously sponsored and we thank our benefactor on behalf of the community :heart:.

Get it now!

To sum up: Plotly.R is out and if you’re excited about any of the above features, head on over to our Getting Started documentation page for full installation instructions.

1 Like