我在R中使用tikzDevice包来获得对乳胶友好的图形。我在删除下图顶部和底部的多余空格时遇到了问题:

我尝试过使用par(mar),但它似乎不适用于ggplot2。此外,theme(plot.margins)似乎也没有响应。当我尝试用theme(aspect.ratio)改变图形的纵横比时,引入了空格。有什么建议吗?谢谢!
编辑:以下是MWE:
library(tikzDevice)
library(reshape2)
x = seq(0,1,0.1)
y1 = x^2+2*x+7
y2= x^+2*x+2
df = data.frame(x,y1,y2)
df <- melt(df, id.vars=c("x"))
names(df) <- c("x","$latex~Name$","value")
plot <- ggplot(df,aes(x=x,y=value,color=`$latex~Name$`,group=`$latex~Name$`)) + geom_line() +
theme(aspect.ratio = 0.4)
plot
tikzDevice(file="mweTex.tex")
plot
dev.off()发布于 2016-12-16 22:50:40
这个问题似乎是由一些影响图像边界框的tikz语句引起的。由于在tikzDevice中似乎没有任何最小化空格的选项,所以我不得不考虑其他方法。我设法修复了生成的tikz文件,在MWE的末尾添加了以下R代码:
# remove all lines that invisibly mess up the bounding box
lines <- readLines(con="mweTex.tex")
lines <- lines[-which(grepl("\\path\\[clip\\]*", lines,perl=F))]
lines <- lines[-which(grepl("\\path\\[use as bounding box*", lines,perl=F))]
writeLines(lines,con="mweTex.tex")我已经在你的MWE上测试过了:

左边的图片没有修复,右边的图片有修复。
https://stackoverflow.com/questions/36023898
复制相似问题