减少R中的绘图的pdf文件大小

时间:2022-10-22 23:15:35

i am plotting some data in R using the following commands:

我正在使用以下命令在R中绘制一些数据:

jj = ts(read.table("overlap.txt"))
pdf(file = "plot.pdf")
plot(jj, ylab="", main="")
dev.off()

The result looks like this:

结果如下:

减少R中的绘图的pdf文件大小

The problem I have is that the pdf file that I get is quite big (25Mb). Is the a way to reduce the file size? JPEG is not an option because I need a vector graphic.

我遇到的问题是我得到的pdf文件非常大(25Mb)。是一种减小文件大小的方法吗? JPEG不是一个选项,因为我需要一个矢量图形。

5 个解决方案

#1


9  

Take a look at tools::compactPDF - you need to have either qpdf or ghostscript installed, but it can make a huge difference to pdf file size.

看看tools :: compactPDF - 您需要安装qpdf或ghostscript,但它可以对pdf文件大小产生巨大影响。

If reading a PDF file from disk, there are 3 options for GostScript quality (gs_quality), as indicated in the R help file:

如果从磁盘读取PDF文件,则有3个GostScript质量选项(gs_quality),如R帮助文件中所示:

  • printer (300dpi)
  • 打印机(300dpi)
  • ebook (150dpi)
  • 电子书(150dpi)
  • screen (72dpi)
  • 屏幕(72dpi)

The default is none. For example to convert all PDFs in folder mypdfs/ to ebook quality, use the command

默认值为none。例如,要将文件夹mypdfs /中的所有PDF转换为电子书质量,请使用该命令

tools::compactPDF('mypdfs/', gs_quality='ebook')

tools :: compactPDF('mypdfs /',gs_quality ='ebook')

#2


6  

You're drawing a LOT of lines or points. Vector image formats such as pdf, ps, eps, svg, etc. maintain logical information about all of those points, lines, or other items that increase complexity, which translates to size and drawing time, as the number of points increases. Generally vector images are the best in a number of ways, most compact, scale best, and highest quality reproduction. But, if the number of graphical elements becomes very large then it's often best to go to a raster image format such as png. When you switch to raster it's best to have a good idea what size image you want, both in pixels and also in things like print measurements, in order to produce the best image.

你正在绘制大量的线条或点。诸如pdf,ps,eps,svg等的矢量图像格式保持关于所有那些增加复杂度的点,线或其他项的逻辑信息,其随着点数的增加而转换为尺寸和绘制时间。通常,矢量图像在许多方面是最好的,最紧凑,最佳缩放和最高质量的再现。但是,如果图形元素的数量变得非常大,那么通常最好采用光栅图像格式,例如png。当你切换到光栅时,最好知道你想要什么尺寸的图像,无论是像素还是印刷测量等,以产生最佳图像。

For information from the other direction, too large a raster image, see this answer.

对于来自其他方向的信息,光栅图像太大,请参阅此答案。

#3


4  

One way of reducing the file size is to reduce the number of values that you have. Assuming you have a dataframe called df:

减小文件大小的一种方法是减少您拥有的值的数量。假设您有一个名为df的数据框:

# take sample of data from dataframe
sampleNo = 10000
sampleData <- df[sample(nrow(df), sampleNo), ]

I think the only other alternative within R is to produce a non-vector. Outside of R you could use Acrobat Professional (which is not free) to optimize the pdf. This can reduce the file size enormously.

我认为R中唯一的另一种选择是产生非向量。在R之外,您可以使用Acrobat Professional(非免费)来优化pdf。这可以极大地减小文件大小。

#4


4  

Which version of R are you using? In R 2.14.0, pdf() has an argument compress to support compression. I'm not sure how much it can help you, but there are also other tools to compress PDF files such as Pdftk and qpdf. I have two wrappers for them in the animation package, but you may want to use command line directly.

您使用的是哪个版本的R?在R 2.14.0中,pdf()有一个参数compress来支持压缩。我不确定它对你有多大帮助,但还有其他工具来压缩PDF文件,如Pdftk和qpdf。我在动画包中有两个包装器,但您可能希望直接使用命令行。

#5


1  

Hard to tell without seeing what the plot looks like - post a screenshot?

很难说没看到情节的样子 - 发布截图?

I suspect its a lot of very detailed lines and most of the information probably isn't visible - lots of things overlapping or very very small detail. Try thinning your data in one dimension or another. I doubt you'll lose visible information.

我怀疑它有很多非常详细的线条,大部分信息可能都不可见 - 很多东西重叠或非常小的细节。尝试在一个维度或另一个维度中细化数据。我怀疑你会丢失可见的信息。

#1


9  

Take a look at tools::compactPDF - you need to have either qpdf or ghostscript installed, but it can make a huge difference to pdf file size.

看看tools :: compactPDF - 您需要安装qpdf或ghostscript,但它可以对pdf文件大小产生巨大影响。

If reading a PDF file from disk, there are 3 options for GostScript quality (gs_quality), as indicated in the R help file:

如果从磁盘读取PDF文件,则有3个GostScript质量选项(gs_quality),如R帮助文件中所示:

  • printer (300dpi)
  • 打印机(300dpi)
  • ebook (150dpi)
  • 电子书(150dpi)
  • screen (72dpi)
  • 屏幕(72dpi)

The default is none. For example to convert all PDFs in folder mypdfs/ to ebook quality, use the command

默认值为none。例如,要将文件夹mypdfs /中的所有PDF转换为电子书质量,请使用该命令

tools::compactPDF('mypdfs/', gs_quality='ebook')

tools :: compactPDF('mypdfs /',gs_quality ='ebook')

#2


6  

You're drawing a LOT of lines or points. Vector image formats such as pdf, ps, eps, svg, etc. maintain logical information about all of those points, lines, or other items that increase complexity, which translates to size and drawing time, as the number of points increases. Generally vector images are the best in a number of ways, most compact, scale best, and highest quality reproduction. But, if the number of graphical elements becomes very large then it's often best to go to a raster image format such as png. When you switch to raster it's best to have a good idea what size image you want, both in pixels and also in things like print measurements, in order to produce the best image.

你正在绘制大量的线条或点。诸如pdf,ps,eps,svg等的矢量图像格式保持关于所有那些增加复杂度的点,线或其他项的逻辑信息,其随着点数的增加而转换为尺寸和绘制时间。通常,矢量图像在许多方面是最好的,最紧凑,最佳缩放和最高质量的再现。但是,如果图形元素的数量变得非常大,那么通常最好采用光栅图像格式,例如png。当你切换到光栅时,最好知道你想要什么尺寸的图像,无论是像素还是印刷测量等,以产生最佳图像。

For information from the other direction, too large a raster image, see this answer.

对于来自其他方向的信息,光栅图像太大,请参阅此答案。

#3


4  

One way of reducing the file size is to reduce the number of values that you have. Assuming you have a dataframe called df:

减小文件大小的一种方法是减少您拥有的值的数量。假设您有一个名为df的数据框:

# take sample of data from dataframe
sampleNo = 10000
sampleData <- df[sample(nrow(df), sampleNo), ]

I think the only other alternative within R is to produce a non-vector. Outside of R you could use Acrobat Professional (which is not free) to optimize the pdf. This can reduce the file size enormously.

我认为R中唯一的另一种选择是产生非向量。在R之外,您可以使用Acrobat Professional(非免费)来优化pdf。这可以极大地减小文件大小。

#4


4  

Which version of R are you using? In R 2.14.0, pdf() has an argument compress to support compression. I'm not sure how much it can help you, but there are also other tools to compress PDF files such as Pdftk and qpdf. I have two wrappers for them in the animation package, but you may want to use command line directly.

您使用的是哪个版本的R?在R 2.14.0中,pdf()有一个参数compress来支持压缩。我不确定它对你有多大帮助,但还有其他工具来压缩PDF文件,如Pdftk和qpdf。我在动画包中有两个包装器,但您可能希望直接使用命令行。

#5


1  

Hard to tell without seeing what the plot looks like - post a screenshot?

很难说没看到情节的样子 - 发布截图?

I suspect its a lot of very detailed lines and most of the information probably isn't visible - lots of things overlapping or very very small detail. Try thinning your data in one dimension or another. I doubt you'll lose visible information.

我怀疑它有很多非常详细的线条,大部分信息可能都不可见 - 很多东西重叠或非常小的细节。尝试在一个维度或另一个维度中细化数据。我怀疑你会丢失可见的信息。