|
1 | | -#' Turn a ggplot into a plotly object |
| 1 | +#' Convert ggplot2 to notly (plotly with embedded ggplot) |
2 | 2 | #' |
3 | | -#' This function takes a ggplot object and turns it into a plotly object. |
4 | | -#' The original ggplot object is retained inside of the new plotly object. |
| 3 | +#' This function converts a [ggplot2::ggplot()] object to a |
| 4 | +#' notly object. The original ggplot object is retained inside of the new plotly object. |
5 | 5 | #' This makes this conversion reversible, allowing you to freely go back and forth betweeen ggplot and plotly. |
6 | 6 | #' |
7 | | -#' @param p ggplot object |
| 7 | +#' @details Conversion of relative sizes depends on the size of the current |
| 8 | +#' graphics device (if no device is open, width/height of a new (off-screen) |
| 9 | +#' device defaults to 640/480). In other words, `height` and |
| 10 | +#' `width` must be specified at runtime to ensure sizing is correct. |
| 11 | +#' For examples on how to specify the output container's `height`/`width` in a |
| 12 | +#' shiny app, see `plotly_example("shiny", "ggplotly_sizing")`. |
| 13 | +#' |
| 14 | +#' @param p a ggplot object. |
| 15 | +#' @param width Width of the plot in pixels (optional, defaults to automatic sizing). |
| 16 | +#' @param height Height of the plot in pixels (optional, defaults to automatic sizing). |
| 17 | +#' @param tooltip a character vector specifying which aesthetic mappings to show |
| 18 | +#' in the tooltip. The default, "all", means show all the aesthetic mappings |
| 19 | +#' (including the unofficial "text" aesthetic). The order of variables here will |
| 20 | +#' also control the order they appear. For example, use |
| 21 | +#' `tooltip = c("y", "x", "colour")` if you want y first, x second, and |
| 22 | +#' colour last. |
| 23 | +#' @param dynamicTicks should plotly.js dynamically generate axis tick labels? |
| 24 | +#' Dynamic ticks are useful for updating ticks in response to zoom/pan |
| 25 | +#' interactions; however, they can not always reproduce labels as they |
| 26 | +#' would appear in the static ggplot2 image. |
| 27 | +#' @param layerData data from which layer should be returned? |
| 28 | +#' @param originalData should the "original" or "scaled" data be returned? |
| 29 | +#' @param source a character string of length 1. Match the value of this string |
| 30 | +#' with the source argument in [event_data()] to retrieve the |
| 31 | +#' event data corresponding to a specific plot (shiny apps can have multiple plots). |
| 32 | +#' @param ... arguments passed onto methods. |
8 | 33 | #' @return Plotly object with a ggplot nested inside |
9 | 34 | #' @export |
10 | | - |
| 35 | +#' @author Carson Sievert |
| 36 | +#' @references \url{https://plotly.com/ggplot2/} |
| 37 | +#' @seealso [plot_ly()] |
| 38 | +#' @examples \dontrun{ |
| 39 | +#' # simple example |
| 40 | +#' ggpenguins <- qplot(bill_length_mm , body_mass_g, |
| 41 | +#' data = palmerpenguins::penguins, color = species) |
| 42 | +#' ggplotly(ggpenguins) |
| 43 | +#' |
| 44 | +#' data(canada.cities, package = "maps") |
| 45 | +#' viz <- ggplot(canada.cities, aes(long, lat)) + |
| 46 | +#' borders(regions = "canada") + |
| 47 | +#' coord_equal() + |
| 48 | +#' geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2) |
| 49 | +#' ggplotly(viz, tooltip = c("text", "size")) |
| 50 | +#' |
| 51 | +#' # linked scatterplot brushing |
| 52 | +#' d <- highlight_key(mtcars) |
| 53 | +#' qplot(data = d, x = mpg, y = wt) %>% |
| 54 | +#' subplot(qplot(data = d, x = mpg, y = vs)) %>% |
| 55 | +#' layout(title = "Click and drag to select points") %>% |
| 56 | +#' highlight("plotly_selected") |
| 57 | +#' |
| 58 | +#' |
| 59 | +#' # more brushing (i.e. highlighting) examples |
| 60 | +#' demo("crosstalk-highlight-ggplotly", package = "plotly") |
| 61 | +#' |
| 62 | +#' # client-side linked brushing in a scatterplot matrix |
| 63 | +#' highlight_key(palmerpenguins::penguins) %>% |
| 64 | +#' GGally::ggpairs(aes(colour = Species), columns = 1:4) %>% |
| 65 | +#' ggplotly(tooltip = c("x", "y", "colour")) %>% |
| 66 | +#' highlight("plotly_selected") |
| 67 | +#' } |
| 68 | +#' |
11 | 69 | ggplotly <- function( |
12 | 70 | p = ggplot2::last_plot(), |
13 | 71 | width = NULL, |
|
0 commit comments