问题 ggplot2脚注


在使用ggplot2创建的绘图底部添加脚注的最佳方法是什么?我尝试过使用这里提到的逻辑组合 http://www.r-bloggers.com/r-good-practice-%E2%80%93-adding-footnotes-to-graphics/ 以及ggplot2 annotate函数

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 [[i]]中的错误,可选= TRUE,stringsAsFactors = stringsAsFactors):无法将类c(”unit.arithmetic“,”unit“)强制转换为data.frame”。


9226
2018-06-14 17:33


起源

如果您在R中查看绘图但是看起来它不适用于ggsave函数,则此方法有效。 - user338714
然后打开一个合适的图形设备而不是使用 ggsave(),例如, pdf("filename.pdf", width=10, height=6); print(p); grid.text(...); dev.off() - rcs


答案:


我会用这样的东西:

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()

12
2018-06-14 18:40



这很棒 - 谢谢! - user338714
是的,这很棒。此外,如果你需要ggplot2为你的脚注留一点余地,试试这个:p + theme(plot.margin = unit(c(1,1,2,1),“lines”)) - Owen


使用labs()函数将脚注直接添加到绘图中。

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

3
2017-07-31 07:01