简体   繁体   English

使用ggsave将生成的文件保存为PDF时出现问题

[英]issues when using ggsave to save the generated file into PDF

I use ggsave to save the figure generated using ggplot2, this is how I do 我使用ggsave来保存使用ggplot2生成的数字,这就是我的工作方式

figure1<-last_plot()
ggsave(figure1,file="/home/user1/figure1.png",width=15,height=3)

These two lines of code successfully save the figure as png file. 这两行代码成功地将数字保存为png文件。

However, when I tried to save it as pdf file, 但是,当我尝试将其保存为pdf文件时,

 ggsave(figure1,file="/home/user1/figure1.pdf",width=15,height=36)

The saved pdf file is just a blank page. 保存的pdf文件只是一个空白页面。 What's the problem? 有什么问题? Thanks a lot. 非常感谢。

library(ggplot2)

ggplot(data=mtcars, aes(x=cyl, y=hp))+
  geom_point()

figure1<-last_plot()
ggsave(figure1,file="figure1.png",width=15,height=3)
ggsave(figure1,file="figure1.pdf",width=15,height=3)

# works with R 2.15

You can try to save it using pdf() command: 您可以尝试使用pdf()命令保存它:

library(ggplot2) 
pdf(file="figure.pdf",width=15,height=3)
figure <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
print(figure)
dev.off()

I supose that it also doesn't work. 我觉得它也不起作用。

Solution (workaround) that should work is: 应该起作用的解决方案(解决方法)是:

1) save it to svg file 1)将其保存到svg文件

ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
ggsave("figure.svg")

2) then convert it to pdf. 2)然后将其转换为pdf。 Eg in Linux 例如在Linux中

rsvg-convert -f pdf -o figure.pdf figure.svg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM