1

Here I'm plotting two different graphs around 300 times with the function AD.graph. Normally I would just add a ggsave in the function and it would save all those 600 graphs, 2 for each Material (Plot_AD and Plot_QTY). But I´d like to save those graphs per material in one pdf. So 300 pdf`s with the name of the variable each with two plots. And I worked out this code but nothing happened. So I hope you guys could help.

AD.graph <- function(df, na.rm = T, ...) {
  
  variable <- unique(T_AD$Material)

  for (i in seq_along(variable)) {
    
    
    plot_AD <- ggplot(subset(T_AD, T_AD$Material == variable[i]),
                      aes(
                        x = "Name Sales Representative", 
                        y = n, fill = Timeline)) +
      geom_bar(stat = "identity", position = "stack") +
      scale_fill_manual(brewer.pal(3, "BuGn"))
    ggtitle("variable[i]") +
      coord_flip()
    
    
    plot_QTY <- ggplot(subset(T_QTY, T_QTY$Material == variable[i]), 
                       aes(Month, QTY, group=Year, colour=Year)) +
      geom_point(size = 2) +
      geom_line(size = 1) +
      expand_limits(y = 0) +
      ggtitle(variable[i])
    
    pdf("variable[i].pdf")
    print(plot_QTY)
    print(plot_AD)
    dev.off()
  }
  
}
NeweR
  • 13
  • 3

0 Answers0