ggsave png error with larger size
NOTE : Using R 2.12.1 on Windows 7 64bit, this problem has vanished. If you run into this problem, first try updating your R version.
After the problem came up again in another question, I reran my test code on my new system to see if the bug was gone, and it is.
EDIT: The trick why underlying code could work is the fact that it uses a resolution of only 72 dpi and not 300dpi as is the standard in ggsave()
I believe.
so ggsave("tst.png",height=9,width=12,dpi=72)
could do the trick.
But you really must have a crazy plot if it can't take it. As far as I can guess, the problem is related to the graphics card (as derived from this message from prof. Ripley ).
If resolution is a problem, you could better go to vectorized formats like eps or pdf.
EDIT 2 :
Apparently, there is a bug somewhere involving some kind of memory leak maybe? Give following code:
library(car)
library(ggplot2)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)
setwd("G:/Temp")
i<-1
while(1){
tryCatch(ggsave("tst.png",height=9+i,width=12+i),error=function(e) {print(i);stop(e);})
i <- i+1
}
This runs fine for me until i reaches about 9, then I get the error you get. Every next attempt at running the code, starting again with i=1
, gives the same error. Trying with png()
and dev.off()
gives again the same error. Seems like there is some part of a memory filling up and not being emptied, effectively preventing to get another png file saved. also for me gc()
didn't do a thing. Even closing R and reopening again didn't work.
It is "solved" using ggsave("tst.pdf")
, but the bug remains. I'd report to the R team.
ggsave() fails with Error in png_dev(..., res = dpi, units = in) : unable to start png() device
If the target directory does not already exist, ggsave()
will not create it as part of saving. It will instead throw this error.
First create the directory, either in your operating system outside of R or with dir.create
like in this answer: https://stackoverflow.com/a/29784923.
Then save with ggsave()
.
Set the size of ggsave exactly
To understand why the DPI is important, consider these two plots:
ggsave(filename = "foo300.png", ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) + theme_bw(base_size = 10),
width = 5, height = 4, dpi = 300, units = "in", device='png')
ggsave(filename = "foo150.png", ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) + theme_bw(base_size = 10),
width = 10, height = 8, dpi = 150, units = "in", device='png')
The resulting files have the same pixel dimensions, but the font size in each is different. If you place them on a page with the same physical size as their ggsave()
calls, the font size will be correct (i.e. 10 as in the ggsave()
call). But if you put them on a page at the wrong physical size, the font size won't be 10. To maintain the same physical size and font size while increasing DPI, you have to increase the number of pixels in the image.
Related Topics
Error: X Must Be Atomic for 'Sort.List'
Nls Troubles: Missing Value or an Infinity Produced When Evaluating the Model
Error When Exporting Dataframe to Text File in R
Ggplot2: Group X Axis Discrete Values into Subgroups
How to Extract Unique Elements from a Data.Frame in R
Remove Text Inside Brackets, Parens, And/Or Braces
Specify Function Parameters in Do.Call
Adjusting the Node Size in Igraph Using a Matrix
Extracting Output from Principal Function in Psych Package as a Data Frame
Error in Bind_Rows_(X, .Id):Column Can't Be Converted from Factor to Numeric
Find and Replace Missing Values with Row Mean
Creating a More Continuous Color Palette in R, Ggplot2, Lattice, or Latticeextra
How to Simultaneously Apply Color/Shape/Size in a Scatter Plot Using Plotly
How to Run a Function Every Second
How to Make Shinyapp to Use Environmental Variables When Deployed on the Web