x-axis and y-axis labels of scatterplots dissaper when merge them by "subplot" function in R

I have a couple of scatterplots in R. all of them have x-axis and y-axis labels and titles as well. But when I merge them by subplot function their labels and titles disappear. How can I resolve it?

 for (i in 1: length(s)) {
          mat = matrix(ncol = 0, nrow = 0)
          E = data.frame(mat)
          E2 = P2[[i]]
          E2 = as.data.frame(E2)
          E1 = P1[[i]]
          E1 = as.data.frame(E1)
          E = cbind(E1, E2)
          colnames(E) = c("group1", "group2")
          w1 = group1_vectors[i]
          w2 = group2_vectors[i]
          w3 = gene_vectors[i]
          plot1 = ggplot(E, aes(x = group1 , y = group2)) +
            labs(title= w3,
                 x= w1, y = w2)+
            geom_point()
          plot_list_final[[i]] = plot1
        }


      plot_list_final =  do.call(tagList, plot_list_final)
      f = length(s)
      p_last <- subplot(plot_list_final[1:f],  nrows = f, margin = 0.045) %>%
       layout(title = "Plots of selected rows (Genes)")
      dev.off()
      return(p_last) 

subplot isn't a function in base R graphics, not tidyvery/ggplot2. I would guess its a plotly functionality...
If the question is how to stack/arrange multiple ggplot2 charts, I would recommend libraries patchwork or cowplot.

I tried plot_grid but I got this error:
Warning in as_grob.default(plot) :
Cannot convert object of class list into a grob.
Warning: Error in dev.off: cannot shut down device 1 (the null device)

because I have a list of plots.
Do you have any suggestion?

Demo, how a list of plots can be used:

library(tidyverse)
library(cowplot)

p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()

plot_grid(plotlist = list(p1,p1))

Thank you, but new error:
Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
geom_GeomDrawGrob() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
Issues · plotly/plotly.R · GitHub

A new error where ? not in the code I shared with you.
If you want me to comment on your code, you should share, your code.
Also, it seems you are mixing plotly into it all again... My suggestions to use cowplot was to avoid that, but if you absolutely want to be using plotly then perhaps cowplot wont be the right tool for you...
You can use plotly and subplot, but that system has its limitations that you will have to accept, or work around.
For example in plotl/y subplot, 'title' is at the canvas level, so there would only be one, if you want individuated text above each subplot (i.e. 'titles') then you would have to add them as annotations, see here for a discussion of this issue.
plotly - How to give subtitles for subplot in plot_ly using R - Stack Overflow

thank you for your help, the last link worked but it shows the last title for all plots.
because I have some subplots and their number is unknown.
please see my code:

output$fancyPlot <- renderPlotly({
 s = input$fancyTable_rows_selected
for (i in 1: length(s)) {
  mat = matrix(ncol = 0, nrow = 0)
  E = data.frame(mat)
  E2 = P2[[i]]
  E2 = as.data.frame(E2)
  E1 = P1[[i]]
  E1 = as.data.frame(E1)
  E = cbind(E1, E2)
  colnames(E) = c("group1", "group2")
  w1 = group1_vectors[i]
  w2 = group2_vectors[i]
  w3 = gene_vectors[i]
 
  plot1 = ggplot(E, aes(x = group1 , y = group2)) +
    geom_point(color='tomato')

  plot_list_final[[i]] = plot1
}


plot_list_final =  do.call(tagList, plot_list_final)
f = length(s)
p_last <- subplot(plot_list_final[1:f], shareX = TRUE, shareY = TRUE) 
 p_last %>% layout(annotations = list(text = w3[1:f] ,showarrow = F, xref='paper', yref='paper')) #based on your link
dev.off()
return(p_last) 

})

Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.