Error saying cannot open file 'file.pdf' when trying to save a plot in ggplot

0 votes

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
Nov 9, 2018 in Data Analytics by Ali
• 10,430 points
509 views

1 answer to this question.

0 votes

You could try adding the following line of code:

dir.create(file.path(dirname(figure_file)))

It will create the directory for you.

answered Nov 9, 2018 by Maverick
• 10,040 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Creating barplot with standard errors plotted in R

For df1 plot # Group data by when ...READ MORE

answered Nov 12, 2018 in Data Analytics by Maverick
• 10,040 points
88 views
0 votes
1 answer

": cannot open the connection" Error in R

There can be two reasons for this ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 42,330 points
587 views
0 votes
1 answer

“subscript out of bounds” Error in r programming

This error is likely to occur when ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 42,330 points
2,082 views
0 votes
1 answer

What does “replacement has” Error mean?

This error occurs when one tries to ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 42,330 points
48 views
0 votes
1 answer

Error saying " cannot open the connection" when trying to install a package in R

Try install.packages(“package_name”, repos="http://cran.us.r-p ...READ MORE

answered Nov 26, 2018 in Data Analytics by Maverick
• 10,040 points
399 views