Sei sulla pagina 1di 11

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 plotly.js

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
https://plot.ly/r/shapes/ 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 <- seq.int(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"
)
https://plot.ly/r/shapes/ 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: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="shapes/lines")
chart_link

https://plot.ly/r/shapes/ 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

https://plot.ly/r/shapes/ 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: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="shapes/unemployment")
chart_link

https://plot.ly/r/shapes/ 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))

https://plot.ly/r/shapes/ 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: https://plot.ly/r/getting-started
chart_link = plotly_POST(d, filename="shapes/diamonds")

https://plot.ly/r/shapes/ 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

https://plot.ly/r/shapes/ 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!

https://plot.ly/r/shapes/ 9/11
11/1/2017 Shapes in R | Examples | Plotly

Still need help?

Contact Us

community.plot.ly support.plot.ly github.com/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
Plotly.js Modern Data
Hardware

Solutions Connect

Plans & Pricing Twitter Facebook Github


Enterprise
Education LinkedIn Google+
https://plot.ly/r/shapes/ 10/11
11/1/2017 Shapes in R | Examples | Plotly
Education LinkedIn Google+
Plotly.js

Copyright © 2015 Plotly. All rights reserved. Terms of Service Privacy Policy

https://plot.ly/r/shapes/ 11/11

Potrebbero piacerti anche