|
| 1 | +--- |
| 2 | +title: Colorway in R | Examples | Plotly |
| 3 | +name: Colorway |
| 4 | +permalink: r/colorway/ |
| 5 | +description: How to set default trace colors using colorway in R with Plotly. |
| 6 | +layout: base |
| 7 | +language: r |
| 8 | +page_type: example_index |
| 9 | +has_thumbnail: true |
| 10 | +display_as: style_opt |
| 11 | +order: 5 |
| 12 | +output: |
| 13 | + html_document: |
| 14 | + keep_md: true |
| 15 | +--- |
| 16 | + |
| 17 | +```{r, echo = FALSE, message=FALSE} |
| 18 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 19 | +Sys.setenv("plotly_username"="RPlotBot") |
| 20 | +Sys.setenv("plotly_api_key"="q0lz6r5efr") |
| 21 | +``` |
| 22 | + |
| 23 | +### New to Plotly? |
| 24 | + |
| 25 | +Plotly's R library is free and open source!<br> |
| 26 | +[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> |
| 27 | +You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br> |
| 28 | +We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! |
| 29 | + |
| 30 | +### Version Check |
| 31 | + |
| 32 | +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br> |
| 33 | +Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. |
| 34 | + |
| 35 | +```{r} |
| 36 | +library(plotly) |
| 37 | +packageVersion('plotly') |
| 38 | +``` |
| 39 | + |
| 40 | +### Set Default Trace Colors with colorway |
| 41 | + |
| 42 | +```{r, results = 'hide'} |
| 43 | +library(plotly) |
| 44 | +
|
| 45 | +x = seq(-1, 3, length=50) |
| 46 | +ones = rep(1, 50) |
| 47 | +a_list = seq(1, 3, length=7) |
| 48 | +b_list = seq(2, 14, length=7) |
| 49 | +df = data.frame(cbind(x, (outer((x*x), a_list) + outer(ones, b_list)))) |
| 50 | +
|
| 51 | +p <- plot_ly(df, x = ~x, y = ~V2, type = 'scatter', mode = 'lines') %>% |
| 52 | + add_trace(y = ~V3) %>% |
| 53 | + add_trace(y = ~V4) %>% |
| 54 | + add_trace(y = ~V5) %>% |
| 55 | + add_trace(y = ~V6) %>% |
| 56 | + add_trace(y = ~V7) %>% |
| 57 | + add_trace(y = ~V8) %>% |
| 58 | + layout(colorway = c('#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844')) |
| 59 | +
|
| 60 | +
|
| 61 | +# Create a shareable link to your chart |
| 62 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 63 | +chart_link = api_create(p, filename="colorway-example") |
| 64 | +chart_link |
| 65 | +``` |
| 66 | + |
| 67 | +```{r, echo=FALSE} |
| 68 | +chart_link |
| 69 | +``` |
| 70 | + |
| 71 | +#Reference |
| 72 | + |
| 73 | +See [https://plot.ly/r/reference](https://plot.ly/r/reference) for more information and chart attribute options! |
0 commit comments