0

I'm trying to print multiple scatter plots each in one page, all in one file. So I print each scatter plot to a page in a pdf file. The problem is that because the scatter plot has too many data points, the pdf file is too large. I understand png will reduce the size of the file significantly, but it wouldn't be possible to put multiple png s in multiple pages of a single file, right? I thought the only way is to do it with pdf, now the pdf file is too large. Any idea how to deal with this? Thanks. Here is my code:

library(ggplot2)
df <- data.frame(x = rnorm(50000),y=rnorm(50000))
pdf("myPlots.pdf",onefile=TRUE)
for (i in 1:10)
   print(ggplot(df,aes(x=x,y=y)) + geom_point())
dev.off()
Mohammad
  • 1,078
  • 2
  • 18
  • 39
  • 1
    Related 1. [ggplot2/R output pdf too large](https://stackoverflow.com/questions/9572518/ggplot2-r-output-pdf-too-large); 2. [geom_point with many data points, the file size of the plot is too big](https://stackoverflow.com/questions/53861642/geom-point-with-many-data-points-the-file-size-of-the-plot-is-too-big) – pogibas Dec 21 '18 at 22:17
  • 2
    I have no idea about architecture of PDF format, but just thinking about if each point needs 32 bytes to store its properties you already end up with a 16 MB pdf here. However with so many points you can't see individual points anyways, you could maybe consider `geom_hex` or `geom_density_2d` instead. – deeenes Dec 21 '18 at 23:21
  • 1
    Other solution is to go for PNG instead of PDF. Rendering so many points in a viewer is heavy job for your computer and does not really make sense. – deeenes Dec 21 '18 at 23:25
  • 1
    Yeah, PDF also has internal compression, would be nice if ggplot (cairo) could use these. Btw, do you use cairo to generate the pdf? See compression options for ready PDFs here: https://stackoverflow.com/a/5857396/854988 – deeenes Dec 21 '18 at 23:29

0 Answers0