在ggplot2中的单个方面注释文本 [英] Annotating text on individual facet in ggplot2

查看:23
本文介绍了在ggplot2中的单个方面注释文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码在绘图的最后一个方面注释一些文本:

I want to annotate some text on last facet of the plot with the following code:

library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)
p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5)
print(p)

但是这段代码在每个方面都对文本进行了注释.如何仅在一个方面获取带注释的文本?

But this code annotates the text on every facet. How can I get the annotated text on only one facet?

推荐答案

通常你会这样做:

ann_text <- data.frame(mpg = 15,wt = 5,lab = "Text",
                       cyl = factor(8,levels = c("4","6","8")))
p + geom_text(data = ann_text,label = "Text")

它应该可以在不完全指定因子变量的情况下工作,但可能会引发一些警告:

It should work without specifying the factor variable completely, but will probably throw some warnings:

这篇关于在ggplot2中的单个方面注释文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆