通过诸如"top","bottom","left","right","center"之类的关键字来指定geom_text的位置. [英] Specify position of geom_text by keywords like "top", "bottom", "left", "right", "center"

查看:171
本文介绍了通过诸如"top","bottom","left","right","center"之类的关键字来指定geom_text的位置.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将文本放置在ggplot中而不指定xy位置,而是使用关键字,例如还可以通过将x设置为列表"bottomright","bottom","bottomleft","left","topleft"中的单个关键字来指定位置. ,顶部",顶部右侧",右侧"和中心" ).

I wish to position text in a ggplot without specifying x and y positions, but instead using keywords, like e.g. in graphics::legend ("The location may also be specified by setting x to a single keyword from the list "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center").

让我们说我正在制作这样的图形.

Lets say I'm making a graph like this.

sp <- ggplot(mpg, aes(hwy, cty, label = "sometext")) +
       geom_point()

我想在每个图形中添加要以相同方式打印的标签.调用以下命令仅会在提供给aes的每个xy值处打印文本.

I want to add the label to be printed in the same way in every graph. Calling the following simply prints text at every x, y value supplied to aes.

sp + geom_text()

我可以操纵提供给geom_text()xy数据以确保文本在图形之间保持相同的相对位置,但是没有一种简单的方法来调用默认位置,例如"bottom"等? IE. sp + geom_text(position = "top").

I could manipulate the x and y data supplied to geom_text() to ensure that the text remained in the same relative position between graphs, but is there not a simply way to call position by default positions like "top", "bottom" etc? I.e. sp + geom_text(position = "top").

推荐答案

geom_text要根据您的数据集绘制标签.听起来您想在绘图中添加单个文本,在这种情况下,annotate是更好的选择.要强制标签显示在相同位置而不管绘图中的单位如何,可以利用Inf值:

geom_text wants to plot labels based on your data set. It sounds like you're looking to add a single piece of text to your plot, in which case, annotate is the better option. To force the label to appear in the same position regardless of the units in the plot, you can take advantage of Inf values:

sp <- ggplot(mpg, aes(hwy, cty, label = "sometext"))+
  geom_point() +
  annotate(geom = 'text', label = 'sometext', x = -Inf, y = Inf, hjust = 0, vjust = 1)
print(sp)

这篇关于通过诸如"top","bottom","left","right","center"之类的关键字来指定geom_text的位置.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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