Show Sidebar Hide Sidebar

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's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.7.0.9000'

Lines

s <- seq.int(0, 15)
p <- 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))
}

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

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="shapes/lines")
chart_link

Rectangles

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

# add shapes to the layout
p <- layout(p, 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")))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="shapes/unemployment")
chart_link

Circles

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

library(plotly)
d <- diamonds[sample(nrow(diamonds), 1000), ]
d <- plot_ly(d, x = ~carat, y = ~price, text = ~paste("Clarity: ", clarity),
             mode = "markers", color = ~carat, size = ~carat, opacity = ~carat)
d <- layout(d, 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)))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(d, filename="shapes/diamonds")
## Error: Client error: (400) Bad Request
##  Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. Use grid references instead. Raw data found at the following paths in the figure [('data', 0, u'opacity')]
chart_link

Reference

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

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.