How to improve a function to convert multiple ggplot plots that are made from Patchwork syntax to a plotly plot easily

20 Views Asked by At

Hi everyone I have been doing some EDA can using Plotly helps a lot with this. Most often I use ggplot for the main plotting and then convert via the plotly::ggplotly function. This works for most of the cases except when different plots are grid arranged. For this, I used the patchwork package for testing the grid-arranged ggplot(example - barplot + scatterplot + boxplot) plots and then subsetted the individual plots to make a similar plotly::subplot plot. This is my designed function

library(patchwork)

my_plotly_Function <- function(patchwork_ggplot_obj) {
    plotly_list <- as.vector(length(ggplot_obj),'list')
    for (i in 1:length(ggplot_obj)) {
        plotly_list[[i]] <- plotly::ggplotly(ggplot_obj[[i]])
    }
    plotly::subplot(plotly_list)   
}

This, however, is not able to use the patchwork packages' special nesting and other capabilities. Also, this becomes very slow for a large number of plots. How can improve this function by adding those abilities while making it run more efficiently?

0

There are 0 best solutions below