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.
To reset your graphics device, call the following code from the console: This will delete your current plots in the RStudio Plots Pane. If you have multiple graphics devices open, repeat this command until the output displays null device. If the above approaches do not solve your problem, try reproducing outside of RStudio.
ADDITION: further testing reveals that this may in fact be a bug with RStudio and how it is displaying the graphics device after ggsave, rather than R itself. Running the above script in RGui does not reproduce the problem, only in RStudio V0.97.336 + V0.97.449.
R Graphics: Device 2 (ACTIVE) RStudio is outputting graphics to a pop-up window entitled "R Graphics: Device 2 (Active)" and not placing anything in the RStudio plots pane.
If the above approaches do not solve your problem, try reproducing outside of RStudio. Use the default interface installed with R such as RGui, R.app, or terminal R. If the same problem occurs without using RStudio, the error is related to R or the R code.
(Copied from Alistaire's comment.)
ggsave()
looks for the file extension on the filename, e.g. .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 e.g. (for .png
output):
filename <- paste0("gene", geneNum, ".png", sep="_")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With