Learn R Programming

plotly (version 4.9.2.1)

ggplotly: Convert ggplot2 to plotly

Description

This function converts a ggplot2::ggplot() object to a plotly object.

Usage

ggplotly(
  p = ggplot2::last_plot(),
  width = NULL,
  height = NULL,
  tooltip = "all",
  dynamicTicks = FALSE,
  layerData = 1,
  originalData = TRUE,
  source = "A",
  ...
)

Arguments

p

a ggplot object.

width

Width of the plot in pixels (optional, defaults to automatic sizing).

height

Height of the plot in pixels (optional, defaults to automatic sizing).

tooltip

a character vector specifying which aesthetic mappings to show in the tooltip. The default, "all", means show all the aesthetic mappings (including the unofficial "text" aesthetic). The order of variables here will also control the order they appear. For example, use tooltip = c("y", "x", "colour") if you want y first, x second, and colour last.

dynamicTicks

should plotly.js dynamically generate axis tick labels? Dynamic ticks are useful for updating ticks in response to zoom/pan interactions; however, they can not always reproduce labels as they would appear in the static ggplot2 image.

layerData

data from which layer should be returned?

originalData

should the "original" or "scaled" data be returned?

source

a character string of length 1. Match the value of this string with the source argument in event_data() to retrieve the event data corresponding to a specific plot (shiny apps can have multiple plots).

...

arguments passed onto methods.

Details

Conversion of relative sizes depends on the size of the current graphics device (if no device is open, width/height of a new (off-screen) device defaults to 640/480). In other words, height and width must be specified at runtime to ensure sizing is correct. For examples on how to specify the output container's height/width in a shiny app, see plotly_example("shiny", "ggplotly_sizing").

References

https://plot.ly/ggplot2

See Also

plot_ly()

Examples

Run this code
# NOT RUN {
# simple example
ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
ggplotly(ggiris)

data(canada.cities, package = "maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
  borders(regions = "canada") +
  coord_equal() +
  geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2)
ggplotly(viz, tooltip = c("text", "size"))

# linked scatterplot brushing
d <- highlight_key(mtcars)
qplot(data = d, x = mpg, y = wt) %>%
  subplot(qplot(data = d, x = mpg, y = vs)) %>% 
  layout(title = "Click and drag to select points") %>%
  highlight("plotly_selected")


# more brushing (i.e. highlighting) examples
demo("crosstalk-highlight-ggplotly", package = "plotly")

# client-side linked brushing in a scatterplot matrix
highlight_key(iris) %>%
  GGally::ggpairs(aes(colour = Species), columns = 1:4) %>%
  ggplotly(tooltip = c("x", "y", "colour")) %>%
  highlight("plotly_selected")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab