Shapes in R

How to use shapes in R. Two examples on highlighting regions by adding shapes to your R charts.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Lines

library(plotly)

s <- seq.int(0, 15)
fig <- plot_ly(x = ~s, y = ~sin(s), mode = "lines")

# initiate a line shape object
line <- list(
  type = "line",
  line = list(color = "pink"),
  xref = "x",
  yref = "y"
)

lines <- list()
for (i in c(0, 3, 5, 7, 9, 13)) {
  line[["x0"]] <- i
  line[["x1"]] <- i + 2
  line[c("y0", "y1")] <- sin(i + 1)
  lines <- c(lines, list(line))
}

fig <- layout(fig, title = 'Highlighting with Lines', shapes = lines)

fig

Rectangles

library(plotly)
fig <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")

# add shapes to the layout
fig <- layout(fig, title = 'Highlighting with Rectangles',
             shapes = list(
               list(type = "rect",
                    fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
                    x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
                    y0 = 4, y1 = 12.5, yref = "y"),
               list(type = "rect",
                 fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
                 x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
                 y0 = 4, y1 = 12.5, yref = "y")))

fig

Circles

Circles are centered around ((x0+x1)/2, (y0+y1)/2))

library(plotly)
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, title = 'Highlighting Regions with Circles',
            shapes = list(
              list(type = 'circle',
                   xref = 'x', x0 = .2, x1 = .7,
                   yref = 'y', y0 = 20, y1 = 3000,
                   fillcolor = 'rgb(50, 20, 90)', line = list(color = 'rgb(50, 20, 90)'),
                   opacity = 0.2),
              list(type = 'circle',
                   xref = 'x', x0 = .75, x1 = 1.5,
                   yref = 'y', y0 = 2500, y1 = 7500,
                   fillcolor = 'rgb(30, 100, 120)', line = list(color = 'rgb(30, 100, 120)'),
                   opacity = 0.2),
               list(type = 'circle',
                   xref = 'x', x0 = 1.6, x1 = 2.5,
                   yref = 'y', y0 = 12500, y1 = 18500,
                   fillcolor = 'rgb(90, 200, 75)', line = list(color = 'rgb(90, 200, 75)'),
                   opacity = 0.2)))

fig

Drawing Shapes on Cartesian Plots

You can create layout shapes programatically, but you can also draw shapes manually by setting the dragmode to one of the shape-drawing modes: drawline,drawopenpath, drawclosedpath, drawcircle, or drawrect. If you need to switch between different shape-drawing or other dragmodes (panning, selecting, etc.), modebar buttons can be added in the config of your figure to select the dragmode.

If you switch to a different dragmode such as pan or zoom, you will need to select the drawing tool in the modebar to go back to shape drawing.

This shape-drawing feature is particularly interesting for annotating graphs, in particular image traces.

Once you have drawn shapes, you can select and modify an existing shape by clicking on its boundary (note the arrow pointer). Its fillcolor turns to pink to highlight the activated shape and then you can

  • drag and resize it for lines, rectangles and circles/ellipses
  • drag and move individual vertices for closed paths
  • move individual vertices for open paths.

An activated shape is deleted by cliking on the eraseshape button.

Drawing or modifying a shape triggers a relayout event, which can be captured by a callback inside a Dash For R application.

library(plotly)

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" ) )

fig

Style of user-drawn shapes

The layout newshape attribute controls the visual appearance of new shapes drawn by the user. newshape attributes have the same names as layout shapes.

Note on shape opacity: having a new shape's opacity > 0.5 makes it possible to activate a shape by clicking inside the shape (for opacity <= 0.5 you have to click on the border of the shape), but you cannot start a new shape within an existing shape (which is possible for an opacity <= 0.5).

library(plotly)

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", newshape=list(fillcolor="yellow", opacity=0.5), 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" ) )

fig

Reference

Check out our reference page for more information on using shapes!

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)