Fork on GitHub

Plotly R API

< Back to Main API Page

Installation

Option 1: Install from GitHub with devtools

First, install and load the devtools package. From within the R console, enter:

> install.packages("devtools")
> library("devtools")

Next, install plotly. From within the R console, enter:

> devtools::install_github("R-api", "plotly")
...
* DONE (plotly)

Option 2: Install from source

First, install the RJSONIO and RCurl libraries. From within the R console, type:

> install.packages("RJSONIO")
> install.packages("RCurl")

Next:

Click to download the latest R API.

Find the file path of downloaded file (e.g. /Users/chris/Downloads/plotly_0.3.tar.gz). In your R console, type:

> mypath = '/Users/chris/Downloads/plotly_0.3.tar.gz' # replace with your path
> install.packages(mypath,type='source')

Quickstart

Begin by importing the Plotly R library

> library(plotly)

This will return:

Loading required package: RCurl
Loading required package: bitops
Loading required package: RJSONIO

Initialize a plotly object to sign in to your Plotly API account

> p <- plotly(username= 'username_or_email', key= 'api_key')

Now you can make a simple API call using p$plotly(), and create your first graph!

> p$plotly(c(0,1,2,3,4),c(0,3,2,4,5))

Here is the response you'll receive:
(Copy and Paste the URL into your browser to see your graph!)

$url
[1] "https://plot.ly/~demos/1108"
$message
[1] ""
$warning
[1] ""
$filename
[1] "from api (154)"
$error
[1] ""

Initialization

To get started, load the plotly library and initialize a plotly object with your username and api key.

library(plotly)
p <- plotly(username=username_or_email, key=api_key)

You will interact with your plotly graphs through 3 methods:

p$plotly p$style p$layout

Authentication

New User - Sign Up

You can sign up on with Plotly's online GUI or through the API:

library(plotly)
username='anna.lyst' email='anna.lyst@plot.ly' response = signup(username, email) tmp_pw = response$tmp_pw api_key = response$api_key

Existing User - Generate API Key

Existing users display or reset their API key here. You will be prompted to log-in if you aren't already.

Display Key
Reset Key

Your key will display here.

Documentation Examples

The following are some examples of how to use the Plotly API. You can copy and paste the code from the examples and test it out! If you're signed in, your Username and API Key will pre-populate. If not, you can still try it out with the demo account!

Search for an example, or explore the links below!

Chart Types

Line and Scatter

Area Plots

Bar Charts

Error Bars

Box Plots

Histograms

Bubble Charts

Heatmaps

Text Charts

Mixed Types

Time Series

Multiple Axes, Insets and Subplots

Multiple Axes

Subplots

Insets

Layout

File Settings

Basic Line Plot

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x0 <- c(1,2,3,4)
y0 <- c(10,15,13,17)
x1 <- c(2,3,4,5)
y1 <- c(16,5,11,9)
response <- p$plotly(x0, y0, x1, y1)
url <- response$url
filename <- response$filename
Basic Line Plot

Line with Scatter

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace0 <- list(
  x = c(1,2,3,4),
  y = c(10,15,13,17),
  type = 'scatter',
  mode = 'markers')

trace1 <- list(
  x = c(2,3,4,5),
  y = c(16,5,11,9),
  type = 'scatter',
  mode = 'lines')

trace2 <- list(
  x = c(1,2,3,4),
  y = c(12,9,15,12),
  type = 'scatter',
  mode = 'lines+markers')

response <- p$plotly(trace0, trace1, trace2)
url <- response$url
filename <- response$filename
Line with Scatter

Styling Line and Scatter

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x1 <- c(1,2,3)
y1 <- c(4,5,6)
x2 <- c(1,2,3)
y2 <- c(2,10,12)

data1 <- list(
	x = x1,
	y = y1,
	name = 'Experiment',
	line = list(color='rgb(3,78,123)',
		width = 6,
		dash = 'dot'),
	marker = list(opacity=1.0,
		symbol = 'square',
		size = 12,
		color = 'rgb(54,144,192)',
		line = list(width=3,
			color = 'lightblue')))

data2 <- list(
	x = x2,
	y = y2,
	name = 'Control',
	line = list(
    color = 'purple',
		width = 4,
		dash = 'dashdot'),
	marker = list(
    opacity = 0.9,
		symbol = 'cross',
		size = 16,
		color = 'fuchsia',
		line = list(
      width = 0,
			color = '')))

response <- p$plotly(data1,data2)

# url and filename
url <- response$url
filename <- response$filename

Styling Line and Scatter

Basic Area Plot

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace0 <- list(
  x = c(1,2,3,4),
  y = c(0,2,3,5),
  fill = 'tozeroy'); options

trace1 <- list(
  x = c(1,2,3,4),
  y = c(3,5,1,7),
  fill = 'tonexty');

response <- p$plotly(trace0, trace1)
url <- response$url
filename <- response$filename

Basic Area Plot

Basic Bar Chart

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x1 <- c('giraffes','orangutans','monkeys')note
y1 <- c(20,14,23)
data <- list(
x = x1,
y = y1,
type = 'bar')options

response <- p$plotly(data)

# url and filename
url <- response$url
filename <- response$filename

Basic Bar Chart

Grouped Bar Chart

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

categories <- c('giraffes', 'orangutans', 'monkeys')

SF <- list(name='SF Zoo',
	x=categories,
	y=c(20, 14, 23),
	type='bar')

LA <- list(name='LA Zoo',
	x=categories,
	y=c(12,18,29),
	type='bar')

layout <- list(
	barmode='group',
	xaxis=list(type='category'),
	categories=categories)

response <- p$plotly(LA, SF, kwargs=list(layout=layout))
url <- response$url
filename <- response$filename

Grouped Bar Chart

Stacked Bar Chart

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

[...]	# ... SF and LA data from above

layout <- list(
barmode='stack',  options
xaxis <- list(
type = 'category'),
categories = categories)

response <- p$plotly(SF, LA, kwargs=list(layout=layout))
url <- response$url
filename <- response$filename

Stacked Bar Chart

Styling Bar Charts

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

categories <- list('giraffes', 'orangutans', 'monkeys')

SF <- list(name='SF Zoo',
  x = categories,
  y = c(20, 14, 23),
  type = 'bar',
  marker = list(
    color = 'orange',
    line = list(color = 'grey',
      width = 3)));

LA <- list(name = 'LA Zoo',
  x = categories,
  y = c(12,18,29),
  type = 'bar',
  marker = list(
    color = 'rgb(111, 168, 220)',
    line = list(
      color = 'grey',
      width = 3)))

layout <- list(
  title = 'Animal Population',
  barmode = 'group',
  yaxis = list(name='# of animals (thousands)'),
  xaxis = list(type='category'),
  categories = categories,
  bargap = 0.25,
  bargroupgap = 0.3,
  bardir = 'v')

response <- p$plotly(LA, SF, kwargs = list(layout=layout))
url <- response$url
filename <- response$filename

Styling Bar Charts

Basic Error Bars

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

data <- list(
  x = c(0,1,2),
  y = c(6,10,2),
  error_y = list(
    type='data', options
    array = c(1,2,3),
    visible = TRUE));
response <- p$plotly(data);
url <- response$url
filename <- response$filename

Basic Error Bars

Percentage, Constant or Square Root

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

data <- list(
  x = c(0,1,2),
  y = c(6,8,4),
  error_y = list(
    type = 'percent', options
    value = 50, note
    visible = TRUE));
response <- p$plotly(data);
url <- response$url
filename <- response$filename

Percentage, Constant or Square Root

Bar Chart with Error Bars

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

categories <- c('Trial 1', 'Trial 2', 'Trial 3')
control <- list(
  x = categories,
  y = c(3, 6, 4),
  type = 'bar',
  name = 'Control',
  error_y = list(
    type='data',
    array = c(1, 0.5, 1.5),  note
    visible = TRUE))

exp <- list(
  x = categories,
  y = c(4, 7, 3),
  type = 'bar',
  name = 'Experiment',
  error_y = list(
    type='data',
    array = c(0.5, 1, 2),
    visible = TRUE));

layout <- list(barmode = 'group')

response <- p$plotly(control, exp, kwargs = list(layout=layout));
url <- response$url
filename <- response$filename

Bar Chart with Error Bars

Basic Box Plot

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

box1 <- list(
  y = c(0,1,2,4),
  type = 'box')
box2 <- list(
  y = c(1,2,4,8),
 	type = 'box')

response <- p$plotly(list(box1,box2))
url <- response$url
filename <- response$filename

Basic Box Plot

Box Plot with Jitter

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

box <- list(
  y = rnorm(50),
	type = 'box',
	boxpoints = 'all', options
	jitter = 0.3, note
	pointpos = -1.8) note

response <- p$plotly(list(box))
url <- response$url
filename <- response$filename

Box Plot with Jitter

Basic Histogram

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- rnorm(500) # normally distributed vector
data = list(
  x = x,
	type = 'histogramx');

response <- p$plotly(data);
url <- response$url
filename <- response$filename

Basic Histogram

Stacked Histogram

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x0 <- rnorm(500)
x1 <- rnorm(500)+1

data0 <- list(
  x = x0,
	type = 'histogramx')

data1 <- list(
  x = x1,
	type = 'histogramx')

layout <- list(barmode = 'stack') options

response <- p$plotly(data0, data1, kwargs=list(layout=layout))
url <- response$url
filename <- response$filename

Stacked Histogram

Overlaid Histogram

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

[...] # data0, data1 from the above example

layout <- list(barmode = 'overlay')  options
response <- p$plotly(data0, data1, kwargs=list(layout=layout))
url <- response$url
filename <- response$filename

Overlaid Histogram

Styling Histograms

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x0 <- rnorm(500)
x1 <- rnorm(500)+1

data0 <- list(
  x = x0,
  type = 'histogramx',
  name = 'control',
  marker = list(  note
    color = 'fuchsia', options
    opacity = 0.75,
    line = list(
      color = 'grey',  note
      width = 0)),
  autobinx = FALSE,
  xbins = list(
    start = -3.2,
    end = 2.8,
    size = 0.2,
    histnorm = 'count')) options

data1 <- list(
  x = x1,
  name = 'experiment',
  type = 'histogramx',
  marker = list(
    color = 'rgb(255, 217, 102)',
    opacity = 0.75),
  autobinx = FALSE,
  xbins = list(
    start = -1.8,
    end = 4.2,
    size = 0.2))

layout <- list(
  barmode = 'overlay', options
  bargap = 0.25, note
  bargroupgap = 0.3, note
  bardir = 'v', options
  title = 'Sampled Results',
  xaxis = list(title='Value'),
  yaxis = list(title='Count'))

response <- p$plotly(data0, data1, kwargs=list(layout=layout))
url <- response$url
filename <- response$filename

Styling Histograms

2D Histogram

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- rnorm(500)
y <- rnorm(500)+1

data <- list(
  x = x,
  y = y,
	type = 'histogram2d')

response <- p$plotly(data)
url <- response$url
filename <- response$filename

2D Histogram

2D Histogram Options

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- rnorm(500);
y <- rnorm(500)+1;

data <- list(
  x = x,
  y = y,
  type = 'histogram2d',
  autobinx = FALSE,
  xbins = list(
    start = -3,
    end = 3,
    size = 0.5),
  autobiny = FALSE,
  ybins = list(
    start = -2.5,
    end = 4,
    size = 0.5),
  zauto = FALSE,
  zmin = 0,
  zmax = 0.4,
  histnorm = 'probability')options

response <- p$plotly(data)
url <- response$url
filename <- response$filename

2D Histogram Options

Basic Heatmap

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

z <- matrix(c(10,20,30,20,1,60,30,60,1), nrow=3, ncol=3)
data <- list(
  z = z,
  type = 'heatmap')

response <- p$plotly(data)
url <- response$url
filename <- response$filename

Basic Heatmap

Labelled Heatmap

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
y <- c('Morning', 'Afternoon', 'Evening')
z <- matrix(c(10,20,30,50,1,20,1,60,80,30,30,60,1,-10,20), nrow=3, ncol=5)

data <- list(
  x = x,
  y = y,
  z = z,
  type = 'heatmap')

response <- p$plotly(data)
url <- response$url
filename <- response$filename

Labelled Heatmap

Bar Chart with Line Plot

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x0 <- seq(0, 5, len=15)
y0 <- sin(x0) + runif(15)
data0 <- list(
  x = x0,
  y = y0,
	type = 'scatter')

x1 <- c(0, 1, 2, 3, 4, 5)
y1 <- c(1, 0.5, 0.7, -1.2, 0.3, 0.4)

data1 <- list(
  x = x1,
  y = y1,
	type = 'bar')

response <- p$plotly(data0, data1)
url <- response$url
filename <- response$filename

Bar Chart with Line Plot

2D Histogram with Scatter Overlay

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x0 <- rnorm(100)/5. + 0.5;
y0 <- rnorm(100)/5. + 0.5;

x1 <- rnorm(80)/7. + 0.1;
y1 <- rnorm(80)/8. + 1.1;

x <- c(x0,x1);
y <- c(y0,y1);

data0 <- list(
  x = x0,
  y = y0,
  marker = list(symbol='circle'),
  type = 'scatter', mode='markers')
data1 <- list(
  x = x1,
  y = y1,
  marker = list(symbol='cross'),
  type = 'scatter', mode='markers')

data_hist <- list(
  x = x,
  y = y,
  type = 'histogram2d')

response <- p$plotly(data0,data1,data_hist)
url <- response$url
filename <- response$filename

2D Histogram with Scatter Overlay

Legend Visibility

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,3,6,4,5,2,3,5,4)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,7,8,3,6,3,3,4)

# By default, when there is more than one trace, showlegend is set to TRUE.
layout <- list(
  'showlegend' = FALSE
);

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename
Legend Visibility

Legend Position Inside

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,3,6,4,5,2,3,5,4)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,7,8,3,6,3,3,4)



# Position the legend inside the graph by assigning x and y values of <= 1.
# e.g =
# list("x" = 0, "y" = 0) -- Bottom Left
# list("x" = 1, "y" = 0) -- Bottom Right
# list("x" = 1, "y" = 1) -- Top Right
# list("x" = 0, "y" = 1) -- Top Left
# list("x" = .5, "y" = 0) -- Bottom Center
# list("x" = .5, "y" = 1) -- Top Center

legendstyle <- list("x" = 1, "y" = 1)

layoutstyle <- list(legend = legendstyle)


response <- p$plotly(x,y,x2,y2, kwargs=list(layout = layoutstyle));

# url and filename
url <- response$url
filename <- response$filename

Legend Position Inside

Legend Position Outside

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,3,6,4,5,2,3,5,4)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,7,8,3,6,3,3,4)


# Position the legend outside of the graph by assigning one of the x and y values to either 100 or -100.
# e.g =
# list("x" = 100, "y" = 0) -- Outside Right Bottom
# list("x" = 100, "y" = 1) -- Outside Right Top
# list("x" = 100, "y" = .5) -- Outside Right Middle
# list("x" = 0, "y" = -100) -- Under Left
# list("x" = 0.5, "y" = -100) -- Under Center
# list("x" = 1, "y" = -100) -- Under Right

legendstyle <- list("x" = 100, "y" = 1)

layoutstyle <- list(legend = legendstyle)

response <- p$plotly(x,y,x2,y2, kwargs=list(layout = layoutstyle));

# url and filename
url <- response$url
filename <- response$filename

Legend Position Outside

Styling the Legend

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,3,6,4,5,2,3,5,4)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,7,8,3,6,3,3,4)



legendstyle = list(
  "x" = 0,
  "y" = 1,
  "bgcolor" = "#E2E2E2",
  "bordercolor" = "#FFFFFF",
  "borderwidth" = 2,
  "traceorder" = "normal",
  "font" = list(
    "color" = "#000",
    "family" = "sans-serif",
    "size" = 12)
)


layoutstyle <- list(legend=legendstyle)

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layoutstyle));

# url and filename
url <- response$url
filename <- response$filename

Styling the Legend

Legend Labels

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace0  = list(
    "x"<- c(0,1,2,3),
    "y"<- c(0,2,4,6),
    "name"= "Blue Trace"
    )

trace1 = list(
    "x"<- c(0,1,2,3),
    "y"<- c(8,4,2,0),
    "name" = "Orange Trace"
    )

response <- p$plotly(trace0, trace1);
# url and filename
url <- response$url
filename <- response$filename

Legend Labels

Basic Text Chart

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
      "x"<- c(0,1,2),
      "y"<- c(1,1,1),
      "name"= "Lines, Markers and Text",
      "text"<- c("Text A", "Text B", "Text C"),
      "type" = "scatter",
      "mode" = "lines+markers+text",
      "textposition" = "top"
    )
trace2 <- list(
      "x"<- c(0,1,2),
      "y"<- c(2,2,2),
      "name" = "Markers and Text",
      "text"<- c("Text D", "Text E", "Text F"),
      "type" = "scatter",
      "mode" = "markers+text",
      "textposition" = "bottom"
    )
trace3 <- list(
      "x"<- c(0,1,2),
      "y"<- c(3,3,3),
      "name"= "Lines and Text",
      "text"<- c("Text G", "Text H", "Text I"),
      "type" = "scatter",
      "mode" = "lines+text",
      "textposition" = "bottom"
    )

#hide legend
layout <- list(
  'showlegend' = FALSE
)

response <- p$plotly(trace1, trace2, trace3, kwargs = list(layout=layout));
url <- response$url
filename <- response$filename

Basic Text Chart

Styling Text Charts

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
      "x" = c(0,1,2),
      "y" = c(1,1,1),
      "name" = "Lines, Markers and Text",
      "text" = c("Text A", "Text B", "Text C"),
      "type" = "scatter",
      "mode" = "lines+markers+text",
      "textposition" = "top",
      "textfont" = list(
        "color" = "#1f77b4",options
        "family" = "sans serif",options
        "size" = 18)
)

trace2 <- list(
      "x" = c(0,1,2), "y" = c(2,2,2),
      "name" = "Lines and Text",
      "text" =  c("Text G", "Text H", "Text I"),
      "type" = "scatter",
      "mode" = "lines+markers+text",
      "textposition" = "bottom",
      "textfont" = list(
        "color" = "#ff7f0e",options
        "family" = "sans serif",options
        "size"= 18)
)

#hide legend
layout <- list(
  'showlegend' = FALSE
);

response <- p$plotly(trace1, trace2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Styling Text Charts

Axes Booleans

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(8,7,6,5,4,3,2,1,0)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,1,2,3,4,5,6,7,8)

axesstyle <- list(
      'autorange' = TRUE,
      'autotick' = TRUE,
      'showgrid' = FALSE,
      'showline' = FALSE,note
      'showticklabels' = FALSE,
      'zeroline' = FALSE,
      'ticks'= ''options
);

layout <- list(
    'xaxis' = axesstyle,
    'yaxis' = axesstyle,
    'showlegend' = FALSE
);

response <- p$plotly(x,y,x2,y2, kwargs = list(layout = layout));
url <- response$url
filename <- response$filename

Axes Booleans

Linear and Log Axes

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(8,7,6,5,4,3,2,1,0)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,1,2,3,4,5,6,7,8)


axesstyle <- list(
    'autorange' = TRUE,
    'type' = 'log',options
)

layout <- list(
    'xaxis' = axesstyle,
    'yaxis' = axesstyle
)


response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Linear and Log Axes

Manual Range

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(8,7,6,5,4,3,2,1,0)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,1,2,3,4,5,6,7,8)


axesstyle <- list(
    'range' = c(2,5)options
);

layout <- list(
    'xaxis' = axesstyle,
    'yaxis' = axesstyle
);


response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename
Manual Range

Range Mode

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(2,4,6)
y <- c(-3,0,3)

layout <- list(
    'showlegend' = FALSE,
    'xaxis' = list(
        'autorange' = TRUE,
        'rangemode' = 'tozero'options
    ),
    'yaxis' = list(
        'autorange' = TRUE,
        'rangemode' = 'nonnegative'options
    )
);

response <- p$plotly(x,y, kwargs = list(layout=layout));
url <- response$url
filename <- response$filename

Range Mode

Axes Lines

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(8,7,6,5,4,3,2,1,0)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,1,2,3,4,5,6,7,8)

axesstyle <- list(
    'showline' = TRUE, note
    'mirror' = 'ticks',optionsnote
    'linecolor' = '#636363',options
    'linewidth' = 6,

    'showgrid' = TRUE, note
    'gridcolor' = '#bdbdbd',options
    'gridwidth' = 2,

    'zeroline' = TRUE, note
    'zerolinecolor' = '#969696',options
    'zerolinewidth' = 4
);

layout <- list(
  'xaxis' = axesstyle,
  'yaxis' = axesstyle
);

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename
Axes Lines

Axes Ticks

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(8,7,6,5,4,3,2,1,0)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,1,2,3,4,5,6,7,8)

axesstyle <- list(
      'autotick' = FALSE,
      'tick0' = 0,note
      'dtick' = .25,note
      'ticks' = 'outside',options
      'tickcolor' = '#000',options
      'ticklen' = 8,note
      'tickwidth' = 4
);

layout <- list(
  'xaxis' = axesstyle,
  'yaxis' = axesstyle
);

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename
Axes Ticks

Axes Labels

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(8,7,6,5,4,3,2,1,0)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,1,2,3,4,5,6,7,8)

axesstyle <- list(
      'title' = 'AXIS TITLE',
      'titlefont' = list(
        'color' = 'lightgrey',options
        'family' = 'Arial, sans-serif',options
        'size' = 18),
      'showticklabels' = TRUE,
      'tickfont' = list(
        'color' = 'black',options
        'family' = 'Old Standard TT, serif',options
        'size' = 14),
      'tickangle' = 45,options
      'showexponent' = 'All',options
      'exponentformat' = 'e'options
);

layout <- list(
  'xaxis' = axesstyle,
  'yaxis' = axesstyle
);

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Axes Labels

Reversed Axes

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(1,2)
y <- c(1,2)

layout <- list(
    'xaxis'= list(
      'autorange'= 'reversed'
    )
)

filename <- 'filename'
response <- p$plotly(x,y, kwargs=list(layout=layout,filename=filename));
url <- response$url

Reversed Axes

Date Axes

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

#Plotly understands dates and times in format YYYY-MM-DD HH:MM:SS (ie, 2009-10-04 22:23:00)

x <- c('2013-10-04 22:23:00', '2013-11-04 22:23:00', '2013-12-04 22:23:00')
y <- c(1,3,6)

response <- p$plotly(x,y)
url <- response$url
filename <- response$filename

Date Axes

Simple Double Axes

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(40,50,60),
      "name" = "yaxis data"
)

trace2 <- list(
      "x" = c(2,3,4),
      "y" = c(4,5,6),
      "yaxis" = "y2", # this will reference the yaxis2 object in the layout object
      "name" = "yaxis2 data"
)

layout <- list (
   "yaxis" = list(
      "title" = "yaxis title" # optional
   ),
   "yaxis2" = list(
      "title" = "yaxis2 title", # optional
      "titlefont" = list(
         "color" ="rgb(148, 103, 189)"
      ),
      "tickfont" = list(
         "color" = "rgb(148, 103, 189)"
      ),
      "overlaying" = "y",
      "side" = "right"
   ),
   "title" = "Double Y Axis Example"
)

response <- p$plotly(trace1,trace2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename
Simple Double Axes

Simple Multiple Axes

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

colors <- c(
    '#1f77b4', # muted blue
    '#ff7f0e', # safety orange
    '#2ca02c', # cooked asparagus green
    '#d62728', # brick red
    '#9467bd', # muted purple
    '#8c564b', # chestnut brown
    '#e377c2', # raspberry yogurt pink
    '#7f7f7f', # middle gray
    '#bcbd22', # curry yellow-green
    '#17becf'); #blue-teal


trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(4,5,6),
    "name" = "yaxis1 data"
   )
trace2 <- list(
      "x" = c(2,3,4),
      "y" = c(40,50,60),
      "name" = "yaxis2 data",
      "yaxis" = "y2" # this references the "yaxis2" object in layout
   )
trace3 <- list(
      "x" = c(3,4,5),
      "y" = c(400,500,600),
      "name" = "yaxis3 data",
      "yaxis" = "y3"
    )
trace4 <- list(
    "x" = c(4,5,6),
    "y" = c(40000,50000,60000),
    "name" = "yaxis4 data",
    "yaxis" = "y4"
   )
trace5 <- list(
    "x" = c(5,6,7),
    "y" = c(400000,500000,600000),
    "name" = "yaxis5 data",
    "yaxis" = "y5"
   )
trace6 <- list(
    "x" = c(6,7,8),
    "y" = c(4000000,5000000,6000000),
    "name" = "yaxis6 data",
    "yaxis" = "y6"
   )

layout <- list(
    "width" = 800,
    "xaxis" = list(
        "domain" = c(0.3,0.7)
    ),
   "yaxis" = list(
      "title" =  "yaxis title",
      "titlefont" = list(
         "color" = colors[0]
      ),
      "tickfont" = list(
         "color" = colors[0]
      )
   ),
   "yaxis2" = list(
      "overlaying" = "y",
      "side" = "left",
      "anchor" = "free",
      "position" = 0.15,

      "title" =  "yaxis2 title",
      "titlefont" = list(
         "color" = colors[1]
      ),
      "tickfont" = list(
         "color" = colors[1]
      )
   ),
   "yaxis3" = list(
      "overlaying" = "y",
      "side" = "left",
      "anchor" = "free",
      "position" = 0,

      "title" =  "yaxis3 title",
      "titlefont" = list(
         "color" = colors[2]
      ),
      "tickfont" = list(
         "color" = colors[2]
      )
   ),

   "yaxis4" = list(
      "overlaying" = "y",
      "side" = "right",
      "anchor" = "x",

      "title" =  "yaxis4 title",
      "titlefont" = list(
         "color" = colors[3]
      ),
      "tickfont" = list(
         "color" = colors[3]
      )
   ),

   "yaxis5" = list(
      "overlaying" = "y",
      "side" = "right",
      "anchor" = "free",
      "position" = 0.85,

      "title" =  "yaxis5 title",
      "titlefont" = list(
         "color" = colors[4]
      ),
      "tickfont" = list(
         "color" = colors[4]
      )
   ),

   "yaxis6" = list(
      "overlaying" = "y",
      "side" = "right",
      "anchor" = "free",
      "position" = 1.0,

      "title" =  "yaxis6 title",
      "titlefont" = list(
         "color" = colors[5]
      ),
      "tickfont" = list(
         "color" = colors[5]
      )
    ),
    "title" =  "multiple y-axes example"
)


response <- p$plotly(trace1,trace2,trace3,trace4,trace5,trace6, kwargs = list(layout = layout));
url <- response$url
filename <- response$filename

Simple Multiple Axes

Simple Subplot

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(4,5,6),
   ),
trace2 <- list(
      "x" = c(20,30,40),
      "y" = c(50,60,70),
      "xaxis" = "x2",
      "yaxis" = "y2"
   )


layout <- list(
    "xaxis" = list(
        "domain" = c(0,0.45) # i.e. let the first x-axis span the first 45% of the plot width
    ),
    "xaxis2" = list(
        "domain" = c(0.55,1) # i.e. let the second x-axis span the latter 45% of the plot width
    ),
    "yaxis2" = list(
        "anchor" = "x2" # i.e. bind the second y-axis to the start of the second x-axis
    )
)

response <- p$plotly(trace1,trace2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Simple Subplot

Custom Sized Subplot

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(4,5,6),
   ),
   list(
      "x" = c(20,30,40),
      "y" = c(50,60,70),
      "xaxis" = "x2",
      "yaxis" = "y2"
   )


layout <- list(
    "xaxis" = list(
        "domain" = c(0,0.7) # i.e. let the first x-axis span the first 70% of the plot width
    ),
    "xaxis2" = list(
        "domain" = c(0.8,1) # i.e. let the second x-axis span the latter 20% of the plot width
    ),
    "yaxis2" = list(
        "anchor" = "x2" # i.e. bind the second y-axis to the start of the second x-axis
    )
)

response <- p$plotly(trace1,trace2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Custom Sized Subplot

Multiple Subplots

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(4,5,6)
   )
trace2 <- list(
      "x" = c(20,30,40),
      "y" = c(50,60,70),
      "xaxis" = "x2",
      "yaxis" = "y2"
   )
trace3 <- list(
      "x" = c(300,400,500),
      "y" = c(600,700,800),
      "xaxis" = "x3",
      "yaxis" = "y3"
   )
trace4 <- list(
      "x" = c(4000,5000,6000),
      "y" = c(7000,8000,9000),
      "xaxis" = "x4",
      "yaxis" = "y4"
   )

layout <- list(
    "xaxis" = list(
        "domain"=c(0,0.45) # let the first x-axis span the first 45% of the plot width
    ),
    "yaxis" = list(
        "domain" = c(0,0.45) # # and let the first y-axis span the first 45% of the plot height
    ),
    "xaxis2" = list(
        "domain" = c(0.55,1) # and let the second x-axis span the latter 45% of the plot width
    ),
    "yaxis2" = list(
        "domain" = c(0,0.45), # and let the second y-axis span the first 45% of the plot height
        "anchor" = "x2" # bind the horizontal position of the second y-axis to the start of the second x-axis
    ),
    "xaxis3" = list(
        "domain" = c(0,0.45),
        "anchor" = "y3" # bind the vertical position of this axis to the start of yaxis3
    ),
    "yaxis3" = list(
        "domain" = c(0.55,1)
    ),
    "xaxis4" = list(
        "domain" = c(0.55,1),
        "anchor" = "y4" # bind the vertical position of this axis to the start of yaxis4
    ),
    "yaxis4" = list(
        "domain" = c(0.55,1),
        "anchor" = "x4" # bind the horizontal position of this axis to the start of xaxis4
    )
)

response <- p$plotly(trace1,trace2,trace3,trace4, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Multiple Subplots

Subplots with Shared Axes

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(2,3,4)
   )
trace2 <- list(
      "x" = c(20,30,40),
      "y" = c(5,5,5),
      "xaxis" = "x2",
      "yaxis" = "y"
   )
trace3 <- list(
      "x" = c(2,3,4),
      "y" = c(600,700,800),
      "xaxis" = "x",
      "yaxis" = "y3"
   )
trace4 <- list(
      "x" = c(4000,5000,6000),
      "y" = c(7000,8000,9000),
      "xaxis" = "x4",
      "yaxis" = "y4"
   )


layout <- list(
    "xaxis" = list(
        "domain" = c(0,0.45) # let the first x-axis span the first 45% of the plot width
    ),
    "yaxis" = list(
        "domain" = c(0,0.45) # and let the first y-axis span the first 45% of the plot height
    ),
    "xaxis2" = list(
        "domain" = c(0.55,1) # and let the second x-axis span the latter 45% of the plot width
    ),
    "yaxis3" = list(
        "domain" = c(0.55,1)
    ),
    "xaxis4" = list(
        "domain" = c(0.55,1),
        "anchor" = "y4" # bind the vertical position of this axis to the start of yaxis4
    ),
    "yaxis4" = list(
        "domain" = c(0.55,1),
        "anchor" = "x4" # bind the horizontal position of this axis to the start of xaxis4
    )
)

response <- p$plotly(trace1,trace2,trace3,trace4, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Subplots with Shared Axes

Stacked Subplots

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
  'x' = c(0,1,2),
  'y' = c(10,11,12)
)
trace2 <- list(
  'x' = c(2,3,4),
  'y' = c(100,110,120),
  'yaxis'= 'y2',
  'xaxis'= 'x2'
)
trace3 <- list(
  'x' = c(3,4,5),
  'y' = c(1000,1100,1200),
  'yaxis'= 'y3',
  'xaxis'= 'x3'
)

layout=list(
    'yaxis' = list('domain' = c(0,0.8/3.)),
    'yaxis2' = list('domain' = c(0.8/3+0.1,2*0.8/3+0.1)),
    'yaxis3' = list('domain' = c(2*0.8/3+0.2,1)),
    'xaxis2' = list('anchor' = 'y2'),
    'xaxis3' = list('anchor' = 'y3'),
    'legend' = list('traceorder' = 'reversed')
)

response <- p$plotly(trace1,trace2,trace3, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Stacked Subplots

Stacked Subplots with Coupled X-Axis

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
  'x' = c(0,1,2),
  'y' = c(10,11,12)
)
trace2 <- list(
  'x' = c(2,3,4),
  'y' = c(100,110,120),
  'yaxis'= 'y2'
)
trace3 <- list(
  'x' = c(3,4,5),
  'y' = c(1000,1100,1200),
  'yaxis'= 'y3'
)

layout <- list(
    'yaxis' = list('domain' = c(0,1./3.)),
    'yaxis2' = list('domain' = c(1./3,2./3.)),
    'yaxis3' = list('domain' = c(2./3.,1)),
    'legend' = list('traceorder' = 'reversed')
)

response  <- p$plotly(trace1,trace2,trace3, kwargs=list(layout=layout));
url  <- response$url
filename  <- response$filename

Stacked Subplots with Coupled X-Axis

Simple Inset

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

trace1 <- list(
    "x" = c(1,2,3),
    "y" = c(4,3,2),
   )
trace2 <- list(
      "x" = c(20,30,40),
      "y" = c(30,40,50),
      "xaxis" = "x2",
      "yaxis" = "y2"
   )

layout <- list(
    "xaxis2" = list(
        "domain" = c(0.6, 0.95),
        "anchor" = "y2"
    ),
    "yaxis2" = list(
        "domain" = c(0.6, 0.95),
        "anchor" = "x2"
    )
)

response <- p$plotly(trace1,trace2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Simple Inset

Plot and Margin Size

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,2,3,4,5,6,7,8)

layout <- list(
    'autosize' = FALSE,
    'height' = 500,
    'width' = 500,
    'margin' = list(
      't' = 100,
      'b' = 100,
      'r' = 50,
      'l' = 50,
      'pad' = 4 # space between border and gridlines
    ),
    "plot_bgcolor" = "#c7c7c7",
    "paper_bgcolor" = "#7f7f7f"
)


response <- p$plotly(x,y, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Plot and Margin Size

Global Font

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,2,3,4,5,6,7,8)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(1,0,3,2,5,4,7,6,8)

layout <- list(
    "title" = "Global Font",
    "font" = list(
      "color" = "#7f7f7f",
      "family" = "Courier New, monospace",
      "size" = 18
      )
)


response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Global Font

Titles, Axes Names and Trace Names

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,2,3,4,5,6,7,8)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(1,0,3,2,5,4,7,6,8)

data <- list(
  list(
    'x' = x,
    'y' = y,
    'name' = 'Name of Trace 1'
  ),
  list(
    'x' = x2,
    'y' = y2,
    'name' = 'Name of Trace 2'
  )
)

layout <- list(
  "title" = "Plot Title",
  "xaxis" = list(
    "title" = "x Axis",
    "titlefont" = list(
      "color" = "#7f7f7f",
      "family" = "Courier New, monospace",
      "size" = 18
    )
  ),
  "yaxis" = list(
    "title" = "y Axis",
    "titlefont" = list(
      "color" = "#7f7f7f",
      "family" = "Courier New, monospace",
      "size" = 18
    )
  )
)


response <- p$plotly(data, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Titles, Axes Names and Trace Names

Simple Annotation

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,3,2,4,3,4,6,5)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,5,1,2,2,3,4,2)


layout <- list(
    "showlegend" = FALSE,
    "annotations" = list(list(
        "text" = "Annotation Text",
        "xref" = "x",
        "yref" = "y",
        "x" = 2,
        "y" = 5,
        "ax" = 0,
        "ay" = -40,
        "showarrow" = TRUE,
        "arrowhead" = 7
    ))
)

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Simple Annotation

Multiple Annotations

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,3,2,4,3,4,6,5)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,5,1,2,2,3,4,2)


layout <- list(
    "showlegend" = FALSE,
    "annotations" = list(list(
        "text" = "Annotation Text",
        "xref" = "x",
        "yref" = "y",
        "x" = 2,
        "y" = 5,
        "ax" = 0,
        "ay" = -40,
        "showarrow" = TRUE,
        "arrowhead" = 7
    ),
    list(
        "text" = "Annotation Text 2",
        "xref" = "x",
        "yref" = "y",
        "x" = 4,
        "y" = 4,
        "ax" = 0,
        "ay" = -40,
        "showarrow" = TRUE,
        "arrowhead" = 7
    ))
)

response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Multiple Annotations

Styling Annotations

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,3,2,4,3,4,6,5)
x2 <- c(0,1,2,3,4,5,6,7,8)
y2 <- c(0,4,5,1,2,2,3,4,2)


layout <- list(
    "showlegend" = FALSE,
    "annotations" = list(list(
        "text" = "max=5",
        "xref" = "x",
        "yref" = "y",
        "x" = 2,
        "y" = 5,
        "ax" = 20,
        "ay" = -30,
        "arrowhead" = 2,
        "arrowsize" = 1,
        "arrowwidth" = 2,
        "arrowcolor" = "#636363",
        "bgcolor" = "#ff7f0e",
        "bordercolor" = "#c7c7c7",
        "borderpad" = 4,
        "borderwidth" = 2,
        "font" = list(
            "color" = "#ffffff",
            "family" = "Courier New, monospace",
            "size" = 16
        ),
        "opacity" = 0.8,
        "showarrow" = TRUE,
        "align" = "center"
    ))
)
response <- p$plotly(x,y,x2,y2, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Styling Annotations

Simple Bubblechart

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

data <- list(
    list(
    'x' = c(1,2,3,4),
    'y' = c(10,11,12,13),
    'marker' = list(
        'size' =  c(12, 22, 32, 42), # diameter
        'color' = c('hsl(0,100,40)', 'hsl(33,100,40)', 'hsl(66,100,40)', 'hsl(99,100,40)'),
        'opacity' = c(.60, .70, .80, .90)
     ),
     'mode' = 'markers'
     ),
    list(
      'x' = c(1,2,3,4),
      'y' =  c(11,12,13,14),
      'marker' = list(
        'color'= 'rgb(31, 119, 180)',
        'size' = 18,
        'symbol' = c('circle', 'square', 'diamond', 'cross')
    ),
    'mode'= 'markers'
    ),
    list(
    'x' = c(1,2,3,4),
    'y' = c(12,13,14,15),
    'marker' = list(
        'size' = 18,
        'line' = list(
            'color' = c('rgb(120,120,120)', 'rgb(120,120,120)', 'red', 'rgb(120,120,120)'),
            'width' = c(2, 2, 6, 2)
        )
    ),
    'mode' = 'markers'
    )
)

layout <- list('showlegend'=FALSE)

response <- p$plotly(data, kwargs=list(layout=layout));
url <- response$url
filename <- response$filename

Simple Bubblechart

Private Graph

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,2,4)
y <- c(0,4,2)

response <- p$plotly(x,y, kwargs=list(world_readable=FALSE));
url <- response$url
filename <- response$filename

Private Graph

Public Graph

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,2,4)
y <- c(0,4,2)

response <- p$plotly(x,y, kwargs=list(world_readable=TRUE));
url <- response$url
filename <- response$filename

Public Graph

File Naming

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x <- c(0,2,4)
y <- c(0,4,2)

response <- p$plotly(x,y, kwargs=list(filename='myplotlygraph'));
#or
response <- p$plotly(x,y, kwargs=list(filename='myfolder/myplotlygraph'));

url <- response$url
filename <- response$filename
File Naming

Data Visibility

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

data <- list(

  trace0  = list(
    'x' = c(0,1,2,3),
    'y' = c(0,2,4,6),
    'visible'= TRUE
  ),

  trace1 = list(
    'x' = c(0,1,2,3),
    'y' = c(8,4,2,0),
    'visible'= FALSE
 )
)
response <- p$plotly(data);
url <- response$url
filename <- response$filename

Data Visibility

New Graph

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(3,4),
	kwargs=list(filename='new plot',
	fileopt='new'))

API RESPONSE:

$url
[1] "https://plot.ly/~demos/1058"
$message
[1] ""
$warning
[1] ""
$filename
[1] "new plot"
$error
[1] ""
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(3,4),
	kwargs=list(filename='new plot',
	fileopt='new'))
Check out the filename and url in the responses. We specified the same filename in both calls, but since we used the fileopt 'new', our second call appended '(1)' to the filename and created a new unique url for that graph. The first graph was not overwritten.

API RESPONSE:

$url
[1] "https://plot.ly/~demos/1059"
$message
[1] ""
$warning
[1] ""
$filename
[1] "new plot (1)"
$error
[1] ""

Overwrite

The overwrite option will save the plot with the specified filename no matter what. If a plot already exists under the same name, it will overwrite it and create a revision. If the plot with that filename doesn't exist, it will create it!
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(3,4),
	kwargs=list(filename='same plot',
	fileopt='overwrite'))

API RESPONSE:

$url
[1] "https://plot.ly/~demos/1055"
$message
[1] ""
$warning
[1] ""
$filename
[1] "same plot"
$error
[1] ""
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(10,20), c(30,40),
	kwargs=list(filename='same plot',
	fileopt='overwrite'))

API RESPONSE:

$url
[1] "https://plot.ly/~demos/1055"
$message
[1] ""
$warning
[1] ""
$filename
[1] "same plot"
$error
[1] ""

Append

The "append" fileopt will append the data that you supplied as new traces. If the filename doesn't exist, it will create a new file with that data.
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(1,2),
	kwargs=list(filename='append plot',
	fileopt='append'))
Here we're starting with one trace.
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(3,4),
	kwargs=list(filename='append plot',
	fileopt='append'))
Here we are appending a new trace with points (1,3) and (2,4).
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(4,5), c(1,2), c(5,6),
	kwargs=list(filename='append plot',
	fileopt='append'))
Here we are appending two new traces at points (1,4), (2,5) and (1,5), (2,6).

Extend

The "extend" fileopt will extend existing traces with your new data.
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(1,2), c(1,2), c(1,2), c(2,3), c(1,2), c(3,4),
	kwargs=list(filename='extend plot',
	fileopt='extend'))
If there is no data in the plot, 'extend' will create new traces.
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(3,4), c(2,1), c(3,4), c(3,2), c(3,4), c(4,3),
	kwargs=list(filename='extend plot',
	fileopt='extend'))
If there is data in the plot, the existing traces will be extended with the data in the order that it is supplied.
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(4,5), c(2,3),
	kwargs=list(filename='extend plot',
	fileopt='extend',
	traces=list(1)))
You can also choose which trace to extend with the "traces" argument. Here we extend the second trace. (traces=[1])
library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

p$plotly(c(5,6), c(3,3), c(4,5), c(1,1),
	kwargs=list(filename='extend plot',
	fileopt='extend',
	traces=list(2,0)))
The traces option accepts an array as an argument. Here we extend the 3rd trace in the plot with the first data set that we supply, and the 1st trace in the plot with the second data set.

Setting Style


A simple example:
p$style(list(type='bar'), list(type='scatter',marker=list(symbol='square', color='marker')))
This sets the first data trace to a bar chart and the second data trace to a scatter plot with purple square scatter points.

The general styling syntax is
> p$style(data1,...,datan, kwargs=kwargs)
datai is a Plotly formatted data list that customizes the style of the i'th trace.

kwargs is an optional keyed list with the following optional keys:
kwargs$filename: filename of the plot inside your plotly account
kwargs$traces: a vector that specifies which traces to style. If not specified, then traces are styled in the order of the data lists or, if supplied, applied to the trace specified by the datai$name value.

Setting Layout

A simple example:
p$layout({'title':'Hello World'})
This sets your plot's title to Hello World.
The general syntax is
p$layout(layout, kwargs)
layout is a Plotly formatted layout dictionary that customizes the style of the layout, the axes, and the legend.

kwargs represents optional arguments. Accepted arguments are:
filename: filename of the plot inside your plotly account. If not specified, then the style is applied to the last plot that was made in your API session.

File Sharing

library(plotly)
p <- plotly(username='rAPI', key='yu680v5eii')

x = c(0,2,4)
y = c(0,4,2)

p$plotly(x,y, kwargs=list(filename='lab_results/experiment1'));

When you share a folder in the Plotly tool, files created within that folder using the API are automatically shared.

In this case user demos has shared the folder lab_results with batman and robin.

When demos saves a file (experiment1 inside lab_results, batman

IFrames

Plotly makes it easy to embed your interactive graphs into your HTML as iframes.
For example, the graph below was embedded using the following code:

<iframe id="igraph" style='border:none' src="https://plot.ly/~Dreamshot/212/550/550" width="100%"></iframe>
Replace the src attribute with the url to your plotly graph. The last two values in the src attribute represent the width and height, respectively.

Wide Example


<iframe id="igraph" style='border:none' src="https://plot.ly/~Dreamshot/212/800/600" width="100%"></iframe>

Pocket Sized Example


<iframe id="igraph" style='border:none' src="https://plot.ly/~Dreamshot/212/400/400" width="100%"></iframe>

Formal Syntax

A simple example:
> p$plotly(c(0,1), c(10,11))
or in an alternate syntax
> p$plotly(list(x=c(0,1),y=c(10,11))
The general plotting syntax is
> p$plotly(x1, y1,...,xn,yn, kwargs=kwargs)
or
> p$plotly(data1,...,datan, kwargs=kwargs)
x1, y1 are lists with numbers or chars as elements.
datai is a Plotly formatted list with at least the fields x and y, i.e. data1 = list(x=x1, y=y1) but can contain more fields with styling and chart-type data. For example,
data1=list(x=c(0,1),y=c(10,11),name='Exp Results', type='scatter', marker=list(symbol='square',color='purple'))
specifies a scatter trace with purple square data markers that is named Exp Results.

kwargs is an optional keyed list with the following optional keys:
kwargs$filename: filename of the plot inside your plotly account
kwargs$fileopt: file-options: 'new', 'overwrite', or 'append'
kwargs$layout: a Plotly formatted layout list describing layout options.
kwargs$style: a Plotly formatted style list describing the style of all the data traces.
kwargs$world_readable: a boolean. If TRUE, then the graph will be public. If FALSE, then only you can see it. Default is TRUE.