Display of plots under ggplot

Hi,

I am writing on some visualization functions for a large database that consists of multiple "cycles" of production described by numerous parameters. I wrote a code to plot all the parameters linked to a given cycle. I am using the package ggplot2 but it looks like instead of plotting all 34 parameters in that case, I only have 20 plots out of the needed 34 once I call the function. I don't know if the problem lies with my code or if 20 is the full capacity of windows displayed at once? I have heard of functions like dev.off() but don't know if they would help in dealing with this issue. Here is my code:

######## Function plotting all the parameters for one cycle using the indexes of the parameters and the cycle : plot_cycle_allparameters_interactive()

plot_cycle_allparameters_interactive <- function(datafile,Cycle_index){
  Cycle_name = names(datafile)[Cycle_index]
  Cycle_data = datafile[[Cycle_name]]   
  
  for(i in 2:length(Cycle_data)){
    figure_interactive <- ggplot(Cycle_data, aes(x =Time,y = Cycle_data[,i], group= 1)) +  
      geom_line(color='blue') + scale_y_continuous(expand = c(0,0))  +  labs(y = colnames(Cycle_data)[i])
    print(ggplotly(figure_interactive))
    dev.off()
  }
  
}

Any recommendation would be appreciated. Thanks!

Is length(Cycle_data)) equal to 34?

I'm not so familiar with your use of a loop to create the plot. An alternative approach could be to use facet_wrap inside the ggplot call, but perhaps you have layout constraints that make that infeasible.

library(tidyverse); library(lubridate); library(plotly)

data("storms")

example_with_32_groups <- 
  storms %>%
  mutate(date = ymd_h(paste(year, month, day, hour))) %>%
  filter(year > 2012)
  
ggplot(example_with_32_groups, aes(date, wind)) +
  geom_line(color='blue') + 
  scale_y_continuous(expand = c(0,0)) +
  facet_wrap(~name, scales = "free") +
  theme(axis.text.x = element_blank())

plotly::ggplotly(.Last.value)

2 Likes

@Zaynab96 you're right that dev.off() won't fix the issue. I would try saving each plotly graph as an HTML file on disk by changing print(ggplotly(figure_interactive)) to htmlwidgets::saveWidget(ggplotly(figure_interactive), sprintf("plot-%s.html", i))