如何在图表底部添加注释?

时间:2016-05-30 00:20:53

标签: r ggplot2

基本上我想在图表下面记下我的发现,例如"在2000年到2006年之间,病例增加了28%。"

我尝试过使用annotation_custom但是在facet_grid的情况下,它将它放在图表的每个方面下方,而不是完全在图形区域之外。我还试图在SO上使用其他搜索的网格排列,但是在我能看到的任何地方都没有显示任何文本。

library(ggplot2)
...
graph <- ggplot(data=dataSource, aes(x=factor(year), y=Cases, fill = type))
graph <- graph+geom_bar(stat="identity")+facet_grid(.~type) 
graph <- graph+labs(title="Some title here", x="Year", y="Cases by county", fill="Type")
print(graph)

2 个答案:

答案 0 :(得分:3)

在cowplot包中使用add_sub。以下是help("add_sub")的示例部分中的第一个示例:

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars, aes(mpg, disp)) + 
        geom_line(colour = "blue") + 
        background_grid(minor='none')
ggdraw(add_sub(p1, "This is an annotation.\nAnnotations can span multiple lines."))

screenshot

答案 1 :(得分:0)

我发现grid.arrange

没有问题
gridExtra::grid.arrange(ggplot(), bottom = "annotation here")

enter image description here