(Guest post by Matt Sundquist on a lovely new service which is pro-actively supporting an API for R)
The Plotly R graphing library allows you to create and share interactive, publication-quality plots in your browser. Plotly is also built for working together, and makes it easy to post graphs and data publicly with a URL or privately to collaborators.
In this post, we’ll demo Plotly, make three graphs, and explain sharing. As we’re quite new and still in our beta, your help, feedback, and suggestions go a long way and are appreciated. We’re especially grateful for Tal’s help and the chance to post.
Sign-up and Install (more in documentation)
From within the R console:
Next, install plotly (a big thanks to Hadley, who suggested the GitHub route):
devtools::install_github("plotly/R-api") # ... # * DONE (plotly)
Then sign-up like this or at https://plot.ly/:
>library(plotly) >response = signup (username = 'username', email= 'youremail') … Thanks for signing up to plotly! Your username is: MattSundquist Your temporary password is: pw. You use this to log into your plotly account at https://plot.ly/plot. Your API key is: “API_Key”. You use this to access your plotly account through the API. To get started, initialize a plotly object with your username and api_key, e.g. >>> p <- plotly(username="MattSundquist", key="API_Key") Then, make a graph! >>> res <- p$plotly(c(1,2,3), c(4,2,1))
And we’re up and running! You can change and access your password and key in your homepage.
1. Overlaid Histograms:
Here is our first script.
library("plotly") p <- plotly(username="USERNAME", key="API_Key") x0 = rnorm(500) x1 = rnorm(500)+1 data0 = list(x=x0, type='histogramx', opacity=0.8) data1 = list(x=x1, type='histogramx', opacity=0.8) layout = list(barmode='overlay') response = p$plotly(data0, data1, kwargs=list(layout=layout)) browseURL(response$url)
Press “Save a Copy” to start styling from the GUI. So, find out “how would this look if I tweaked…,” or “what if I tweaked [element of graph I love to obsess over]?”
Plotly supports line charts, scatter plots, bubble charts, histograms, 2D histograms, box plots, heatmaps, and error bars. We also support log axes, date axes, multiple axes, subplots, and LaTeX. Or, analyze your data:
You can also embed your URL into this snippet to make an iframe (e.g., this Washington Post piece). You can adjust the width and height.
library(plotly) p <- plotly(username='USERNAME', key='API_KEY') zd <- matrix(rep(runif(38,0,38),26),26) #random.sample(range(0, 41),41) for j in range(8)] z <- tapply(z,(rep(1:nrow(z),ncol(z))),function(i)list(i)) cs <- list( c(0,"rgb(12,51,131)"), c(0.25,"rgb(10,136,186)"), c(0.5,"rgb(242,211,56)"), c(0.75,"rgb(242,143,56)"), c(1,"rgb(217,30,30)") ) data <- list( z = zd, scl = cs, type = 'heatmap' ) response <- p$plotly(data) browseURL(response$url)
library(plotly) p <- plotly(username='USERNAME', key='API_KEY') x <- c(seq(0,0,length=1000),seq(1,1,length=1000),seq(2,2,length=1000)) y <- c(rlnorm(1000,0,1),rlnorm(1000,0,2),rlnorm(1000,0,3)) s <- list( type = 'box', jitter = 0.5 ) layout <- list( title = 'Fun with the Lognormal distribution', yaxis = list( type = 'log' ) ) response <- p$plotly(x,y, kwargs = list(layout = layout, style=s)) browseURL(response$url)
Collaborating and Sharing: You’re in Control
Nicola Sommacal posted about Plotly this week, which we thoroughly appreciate. He mentioned privacy, and we wanted to make sure we highlighted:
(1) You control if graphs are public or private, and who you share with (like Google Docs)
(2) Public sharing in Plotly is free (like GitHub).
To share privately, press “Share” in our GUI or share with your script. Users you share with get an email and can edit and comment on graphs. That means no more emailing data, graphs, screenshots, and spreadsheets around: you can do it all in Plotly. You can also save and apply custom themes to new data to avoid re-making the same graphs with new data. Just upload and apply your theme.
Click here to see the graphs and code for our gallery. Happy plotting!