plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Feature: convert ggplot to plotly by adding a layer

Open atusy opened this issue 6 years ago • 2 comments

By this PR, we can convert ggplot to plotly by implementing gginteractive(). In addition, gginteractive has the interactive argument to control the conversion.

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  gginteractive()
  # gginteractive(FALSE) # can suppress conversion

This is a sort of syntax sugar for ggplotly(), which has to wrap whole ggplot:

ggplotly(
  ggplot(mtcars, aes(wt, mpg)) +
  geom_point()
)

atusy avatar Aug 15 '19 04:08 atusy

Interesting, thanks! Is there a way we could do this without introducing a new function? That is, could something like this work?

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  ggplotly()

cpsievert avatar Sep 03 '19 15:09 cpsievert

Well, I wanted to do so. I did not do it because the first argument of ggplotly is last_plot(). If it is missing or NULL, we can do what we want by

if (missing(p)) return(ggintearactive())
# or
if (is.nuill(p)) return(gginteractive())

The only way is to add a new argument like

ggplotly <- function(..., layer = FALSE) {
  if (layer) return(gginteractive())
  ...
}

atusy avatar Sep 04 '19 04:09 atusy