在绘图区域外添加文本

时间:2019-02-19 22:50:59

标签: r ggplot2

示例代码

library(ggplot2)
ggplot(mtcars, aes(mpg, cyl, color = vs)) + geom_line()

如何像在使用上图所示的示例中那样向x轴添加任意文本,然后在上面写上“任意单词”

enter image description here

2 个答案:

答案 0 :(得分:3)

我不确定您要做什么,这可能会也可能不会很好地概括。

也就是说,一种可能性是将annotatecoord_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")

enter image description here

答案 1 :(得分:1)

您可以在tag中使用ggplot2(请参阅?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))

enter image description here