Thanks! The trick of printing the plot does work. However, I also figured out that I can just use the ggsave function from ggplot instead, that way I do not have to do dev.off() everytime. I was under the impression that ggsave only works for plots made with ggplot but apparently that does not seem to be the case. For anyone interested, in addition to printing the plot, this is also one other way of doing it:
#making enrichment plot
barplot = barplot(x)
#saving with ggsave
ggsave(filename = paste(backup_file, "barplot.jpg", sep = ""), plot = barplot,
dpi = 300, height = 10, width = 12, device = "jpeg")
Does it work if you add a
dev.off()before callingjpeg?I tried doing that but it gives this error now -
Error in dev.off() : cannot shut down device 1 (the null device). Probably understandable because there is no open connection?