Making flattened pdfs in Sweave

左心房为你撑大大i 提交于 2019-11-30 18:10:51

问题


So I am creating pdfs using Sweave that include some graphs that have a ton of points on them. I can get the pdf well enough, but it seems to have created it with a ton of layers, so it's hard to open the file in Acrobat or Reader. When I do, I literally can watch the points load on the document.

Is there a way to flatten the pdf in Sweave so that it's not so bulky?

(Note that I am using RStudio. I know I should probably be using something else, but I haven't found anything that has worked this smoothly yet.)


回答1:


As Gavin said, there is no need to switch to knitr for this, though there are other advantages to do so. However, you don't even need to write your own saving and including code; Sweave can do that for you. If the initial document is:

\documentclass{article}
\usepackage[american]{babel}
\begin{document}

<<>>=
n <- 100000
DF <- data.frame(x=rnorm(n), y=rnorm(n))
@

<<gen_fig, fig=TRUE>>=
plot(DF)
@

\end{document}

Then just by changing the arguments to the figure chunk, you can get a PNG instead of a PDF:

<<gen_fig, fig=TRUE, png=TRUE, pdf=FALSE>>=
plot(DF)
@

In this simple example, it shrinks my final PDF from 685K to 70K.




回答2:


There is no need to switch to Knitr for this, though there are plenty of advantages in doing so.

One solution is just to arrange for the plot file to be produced and then include it yourself rather than rely on Sweave to do it for you

<<gen_fig, echo=true, eval=true>>=
png("path/to/fig/location/my_fig.png")
plot(1:10)
dev.off()
@

\includegraphics[options_here]{path/to/fig/location/my_fig}

Another option is to consider whether a plot with a "ton of points" is a useful figure - can you see all the points? Is the density of the points of interest? Alternatives include plotting via the hexbin package or generating a 2-d density of the points and plotting that as a lower-density set of points. The ggplot2 package has plenty of this functionality built in, see e.g. stat__bin2d() or stat_binhex() for examples.




回答3:


As has already been mentioned you should probably switch to knitr, which makes swapping between pdfs and other formats much nicer. In particular, you should look at:

  • the transition guide between knitr and sweave
  • global options: that way you can easily swap between pdfs, high-res png and low-res pngs.
  • caching: only generate the figures when needed.

Here is an example of using the PNG device:

\documentclass{article}
\begin{document}

<<gen_fig, dev='png'>>=
n <- 100000
DF <- data.frame(x=rnorm(n), y=rnorm(n))
plot(DF)
@

\end{document}

There is no need to specify fig=TRUE for knitr. If the image quality of the PNG device in the graphics package is not enough, you can easily switch to other PNG devices, e.g. dev='CairoPNG' or 'Cairo_png'. In Sweave you just write more code to do the same thing.



来源:https://stackoverflow.com/questions/13115898/making-flattened-pdfs-in-sweave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!