Show Sidebar Hide Sidebar

Colorway in R

How to set default trace colors using colorway in R with Plotly.

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.8.0'

Set Default Trace Colors with colorway

library(plotly)

x <- seq(-1, 3, length=50)
ones <- rep(1, 50)
a_list <- seq(1, 3, length=7)
b_list <- seq(2, 14, length=7)
df <- data.frame(cbind(x, (outer((x*x), a_list) + outer(ones, b_list))))

p <- plot_ly(df, x = ~x, y = ~V2, type = 'scatter', mode = 'lines') %>%
  add_trace(y = ~V3) %>%
  add_trace(y = ~V4) %>%
  add_trace(y = ~V5) %>%
  add_trace(y = ~V6) %>%
  add_trace(y = ~V7) %>%
  add_trace(y = ~V8) %>%
  layout(colorway = c('#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844'))


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

Reference

See https://plot.ly/r/reference/#layout-colorway for more information about the colorway attribute.

Still need help?
Contact Us

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