2012-02-22 44 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機器。