Problems using ggsave to save generated file to PDF

I use ggsave to save the shape generated with ggplot2, here is how I do it

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

These two lines of code successfully save the shape as a png file.

However, when I tried to save it as a pdf file,

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

A saved pdf file is just a blank page. What is the problem? Thank you very much.

+4
source share
2 answers
 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 
+1
source

You can try to save it using the pdf () command:

 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 believe that it also does not work.

Solution (workaround) that should work:

1) save it in svg file

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

2), then convert it to pdf. For instance. on Linux

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

Source: https://habr.com/ru/post/1385233/


All Articles