在绘图区域外添加文本

bvo*_*owe 7 r ggplot2

示例代码

library(ggplot2)
ggplot(mtcars, aes(mpg, cyl, color = vs)) + geom_line()
Run Code Online (Sandbox Code Playgroud)

如果可以像使用上面的图制作的本示例中那样将任意文本添加到 x 轴,然后我在上面写了“任意文字”

在此处输入图片说明

PoG*_*bas 13

您可以使用tagin ggplot2(see ?ggplot2::labs) 并指定它的位置theme

library(ggplot2)
ggplot(mtcars, aes(mpg, cyl, color = vs)) + 
    geom_line() +
    labs(tag = "arbitrary words") +
    theme(plot.tag.position = c(0.15, 0.02))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 是否可以添加多个标签? (7认同)

Mau*_*ers 12

我不完全确定你想要做什么,所以这可能会也可能不会很好地概括。

也就是说,一种可能性是使用annotatewithcoord_cartesian(clip = "off")允许绘图区域之外的文本。

ggplot(mtcars, aes(mpg, cyl, color = vs)) +
    geom_line() +
    annotate("text", x = 12.5, y = 3.5, label = "Arbitrary text") +
    coord_cartesian(ylim = c(4, 8), clip = "off")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明