Kyle Weise
Kyle Weise

Reputation: 879

Function to convert plotly object to ggplot2?

I know the function ggplotly() of the plotly package converts a ggplot2 object into a plotly one, but is there a function that does the reverse? Converts a plotly object to a ggplot2 one?

Upvotes: 21

Views: 4740

Answers (1)

Gordon McDonald
Gordon McDonald

Reputation: 281

Boy do I have the solution for you!

We wrote a package to do this. It's called notly.

Notly R package

Installation:

# install.packages("devtools")
devtools::install_github("gdmcdonald/notly")

Example usage:

library(ggplot2)
library(plotly)
library(notly)

data(iris)

# Create a ggplot
ggplot_object <-
  iris %>%
  ggplot(aes(x = Sepal.Length,
                y = Sepal.Width,
                color = Species))+
      geom_point()

# Create a plotly object - but with the ggplot hiding inside of it as well
notly_obj <-
ggplot_object %>%
  ggplotly

notly_obj

# Extract the ggplot again
ggplot_obj_again <-
  notly_obj %>%
  notly
  
ggplot_obj_again

Upvotes: 2

Related Questions