Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to convert plotly object to ggplot2?

Tags:

ggplot2

plotly

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?

like image 873
Kyle Weise Avatar asked Nov 23 '25 14:11

Kyle Weise


1 Answers

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
like image 172
Gordon McDonald Avatar answered Nov 26 '25 16:11

Gordon McDonald