0

I need your help, because I don't know how to solve my problem. I have my shiny app where I have data frame (imported from file) and checkboxgroupinput where I can mark which columns are for me interesting. The problem is that number of interesting columns is not constant. I would like to have all plots on one page, so I decided to save all of them to the list. But now I have no idea how to plot them all. I found function:

  multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)
  plots <- c(list(...), plotlist)
  numPlots = length(plots)

  if (is.null(layout)) {
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols))
  }

  if (numPlots==1) {
    print(plots[[1]])
  } 
  else {
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    for (i in 1:numPlots) {
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col))
    }
  }
}

And I know that I can use it:

multiplot(plot[[1]], plot[[2]],...,plot[[n]], cols = 2)

But how can I draw it if i don't know number n?

multiplot(plot, cols = 2)

Doesn't work :/ Any tips?

tomsu
  • 371
  • 2
  • 16
  • 2
    Just use `multiplot(plotlist=plot, cols = 2)` – MrFlick Jan 02 '18 at 19:48
  • now I get error: ggplot2 doesn't know how to deal with data of class character – tomsu Jan 02 '18 at 20:22
  • 1
    Well what exactly did you store in `plot`? It would be easier to help you if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jan 02 '18 at 20:23
  • ok, a solve a problem. No I have something really wirld... I try to plots in loop and when I do: for (i in 1:ncol(df)){ plot[[i]] <- ggplot(df, aes(x = df[,i], y=(..count..)/sum(..count..))) + geom_bar(fill="#003366") + labs(title = dfcolnames[i], x = "Variable Values", y = "Frequency") } I get 3 this same plots, but when I do it without loop plot[[1]] <- ggplot(df, aes(x = df[,1], y=(..count..)/sum(..count..))) + geom_bar(fill="#003366") + labs(title = dfcolnames[1], x = "Variable Values", y = "Frequency") plot[[2]] and plot[[3]] I get correct plots. I don't understand.... – tomsu Jan 02 '18 at 20:54
  • 3
    Seems like a new question unrelated to your original post. You should ask a new question and include the sample code there where it can be properly formatted, rather than putting it in a comment. – MrFlick Jan 02 '18 at 21:03

0 Answers0