This is a test version of Biostars. For the public version, visit https://www.biostars.org.
conditional loop does not work for plotting in R !!

Dear all,

I am trying to use function from clusterProfiler and DOSE packages and made a conditional loop for plotting. It seems it does not work properly (plot only last one), why I really dont figure it out. Please help.

ont="DO"

## this does not work ?? plot only enrichMap, why?

if (ont=="DO") {
  pdf("DO.plots1.pdf",width=11,height=8.5)
  barplot(enrich_go_do, drop=T, showCategory = 30, title = "Disease Ontology")
  # altrneative of barplot
  dotplot(enrich_go_do, x = "geneRatio", colorBy = "p.adjust", showCategory = 10, title = "Disease Ontology (Category 10)")
  enrichMap(enrich_go_do,n = 10) # topten
  dev.off()
}

## this works perfectly
pdf("DO.plots2.pdf",width=11,height=8.5)
# # Disease Ontology plots
barplot(enrich_go_do, drop=T, showCategory = 30, title = "Disease Ontology")
# altrneative of barplot
dotplot(enrich_go_do, x = "geneRatio", colorBy = "p.adjust", showCategory = 10, title = "Disease Ontology (Category 10)")
enrichMap(enrich_go_do,n = 10) # topten
dev.off()
r

What do you mean by "plot only last one"? Does it produce only one graph for the whole for-loop? Or that without if it plots?

yes its processed only one plot out of 3 plot given.

Thank you guys..problem solved.

it was needed print(barplot()). The reason was author has made barplot function using ggplot2.

I stuck on this problem for several hours. Yes. print(barplot()) and print(dotplot()) work fine.

1 answer

You open a PDF file to write to, and override it on every iteration by the looks of things. Try using a for loop and changing the filename for each iteration.

as this not gonna work.....you have seen without loop its work perfectly. However, i used PostScript function inside same loop (if), which produces separate file name for each plot (plot%03d). Therefore in this case, alos does not work...results is the same. only one plot i got at the last.

Log in to answer this question.