R:如何用文本框注释ggplot? [英] R: How can I annotate a ggplot with a text box?
本文介绍了R:如何用文本框注释ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望添加一个小的白色文本框,并在ggplot图的正文中添加自定义文本.我要添加的文本是识别要添加到绘图中的水平线.
I am looking to add a small white text box, with custom text in the body of my ggplot plot. The text I want to add is to identify a horizontal line I am adding to the plot.
ggplot(cb_emp) +
geom_point(aes(x = grossunits,
y = rate,
color = as.factor(outlier))
, alpha = 1/4) +
scale_color_discrete(name ="Outcome",
breaks=c(0, 1),
labels=c("Not outlier", "Outlier")) +
geom_hline(aes(yintercept = meancbrate)) +
geom_vline(aes(xintercept = meanac) +
annotate("text", x = max(grossunits), y = meancbrate, label = "avg rate")
这是我得到的情节:
这是我想要的情节(或类似的东西):
Here is the plot I want (or something like this):
请告诉我是否有实现此目标的简便方法.
Please let me know if there is an easy way of achieving this.
非常感谢您的帮助!
推荐答案
您可以简单地更改为
annotate("label", x = max(grossunits), y = meancbrate, label = "avg rate")
,它将使用geom_label
而不是geom_text
,因此您将在标签周围得到一个矩形.
which will use geom_label
rather than geom_text
and so you get a rectangle around the label.
这篇关于R:如何用文本框注释ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文