使用annotate为不同的分面添加不同的注释

20

我想要在一个图形的不同分面上添加面板标签。我希望它们是1:7,但是以下代码似乎没有工作:

d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
     xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)

d1<-d + facet_wrap(~ color)

d1+annotate("text", x=0.25, y=1.5e+04, label=1:7)
产生。
Error: When _setting_ aesthetics, they may only take one value. Problems: label

现在,我可以提供一个单一的值,并将其复制到所有维度上。但是如何在使用annotate()时在不同的维度中使用不同的标签?

1个回答

28

使用annotate是无法实现的。但是,通过设置一个data.frame并将其用作geom_text的数据源,这很容易实现(需要一些簿记方面的考虑)。

d1 + geom_text(data=data.frame(x=0.25, y=1.5e+04, label=1:7, 
                               color=c("D","E","F","G","H","I","J")), 
               aes(x,y,label=label), inherit.aes=FALSE)

在此输入图片描述


如果您想将文本添加到多行,请确保您在文本“data.frame”中的colnames()与您即将绘制的数据的列名匹配。 - Kots

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