Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: convert ggplot to plotly by adding a layer #1599

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,42 @@ LazyData: true
RoxygenNote: 6.1.1
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Collate:
'add.R'
'animate.R'
'api.R'
'api_exports.R'
'data.R'
'deprecated.R'
'dev.R'
'embed.R'
'export.R'
'ggplotly.R'
'gginteractive.R'
'group2NA.R'
'helpers.R'
'highlight.R'
'imports.R'
'last_plot.R'
'layers2layout.R'
'layers2traces.R'
'layout.R'
'mathjax.R'
'orca.R'
'partial_bundles.R'
'pipe.R'
'plotly.R'
'plotly_IMAGE.R'
'plotly_build.R'
'plotly_data.R'
'plotly_example.R'
'print.R'
'process.R'
'proxy.R'
'sf.R'
'shiny.R'
'signup.R'
'style.R'
'subplots.R'
'toRGB.R'
'utils.R'
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ S3method(geom2trace,GeomText)
S3method(geom2trace,GeomTile)
S3method(geom2trace,default)
S3method(ggplot,plotly)
S3method(ggplot_add,gginteractive)
S3method(ggplotly,"NULL")
S3method(ggplotly,ggmatrix)
S3method(ggplotly,ggplot)
Expand Down Expand Up @@ -136,6 +137,7 @@ export(filter_)
export(geom2trace)
export(get_figure)
export(gg2list)
export(gginteractive)
export(ggplotly)
export(group2NA)
export(group_by)
Expand Down Expand Up @@ -222,6 +224,7 @@ importFrom(dplyr,summarise_)
importFrom(dplyr,transmute)
importFrom(dplyr,transmute_)
importFrom(dplyr,ungroup)
importFrom(ggplot2,ggplot_add)
importFrom(grDevices,as.raster)
importFrom(grDevices,col2rgb)
importFrom(grDevices,dev.list)
Expand Down Expand Up @@ -257,6 +260,7 @@ importFrom(lazyeval,f_new)
importFrom(lazyeval,is_formula)
importFrom(lazyeval,is_lang)
importFrom(magrittr,"%>%")
importFrom(purrr,partial)
importFrom(purrr,transpose)
importFrom(rlang,eval_tidy)
importFrom(stats,complete.cases)
Expand Down
30 changes: 30 additions & 0 deletions R/gginteractive.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' @include ggplotly.R
#' @rdname ggplotly
#' @importFrom purrr partial
#'
#' @param interactive
#' If `TRUE` (default), then ggplot object is converted to plotly object.
#'
#' @export
gginteractive <- function(
interactive = TRUE, width = NULL, height = NULL, tooltip = "all",
dynamicTicks = FALSE, layerData = 1, originalData = TRUE, source = "A", ...
) {
structure(
if (interactive) {
partial(
ggplotly,
width = width, height = height, tooltip = tooltip,
dynamicTicks = dynamicTicks, layerData = layerData,
originalData = originalData, source = source, ...
)
} else {
identity
},
class = c("gginteractive", "function")
)
}

#' @importFrom ggplot2 ggplot_add
#' @export
ggplot_add.gginteractive <- function(object, plot, object_name) object(plot)
4 changes: 2 additions & 2 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Convert ggplot2 to plotly
#'
#' This function converts a [ggplot2::ggplot()] object to a
#' plotly object.
#' These functions convert a [ggplot2::ggplot()] object to a plotly object.
#'
#' @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)
Expand Down Expand Up @@ -38,6 +37,7 @@
#' # simple example
#' ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
#' ggplotly(ggiris)
#' ggiris + gginteractive()
#'
#' data(canada.cities, package = "maps")
#' viz <- ggplot(canada.cities, aes(long, lat)) +
Expand Down
13 changes: 10 additions & 3 deletions man/ggplotly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-ggplot-ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,53 @@ context("ggplotly+plotly")
p <- ggplot(txhousing, aes(x = date, y = median, group = city)) +
geom_line(alpha = 0.3)

expect_identical_plotly <- function(object, expected, ...) {
expect_identical(names(object$x), names(expected$x))

exceptions <- c("attrs", "cur_data", "visdat", "layoutAttrs")
object$x <- object$x[setdiff(names(object$x), exceptions)]
expected$x <- expected$x[setdiff(names(expected$x), exceptions)]
expect_identical(object, expected, ...)
}

test_that("ggplotly returns original data with special attributes", {
dat <- ggplotly(p) %>% plotly_data()
expect_equivalent(dat, p$data)
expect_equivalent(as.character(dplyr::groups(dat)), "city")
expect_equivalent(dat, plotly_data(p + gginteractive()))
})

test_that("can filter data returned by ggplotly", {
dat <- ggplotly(p) %>% filter(city == "Houston") %>% plotly_data()
expect_equivalent(dat, subset(p$data, city == "Houston"))
expect_equivalent(as.character(dplyr::groups(dat)), "city")
expect_equivalent(
dat, (p + gginteractive()) %>% filter(city == "Houston") %>% plotly_data()
)
})

test_that("can add traces with original _and_ scaled data", {
l1 <- ggplotly(p) %>% add_lines() %>% plotly_build()
expect_equivalent(length(l1$x$data), 2)
expect_identical_plotly(
l1, (p + gginteractive()) %>% add_lines() %>% plotly_build()
)
l2 <- ggplotly(p, originalData = FALSE) %>%
add_lines() %>% plotly_build()
# ideally we'd test that the two plots have the same data, but
# for some reason R CMD check throws an error which I can't replicate :(
expect_equivalent(length(l2$x$data), 2)
expect_identical_plotly(
l2, (p + gginteractive(originalData = FALSE)) %>% add_lines() %>% plotly_build()
)
})

test_that("can access ggplot data in layout()", {
l <- ggplotly(p) %>% layout(title = ~range(date))
expect_equivalent(plotly_build(l)$x$layout$title, range(txhousing$date))
expect_identical_plotly(l, (p + gginteractive()) %>% layout(title = ~range(date)))
})

test_that("can suppress conversion of ggplot to plotly", {
expect_s3_class(p + gginteractive(interactive = FALSE), class(p))
})