R错误保存ggplot pdf

Kas*_*ton 5 pdf r ggplot2

当我尝试使用此代码保存使用ggplot作为pdf制作的绘图时:

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
)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

grDevices :: pdf(...,version = version)中的错误:

无法打开文件'file.pdf'

调用:ggsave - > device - >

执行停止

我认为我的R版本最近更新为3.2.0,我已经确认它在v3.1.1中工作正常,所以我假设这与版本有关.我还确认我可以将csv文件写入目录.

任何想法如何解决这一问题?

Joh*_*son 3

我认为您正在尝试写入一个不存在的文件夹,据我所知 grDevices 不允许这样做。其他人似乎也有类似的问题。

\n\n

我在 R 3.1.1 和 3.2.1\xe2\x80\x94 中尝试了你的代码,它们都给出了相同的错误(除非事先创建了目录。

\n\n

您可以尝试添加以下代码行:

\n\n
dir.create(file.path(dirname(figure_file)))\n
Run Code Online (Sandbox Code Playgroud)\n\n

它将为您创建目录。

\n