如何只旋转ggplot中注释中的文本?

hal*_*ass 31 r ggplot2

我有这样的情节:

fake = data.frame(x=rnorm(100), y=rnorm(100))

ggplot(data=fake, aes(x=x, y=y)) + geom_point() + theme_bw() +
  geom_vline(xintercept=-1, linetype=2, color="red") +
  annotate("text", x=-1, y=-1, label="Helpful annotation", color="red")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何将带注释的文本旋转90度,使其与参考线平行?

Gre*_*gor 61

告诉它你想要的角度.

ggplot(data = fake, aes(x = x, y = y)) + 
    geom_point() +
    theme_bw() +
    geom_vline(xintercept = -1, linetype = 2, color = "red") +
    annotate(geom = "text", x = -1, y = -1, label = "Helpful annotation", color = "red",
             angle = 90)
Run Code Online (Sandbox Code Playgroud)

?geom_text你可以看到,angle是一个可能的审美,而annotate将沿着通过它,就像任何其他的说法geom_text理解(如x,y,label,和color已被使用).

  • @orrymr [这是文档页面](https://ggplot2.tidyverse.org/reference/geom_text.html)。在页面中搜索“角度”,您就会找到它。它列在“geom_text 了解以下美学...”列表下,并在示例中使用。 (2认同)