2010-06-14 50 views
11

將腳註添加到使用ggplot2創建的情節底部的最佳方法是什麼?我已經使用邏輯的組合注意到這裏試圖http://www.r-bloggers.com/r-good-practice-%E2%80%93-adding-footnotes-to-graphics/還有GGPLOT2註釋功能ggplot2腳註

p + annotate("text",label="Footnote", 
    x=unit(1,"npc") - unit(2, "mm"),y=unit(2, "mm"), 
    just=c("right", "bottom"),gp=gpar(cex= 0.7, col=grey(.5))) 

,但我得到的as.data.frame.default錯誤「錯誤(X [我] ,optional = TRUE,stringsAsFactors = stringsAsFactors):不能強制類c(「unit.arithmetic」,「unit」)到data.frame中。

+0

如果您在R中查看繪圖,但它看起來不像ggsave功能,則此功能可行。 – user338714 2010-06-14 18:18:14

+0

然後打開一個合適的圖形設備,而不是使用'ggsave()',例如'pdf(「filename.pdf」,width = 10,height = 6);打印(P); grid.text(...); dev.off()' – rcs 2010-06-14 18:37:41

回答

12

我會使用類似的東西:

pdf("filename.pdf", width=10, height=6) # open an appropriate graphics device 
print(p) 
makeFootnote() # from webpage above (uses grid.text; ggplot2 is based on grid) 
dev.off() 
+0

這很好用 - 謝謝! – user338714 2010-06-14 18:46:36

+2

是的,這非常好。另外,如果你需要ggplot2爲你的腳註留下一點餘量,試試這個:p + theme(plot.margin = unit(c(1,1,2,1),「lines」)) – Owen 2014-01-28 18:56:00

3

使用實驗室()函數直接加個註腳的情節。

p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + 
    geom_point() 
p + labs(caption = "(Pauloo, et al. 2017)")