ggplot2注脚

22

如何在使用ggplot2创建的图表底部添加脚注?我已经尝试过结合这里提到的逻辑以及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)))

但我收到了以下错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class c("unit.arithmetic", "unit") into a data.frame


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

37

labs(caption = "my caption") 添加了一个脚注:

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

在此输入图像描述


1
我们可以将标题左对齐吗? - akash ansari
2
尝试使用 + theme(plot.caption = element_text(hjust = 0))。要使图例居中,使用 just = 0.5。默认情况下,hjust = 1hjust代表“水平对齐”,范围从0到1,从左到右,因此0是全部向左,1是全部向右,0.5在中间,依此类推。 - Rich Pauloo

14

我会使用类似这样的东西:

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

2
是的,这非常好。另外,如果您需要让ggplot2为您的脚注留出更多的边距,请尝试使用以下代码:p + theme(plot.margin = unit(c(1,1,2,1), "lines"))。 - Owen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接