减小 EPS 格式的绘图大小
我有一个包含数百个项目的直方图,我为其绘制了 QQ 图。 这导致 EPS 大小为 2.5 兆字节。 对于仅包含在出版物中并且不会以 100 倍放大倍率查看的图形来说,这太大了。
R 中是否有任何选项可以以某种方式输出较小的 EPS? 我搜索过文档没有结果。 或者是我最好的选择,比如说,之后以 300 dpi 对其进行光栅化? 如果是这样的话,对于这项工作的工具有什么建议吗?
该图的 R 代码没什么花哨的:
postscript(filename)
qqnorm(n, main=title))
qqline(n)
dev.off()
编辑: 哦! 我的问题提到输出 EPS,然后将其转换为某种光栅格式。 当然,我首先可以从 R 生成 PNG。
I have a histogram with several hundred items, for which I do a Q-Q plot. This results in EPS that is 2.5 megabytes large. This is too much for a figure that is only going to be included in a publication and is not going to be viewed at 100x magnification.
Is there any option in R to somehow output smaller EPS? I have searched docs to no avail. Or is my best option to, say, rasterize it afterwards at 300 dpi? If that's the case, any recommendations for the tool for this job?
The R code for the plot is nothing fancy:
postscript(filename)
qqnorm(n, main=title))
qqline(n)
dev.off()
EDIT: Doh! My question mentioned outputting EPS, and then converting it to some raster format. When of course I could just generate PNG in the first place from R.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我刚刚尝试了一些不起作用的方法 - 我将它们放在这里是为了避免其他人浪费时间。 作为参考,我在上面的代码中设置了
n <- rnorm(1e5)
。不起作用的事情:
设置
colormodel <-“gray”
。使用不同的 pch 值。 (一些其他值增加文件大小,但我发现没有一个可以减少文件大小。)
设置
useKerning = FALSE
。更改宽度和高度设置。
使用 pdf 而不是 postscript。
使用 Cairo 软件包中的 CarioPS。
有鉴于此,我认为您不太可能使用矢量格式来减小文件大小。 这意味着您必须使用光栅格式(最有可能的是 PNG)。
I've just tried several things that didn't work - I'm including them here to save others wasting their time. For reference, I set
n <- rnorm(1e5)
in your code above.Things that don't work:
Setting
colormodel <- "gray"
.Using a different value of pch. (Some other values increase the file size, but I found none that decrease it.)
Setting
useKerning = FALSE
.Changing the width and height settings.
Using pdf instead of postscript.
Using CarioPS from the Cairo package.
In the light of this, I think that you are unlikely to be able to decrease the file size using a vector format. This means that you will have to use a raster format (most likely PNG).
你有三个选择。
在数据的随机样本上创建 QQplot。 几百个点的随机样本应该会给出类似的 QQplot。
后记(文件名)
样本 <- 样本(n, 大小 = 200)
qqnorm(Samp, main=标题))
QQline(桑普)
dev.off()
You have three options.
Create the QQplot on a random sample of the data. A random sample of a few hundred points should give a similar QQplot.
postscript(filename)
Samp <- sample(n, size = 200)
qqnorm(Samp, main=title))
qqline(Samp)
dev.off()
在 R-list 链接文本的讨论中,我了解了 pdftk。 使用
n= 1e5
将 pdf 大小从 6mb 减小到 600k。 很简约!In this discussion at the R-list link text I learned about pdftk. With
n= 1e5
reduced the pdf size from 6mb to 600k. Pretty neat!在GS视图中,将我们的文件转换为pdf,然后再次转换为PS或EPS,最终文件的大小减小了5-7倍。
in GS view, convert our files to pdf, then convert to PS or EPS again, final file's size is reduced 5-7 times.
嗯,EPS 仅包含绘制绘图的指令,因此其大小很大程度上取决于您拥有的数据点数量。 在使用压缩的 PDF 中,它可能会更小,但最好的选择可能是使用光栅格式,它可能会比这更小。
我怀疑 EPS R 生成的数据已经尽可能小了(我确信它们在 Postscript 中有一个自己的函数来处理使用单字符名称绘制数据等,因为这是相当常见的做法)。 我怀疑有很多方法可以优化它。 不过,我可能是错的,但 R 很可能是唯一具有足够高级信息来合理压缩输出的程序。
Well, EPS just contains instructions to draw the plot, so its size would greatly depend on how many data points you have. It's likely smaller in a PDF where compression is used but your best bet might probably be to use a raster format, which can get smaller than that.
I would suspect the EPS R generates is already as small as it can get (I'm sure they have an own function in Postscript to handle plotting the data with a one-char name, etc. as that is fairly common practice). I doubt there are many ways in which to optimize that. I might be mistaken, though, but chances are that R is the only program which has enough high-level information to reasonably compress the output.
OP通过直接生成PNG文件解决了这个问题。 我必须使用 EPS,因为 PNG 和其他格式给图像起别名。 无论如何,我必须转换为 EPS 才能包含到 LaTeX 文件中。
我使用 GIMP 导入由 R 图像函数生成的 10 MB eps 文件。 然后旋转、展平并保存为 300KB 的 eps 文件。 拼合将所有图层合并为一个图层,并删除 Alpha 通道以获得透明度。 经过这种转换后,LaTeX 可以轻松处理。
转换之前,图像在 Ghost Script 中渲染速度非常慢,并且根本无法在 epsviewer 中渲染。 GIMP 使用 Ghost Script 作为前端,因此导入速度很慢,但是导入后,所有处理和渲染都非常快。
The OP solved the problem by generating a PNG file directly. I had to use EPS because PNG and other formats aliased the image. I would have to convert to EPS anyway to include into a LaTeX file.
I used GIMP to import the 10 MB eps file generated from R image function. Then rotated, flattened, and saved as a 300KB eps file. Flattening merges all layers into a single layer and removes the alpha channel for transparency. Easily handled by LaTeX after this transformation.
Before transformation the image rendered very slowly in Ghost Script and couldn't be rendered at all in epsviewer. GIMP uses Ghost Script as a front end, so import is slow, but, once imported, all processing and rendering was very fast.