how to return multiple plots once in R ?

i wrote a function to return a multiple plots in R. But only one plot is returning, but i have to return multiple output plots how can I return the all the plots ?

cgr_res <- function(fasta_file){
library(kaos)
library(seqinr)
  y=read.fasta(file=fasta_file)
  A=function(y){
sequence <-unlist(strsplit(y,""))

sequence.cgr = cgr(sequence,res = 100)

rplot_cgr <- cgr.plot(sequence.cgr, mode = "points")
dev.copy(png,'rplot_cgr.png')
dev.off()

cgrvector=vectorize(sequence.cgr)
result=list(sequencecgr=sequence.cgr,allplots=rplot_cgr, output_of_cgr_vector=cgrvector)
dev.copy(png,'rplot_cgr.png')
dev.off()
return(result)

  }
  B=lapply(y,A)
  return(B)
}


#example run
#cgr_res("kaos.fasta")

sir u can run it just 2 small packages needs to install.. I tried ur suggestion I am getting same results sit

Not sure if this is what you are looking for. But if I have one plot p1, and a second plot p2, then with libraries ggplot2 and gridExtra loaded, try

grid.arrange(p1, p2, ncol=2)

This worked for me:

library(ggplot2)
library(gridExtra)
df <- data.frame(x=c(1,2,3,4,5), y=c(5,1, 2, 4,3), z=c(1,4,2,5,3))
p1 <- ggplot(df, aes(x=x, y=y)) + geom_line()
p2 <- ggplot(df, aes(x=x, y=z)) + geom_line()
grid.arrange(p1, p2, ncol=2)

1 Like

https://drive.google.com/file/d/13JOrevqOWtmfc6UiXyR_klvTcLeogbgJ/view?usp=sharing

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

Since it is impossible to actually run your code I can only give you general advice. In R (as in most other languages, actually), once you get to return, everything else won't be executed. So, if you want to return multiple things, you can always put results into a list and return that instead. So you'll have something like return(list(plotA = plotA, plotB = plotB)) or similar. You can then use output from this list as you would any other output.

2 Likes

What did you try? Can you post your revised code?

As is, your problem is not about multiple plots, it's more about how to return multiple things from the same function and I've shown how to do it - use list.

sir the number of plots depend on the number of different sequences present in the input file. in my research work I have one file that contain many sequences in it

Blockquote[quote="mahii, post:4, topic:73155, full:true"]
sir u can run it just 2 small packages needs to install.. I tried ur suggestion I am getting same results sit
[/quote]

with what data should we run it ?

Error: object 'kaos.fasta' not found
1 Like