我使用R来分析数据,使用ggplot创建图表,使用tikzDevice将其打印出来,最终使用latex创建报告。问题在于,由于latex的内存限制,具有许多点的大型图表会失败。我在这里https://github.com/yihui/tikzDevice/issues/103找到了一种解决方案,即在打印tikz文件之前对图表进行光栅化处理,从而允许单独打印点和文本。
在这个例子中,第一个
require(png)
require(ggplot2)
require(tikzDevice)
## generate data
n=1000000; x=rnorm(n); y=rnorm(n)
## first try primitive
tikz("test.tex",standAlone=TRUE)
plot(x,y)
dev.off()
## fails due to memory
system("pdflatex test.tex")
## rasterise points first
png("inner.png",width=8,height=6,units="in",res=300,bg="transparent")
par(mar=c(0,0,0,0))
plot.new(); plot.window(range(x), range(y))
usr <- par("usr")
points(x,y)
dev.off()
# create tikz file with rasterised points
im <- readPNG("inner.png",native=TRUE)
tikz("test.tex",7,6,standAlone=TRUE)
plot.new()
plot.window(usr[1:2],usr[3:4],xaxs="i",yaxs="i")
rasterImage(im, usr[1],usr[3],usr[2],usr[4])
axis(1); axis(2); box(); title(xlab="x",ylab="y")
dev.off()
## this works
system("pdflatex test.tex")
## now with ggplot
p <- ggplot(data.frame(x=x, y=y), aes(x=x, y=y)) + geom_point()
## what here?
在这个例子中,第一个
pdflatex
失败了。第二个成功是由于光栅化处理。如何在ggplot中应用这种方法?
geom_rasterise
或者geom_point(raster=T)
这样的东西;-) - Jonasggrastr
。 - jan-glx