Getting Started with Plotly in R

How to get started making charts with Plotly's R graphing library.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Getting Started with Plotly for R

plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library plotly.js.

As of version 2.0 (November 17, 2015), graphs created with the plotly R package are rendered locally through the htmlwidgets framework.

Installation

Download from CRAN

Use the install.package() function to install the plotly R package from CRAN. This version may not be the absolute latest version, so we recommend downloading from Github using the instructions below if you can.

install.packages("plotly")
Click to copy

Download from GitHub

Alternatively, you can install the latest development version of plotly from GitHub via the devtools R package:

devtools::install_github("ropensci/plotly")
Click to copy

Note For RStudio Users

RStudio users should ensure that they are using the latest RStudio release in order to ensure compatibility with the htmlwidgets R package.

Rendering Charts

By default, the plotly R package runs locally in your web browser or in the RStudio viewer.

library(plotly)
fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
fig
Click to copy

Simply printing the plot object will render the chart locally in your web browser or in the RStudio viewer.

Graphs created with the plotly R package are interactive!

Click on legend entries to hide/show traces, click-and-drag on the chart to zoom, double-click to autoscale, shift-and-drag to pan.

Next Steps

Once you have installed the plotly R package, learn more about the fundamentals of making charts and start making basic charts.

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)
Click to copy