Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 3.28 KB

2017-10-18-extending_ggplotly.md

File metadata and controls

115 lines (81 loc) · 3.28 KB
title name permalink description layout thumbnail language page_type has_thumbnail display_as order output
Extending ggplotly | Examples | Plotly
Extending ggplotly
ggplot2/extending-ggplotly/
How modify the plotly object after ggplot2 conversion.
base
thumbnail/extending_ggplotly.png
ggplot2
example_index
true
fundamentals
1
html_document
keep_md
true

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 offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

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.

library(plotly)
packageVersion('plotly')
## [1] '4.7.1.9000'

Modify with Style

library(plotly)

p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line()

gg <- ggplotly(p)

gg <- style(gg, line = list(color = 'gold'), hoverinfo = "y", traces = 1)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(gg, filename="extending/style")
chart_link
<iframe src="https://plot.ly/~RPlotBot/5187.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Modify with Build

library(plotly)

p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line()

gg <- ggplotly(p)

gg <- plotly_build(p)

gg$x$data[[1]]$line$color <- 'blue'

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(gg, filename="extending/build")
chart_link
<iframe src="https://plot.ly/~RPlotBot/5189.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Modify with LayerData

library(plotly)

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
   geom_point() + geom_smooth()

p <- p %>%
  ggplotly(layerData = 2, originalData = F) %>%
  add_fun(function(p) {
    p %>% slice(which.max(se)) %>%
      add_segments(x = ~x, xend = ~x, y = ~ymin, yend = ~ymax) %>%
      add_annotations("Maximum uncertainty", ax = 60)
  }) %>%
  add_fun(function(p) {
    p %>% slice(which.min(se)) %>%
      add_segments(x = ~x, xend = ~x, y = ~ymin, yend = ~ymax) %>%
      add_annotations("Minimum uncertainty")
  })

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="extending/layerdata")
chart_link
<iframe src="https://plot.ly/~RPlotBot/5191.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Reference

For more information concerning modidfying the plotly object see The Plotly Book