Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown graphics device error in Rstudio

Tags:

r

ggplot2

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.

like image 304
Nikita Vlasenko Avatar asked Apr 12 '18 19:04

Nikita Vlasenko


People also ask

How do I Reset my graphics device in RStudio?

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.

Why doesn't R show the graphics device after ggsave?

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.

What is R graphics device 2 (active) RStudio?

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.

What to do if RStudio is not working?

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.


1 Answers

(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="_")
like image 100
CoderGuy123 Avatar answered Sep 16 '22 22:09

CoderGuy123