R error saving ggplot pdf
Solution 1
I think you're trying to write to a non-existent folder, and as far as I know grDevices won't allow this. Someone else seems to have a similar issue.
I tried your code both in R 3.1.1 and 3.2.1—they both give the same error (unless the directory is created on beforehand.
You could try adding the following line of code:
dir.create(file.path(dirname(figure_file)))
It will create the directory for you.
Solution 2
I had the same, turned out I had already an object with the same name saved in that directory that apparently does not get overwritten.
Solution 3
I got the same error when my file name was too long - and therefore the path name in windows exceeded the allowed letters limit. Shortening the file name solved this issue.
Kaston
Updated on June 08, 2022Comments
-
Kaston almost 2 years
When I try to save a plot made using ggplot as a pdf using this code:
library(ggplot2) file = "/data/mda/20150630-1Mb-full_comparison-low_depth_hTERT/result/comparison_figure/SD_rank_custom.csv" figure_file = "/data/mda/20150604-1Mb-full_comparison-low_depth_hTERT/result/comparison_figure/SD_rank_custom.pdf" sd_data <- as.data.frame(read.csv(file, header=TRUE)) # generate box plot ggplot( data=sd_data, aes( x=Experiment, y=SD ) )+ theme_bw() + #use bw theme geom_boxplot(outlier.shape = NA) + #hide outlier points geom_jitter() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) ggsave( filename=figure_file, width=10, height=10 )
I get the following error:
Error in grDevices::pdf(..., version = version) :
cannot open file 'file.pdf'
Calls: ggsave -> device ->
Execution halted
I think my version of R was recently updated to 3.2.0, and I've confirmed that it works fine in v3.1.1 so I'm assuming this is version related. I also confirmed I can write a csv file to the directory.
Any ideas how to fix this?