2012-02-22 39 views
3

我使用savePlot以多种格式保存图表。我有一个PDF格式的问题:我的图例被截断在PDF文件中,但不是在R图窗口中。我在Windows 7下面是一个小例子表明,传说在屏幕上看起来很大:使用savePlot以pdf格式保存图例时截图

win.graph(width=4.375,height=2.8) 
par(bty="l",las=1,lwd=1,cex=0.7,oma = c(1, 1, 0, 1), mar = c(3, 4, 3.5, 5)) 
plot(1:10,type="l") 
legend.names <-c("Dividend yield (L)","Core CPI (L)", 
    "Dividend yield - core CPI spread (R)") 
legend("bottomright", legend=legend.names, lwd=1, cex=0.7, col =1:3,lty=1:3) 

但是,当我尝试保存图表为PDF,传说被截断。最后的'(R)'并不完整。

savePlot(filename = paste("c:/out.pdf",sep=""), type ="pdf") 

enter image description here

+0

您是否尝试过手动设置pdf的大小? – Stedy 2012-02-22 17:51:16

+0

我正在使用'savePlot',因为我需要生成多个图形文件。绘制图表比较容易,并使用'savePlot'将其保存为多种格式。 pdf的大小与'win.graph()'所调用的相同' – 2012-02-22 17:55:44

+0

您可能对win.graph和pdf设备有不同的字体。您可以更改默认值。 – 2012-02-22 18:01:40

回答

3

的溶液是打印直成pdf()设备,而不是通过一个窗口中的一个:

pdf(width=4.375, height=2.8, file = "out.pdf") 

par(bty="l",las=1,lwd=1,cex=0.7,oma = c(1, 1, 0, 1), mar = c(3, 4, 3.5, 5)) 
plot(1:10,type="l") 
legend.names <-c("Dividend yield (L)","Core CPI (L)", 
    "Dividend yield - core CPI spread (R)") 
legend("bottomright", legend=legend.names, lwd=1, cex=0.7, col =1:3,lty=1:3) 

dev.off() 
+0

如前所述,我使用'savePlot',因为我必须以多种格式保存图表。您的解决方案需要多次重复剧情调用。必须有一种方法可以用'savePlot'做到这一点,因此赏金。 – 2012-02-25 00:44:06

+0

好吧,你可以用一个函数('tmpplot < - function(){...}')包装剧情代码的主体,这会比使用'savePlot'难。但我同意,如果有更好的解决方案,这将是很好的。 – 2012-02-25 00:45:50

1

你可以切换到更小的“黑体窄边”字型,使用此功能可以在打印到pdf时覆盖savePlot()的行为:

my.savePlot <- function(filename, type) { 
    if (type == "pdf") { 
     dev.copy(pdf, filename, width = par("din")[1], 
           height = par("din")[2], 
           family = "Helvetica-Narrow") 
     invisible(dev.off()) 
    } else { 
     savePlot(filename, type) 
    } 
} 
+0

这将图表中的所有文本转换为“Helvetica-Narrow”。我不介意它是否只是传说。 – 2012-02-25 11:21:15

0

让我们看看你对此有何评论,或许是第三次的魅力!在这里,我们利用recordPlot()replayPlot(),所以我们可以将图复制到pdf()。请注意,legend()调用必须包装为recordGraphics(),以便将其记录在图形的显示列表中。

my.savePlot <- function(filename, type) { 
    if (type == "pdf") { 
     saved.plot <- recordPlot() 
     pdf(filename, width = par("din")[1], 
        height = par("din")[2]) 
     replayPlot(saved.plot) 
     invisible(dev.off()) 
    } else { 
     savePlot(filename, type) 
    } 
} 

win.graph(width=4.375,height=2.8) 
par(bty="l",las=1,lwd=1,cex=0.7,oma = c(1, 1, 0, 1), mar = c(3, 4, 3.5, 5)) 
plot(1:10,type="l") 
legend.names <-c("Dividend yield (L)","Core CPI (L)", 
       "Dividend yield - core CPI spread (R)") 
recordGraphics(legend("bottomright", legend=legend.names, 
         lwd=1, cex=0.7, col =1:3,lty=1:3), 
       list(), getNamespace("graphics")) 

my.savePlot(filename = paste("out.pdf",sep=""), type ="pdf") 
0

你为什么不试试这种方式?它不会再造成问题,并且您可以更好地控制它。

pdf("out.pdf", width=4.375,height=2.8) 
par(bty="l",las=1,lwd=1,cex=0.7,oma = c(1, 1, 0, 1), mar = c(3, 4, 3.5, 5)) 
plot(1:10,type="l") 
legend.names <-c("Dividend yield (L)","Core CPI (L)", 
      "Dividend yield - core CPI spread (R)") 
legend("bottomright", legend=legend.names, lwd=1, cex=0.7, col =1:3,lty=1:3) 
dev.off() 

唔...,flodel得到了相同的答案,我同意,你应该写或者包装器,或写自己的savePlot功能效果很好。

0

尝试inset=0.05。这在过去帮助我解决了类似的问题,虽然我没有用于测试的Win7机器。