|
| 1 | +--- |
| 2 | +name: Getting Started with Plotly |
| 3 | +permalink: r/getting-started/ |
| 4 | +description: Get started with Plotly's graphing library |
| 5 | +page_type: example_index |
| 6 | +layout: base |
| 7 | +language: r |
| 8 | +output: |
| 9 | + html_document: |
| 10 | + keep_md: true |
| 11 | +--- |
| 12 | + |
| 13 | +# Getting Started with Plotly for R |
| 14 | + |
| 15 | +Plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library [plotly.js](http://plot.ly/javascript). |
| 16 | +As of version 2.0 (November 17, 2015), Plotly graphs are rendered *locally* through the [htmlwidgets](http://www.htmlwidgets.org/) framework. |
| 17 | + |
| 18 | + |
| 19 | +#### Installation |
| 20 | + |
| 21 | +Plotly is now on CRAN! |
| 22 | + |
| 23 | +```r |
| 24 | +install.packages("plotly") |
| 25 | +``` |
| 26 | + |
| 27 | +Or install the latest development version (on GitHub) via devtools: |
| 28 | + |
| 29 | +```r |
| 30 | +devtools::install_github("ropensci/plotly") |
| 31 | +``` |
| 32 | + |
| 33 | +<br> |
| 34 | +RStudio users should download the latest [RStudio release](https://www.rstudio.com/products/rstudio/download/) for compatibility with htmlwidgets. |
| 35 | + |
| 36 | +#### Initialization for Offline Plotting |
| 37 | + |
| 38 | +By default, Plotly for R runs locally in your web browser or in the R Studio viewer. |
| 39 | + |
| 40 | +```r |
| 41 | +library(plotly) |
| 42 | +p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box") |
| 43 | +p |
| 44 | +``` |
| 45 | + |
| 46 | +Simply printing the Plotly object will render the chart locally in your web browser or in the R Studio viewer. |
| 47 | + |
| 48 | +<iframe style="border: none; width: 100%; height: 500px;" src="https://plot.ly/~chriddyp/1799.embed"></iframe> |
| 49 | + |
| 50 | +Plotly graphs are interactive. Click on legend entries to toggle traces, click-and-drag on the chart to zoom, double-click to autoscale, shift-and-drag to pan. |
| 51 | + |
| 52 | +#### Initialization for Online Plotting |
| 53 | + |
| 54 | +You can publish your charts to the web with Plotly's web service. |
| 55 | + |
| 56 | +1 - [Create a free Plotly account](https://plot.ly/api_signup):<br> |
| 57 | +A Plotly account is required to publish charts online. It's free to get started, and you control the privacy of your charts. |
| 58 | + |
| 59 | +2 - Save your authentication credentials<br> |
| 60 | +Find your authentication API keys [in your online settings](https://plot.ly/settings/api). Set them in your R session with: |
| 61 | + |
| 62 | +```r |
| 63 | +Sys.setenv("plotly_username"="your_plotly_username") |
| 64 | +Sys.setenv("plotly_api_key"="your_api_key") |
| 65 | +``` |
| 66 | + |
| 67 | +Save these commands in your [.Rprofile](http://www.statmethods.net/interface/customizing.html) file to be run every time you start R. |
| 68 | + |
| 69 | +3 - Publish your graphs to Plotly with `api_create` |
| 70 | + |
| 71 | +Use `filename` to title the file in your Plotly account. |
| 72 | + |
| 73 | +```r |
| 74 | +library(plotly) |
| 75 | +p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box") |
| 76 | +api_create(p, filename = "r-docs-midwest-boxplots") |
| 77 | +``` |
| 78 | + |
| 79 | +4 (optional) - Suppress auto open |
| 80 | + |
| 81 | +When following the instructions above, `api_create(p)` will auto open the created URL in the browser. To suppress this behavior, you can update your browser options in R: |
| 82 | + |
| 83 | +```r |
| 84 | +options(browser = 'false') |
| 85 | +api_create(p, filename = "r-docs-midwest-boxplots") |
| 86 | +``` |
| 87 | + |
| 88 | +#### Special Instructions for Chart Studio Enterprise Users |
| 89 | + |
| 90 | +Your API key for account on the public cloud will be different than the API key in [Chart Studio Enterprise](https://plot.ly/product/enterprise/). Visit <https://plotly.your-company.com/settings/api/> to find your Chart Studio Enterprise API key. Remember to replace "your-company.com" with the URL of your Chart Studio Enterprise server. |
| 91 | + |
| 92 | +If your company has a Chart Studio Enterprise server, change the R API endpoint so that it points to your company's Plotly server instead of Plotly's cloud. |
| 93 | + |
| 94 | +In your .RProfile write: |
| 95 | + |
| 96 | +```r |
| 97 | +Sys.setenv("plotly_domain"="https://plotly.your-company.com") |
| 98 | +``` |
| 99 | + |
| 100 | +Remember to replace "your-company" with the URL of your Chart Studio Enterprise server. |
| 101 | + |
| 102 | +<div class="row centered btnrow"> |
| 103 | + <a href="/r/" class="button no_underline">Make your first graph</a> |
| 104 | +</div> |
| 105 | + |
| 106 | +#### Online Plot Privacy |
| 107 | + |
| 108 | +Plots can be set to three different type of privacies: public, private or secret. |
| 109 | + |
| 110 | +* **public:** |
| 111 | + |
| 112 | + Anyone can view this graph. It will appear in your profile |
| 113 | + and can appear in search engines. You do not need to be |
| 114 | + logged in to Plotly to view this chart. |
| 115 | + |
| 116 | +* **private:** |
| 117 | + |
| 118 | + Only you can view this plot. It will not appear in the |
| 119 | + Plotly feed, your profile, or search engines. You must be |
| 120 | + logged in to Plotly to view this graph. You can privately |
| 121 | + share this graph with other Plotly users in your online |
| 122 | + Plotly account and they will need to be logged in to |
| 123 | + view this plot. This option is only available for Personal |
| 124 | + and Professional subscribers. |
| 125 | + |
| 126 | +* **secret:** |
| 127 | + |
| 128 | + Anyone with this secret link can view this chart. It will |
| 129 | + not appear in the Plotly feed, your profile, or search |
| 130 | + engines. If it is embedded inside a webpage or an IPython |
| 131 | + notebook, anybody who is viewing that page will be able to |
| 132 | + view the graph. You do not need to be logged in to view |
| 133 | + this plot. This option is only available for Personal |
| 134 | + and Professional subscribers. |
| 135 | + |
| 136 | +By default all plots are set to public. Users with a free account are limited to creating public plots. If you have private storage needs, please visit [Plotly products page](https://plot.ly/products). If you're a [Personal or Professional USER](https://plot.ly/settings/subscription/?modal=true&utm_source=api-docs&utm_medium=support-oss) and would like the setting for your plots to be private, you can specify sharing as private: |
| 137 | + |
| 138 | +```r |
| 139 | +api_create(filename = "private-graph", sharing = "private") |
| 140 | +``` |
| 141 | +For more examples on privacy settings please visit [R privacy documentation](https://plot.ly/r/privacy/) |
0 commit comments