11/1/2017 Shapes in R | Examples | Plotly
Help API Libraries R Shapes Fork on Github
plotly Developer Support PLOTCON Consulting
Navigation
New to Plotly
Shapes in R
Version Check How to use shapes in R. Two examples on highlighting regions by
adding shapes to your R charts.
Lines
Rectangles R Python [Link]
Circles
Reference
Back To R
New to Plotly?
Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or o ine mode.
We also have a quick-reference cheatsheet (new!) to help you get started!
Version Check
[Link] 1/11
11/1/2017 Shapes in R | Examples | Plotly
Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this
version.
Copy to clipboard!
library(plotly)
packageVersion('plotly')
Copy to clipboard!
## [1] '4.7.0.9000'
Lines
Copy to clipboard!
s <- [Link](0, 15)
p <- plot_ly(x = ~s, y = ~sin(s), mode = "lines")
# initiate a line shape object
line <- list(
type = "line",
line = list(color = "pink"),
xref = "x",
yref = "y"
)
[Link] 2/11
11/1/2017 Shapes in R | Examples | Plotly
lines <- list()
for (i in c(0, 3, 5, 7, 9, 13)) {
line[["x0"]] <- i
line[["x1"]] <- i + 2
line[c("y0", "y1")] <- sin(i + 1)
lines <- c(lines, list(line))
}
p <- layout(p, title = 'Highlighting with Lines', shapes = lines)
# Create a shareable link to your chart
# Set up API credentials: [Link]
chart_link = plotly_POST(p, filename="shapes/lines")
chart_link
[Link] 3/11
11/1/2017 Shapes in R | Examples | Plotly
4 4 Plot twist!
There's nothing here...
You might need to sign in to see this plot.
Plotly is free to view plots that have been shared with you.
Rectangles
[Link] 4/11
11/1/2017 Shapes in R | Examples | Plotly
Copy to clipboard!
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")
# add shapes to the layout
p <- layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y")))
# Create a shareable link to your chart
# Set up API credentials: [Link]
chart_link = plotly_POST(p, filename="shapes/unemployment")
chart_link
[Link] 5/11
11/1/2017 Shapes in R | Examples | Plotly
4 4 Plot twist!
There's nothing here...
You might need to sign in to see this plot.
Plotly is free to view plots that have been shared with you.
Circles
Circles are centered around ((x0+x1)/2, (y0+y1)/2))
[Link] 6/11
11/1/2017 Shapes in R | Examples | Plotly
Copy to clipboard!
library(plotly)
d <- diamonds[sample(nrow(diamonds), 1000), ]
d <- plot_ly(d, x = ~carat, y = ~price, text = ~paste("Clarity: ", clarity),
mode = "markers", color = ~carat, size = ~carat, opacity = ~carat)
d <- layout(d, title = 'Highlighting Regions with Circles',
shapes = list(
list(type = 'circle',
xref = 'x', x0 = .2, x1 = .7,
yref = 'y', y0 = 20, y1 = 3000,
fillcolor = 'rgb(50, 20, 90)', line = list(color = 'rgb(50, 20,
90)'),
opacity = 0.2),
list(type = 'circle',
xref = 'x', x0 = .75, x1 = 1.5,
yref = 'y', y0 = 2500, y1 = 7500,
fillcolor = 'rgb(30, 100, 120)', line = list(color = 'rgb(30, 100, 12
0)'),
opacity = 0.2),
list(type = 'circle',
xref = 'x', x0 = 1.6, x1 = 2.5,
yref = 'y', y0 = 12500, y1 = 18500,
fillcolor = 'rgb(90, 200, 75)', line = list(color = 'rgb(90, 200, 7
5)'),
opacity = 0.2)))
# Create a shareable link to your chart
# Set up API credentials: [Link]
chart_link = plotly_POST(d, filename="shapes/diamonds")
[Link] 7/11
11/1/2017 Shapes in R | Examples | Plotly
Copy to clipboard!
## Error: Client error: (400) Bad Request
## Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. U
se grid references instead. Raw data found at the following paths in the figure
[('data', 0, u'opacity')]
Copy to clipboard!
chart_link
[Link] 8/11
11/1/2017 Shapes in R | Examples | Plotly
4 4 Plot twist!
There's nothing here...
You might need to sign in to see this plot.
Plotly is free to view plots that have been shared with you.
Reference
Check out our reference page for more information on using shapes!
[Link] 9/11
11/1/2017 Shapes in R | Examples | Plotly
Still need help?
Contact Us
[Link] [Link] [Link]/plotly
For guaranteed 24 hour response
turnarounds, upgrade to a Developer
Support Plan.
API About Us Help
Documentation Team Knowledge Base
API Libraries Careers Benchmarks
REST APIs Plotly Blog Workshop
[Link] Modern Data
Hardware
Solutions Connect
Plans & Pricing Twitter Facebook Github
Enterprise
Education LinkedIn Google+
[Link] 10/11
11/1/2017 Shapes in R | Examples | Plotly
Education LinkedIn Google+
[Link]
Copyright © 2015 Plotly. All rights reserved. Terms of Service Privacy Policy
[Link] 11/11