简体   繁体   中英

Unknown graphics device error in Rstudio

I want to save 10 different ggplots to disc with different parameters, but getting the error:

Error: Unknown graphics device ''

Here is my code:

for (geneNum in 1:10) {
  geneCounts <- plotCounts(dds, gene=gene_list[geneNum], 
  intgroup=c("Groups","Mouse"), returnData=TRUE)
  ggplot(geneCounts, aes(x=Mouse, y=count, color=Groups, 
  group=Groups)) +
  scale_y_log10() + geom_point(size=3) + geom_line() + 
  ggtitle(gene_list[geneNum])
  filename <- paste0("gene", geneNum, sep="_")
  ggsave(filename,
     plot = last_plot(), # or give ggplot object name as in myPlot,
     width = 5, height = 5,
     units = "in", # other options c("in", "cm", "mm"), 
     dpi = 300)
}

Any suggestions would be greatly appreciated.

(Copied from Alistaire's comment.)

ggsave() looks for the file extension on the filename, eg .png , and uses the appropriate ( what R calls ) graphics device to save the image (really, the kind of system used to encode the image data, PNG, BMP, JPG, PDF etc.). This error is usually caused by a missing or incorrect file extension in the filename. Specifically, in your case,

change

filename <- paste0("gene", geneNum, sep="_")

to eg (for .png output):

filename <- paste0("gene", geneNum, ".png", sep="_")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM