【问题标题】:Rotate label annotation in ggplot2在ggplot2中旋转标签注释
【发布时间】:2021-08-28 10:17:54
【问题描述】:

我正在尝试在 R 中旋转 ggplot 上的注释,类似于 this question,但使用带有背景的标签几何图形。

将适用于geom = "text"geom_text 的代码与geom = 'label'geom_label 一起使用会导致注释未旋转。

fake = data.frame(x=rnorm(100), y=rnorm(100))
ggplot(data = fake, aes(x = x, y = y)) + 
    geom_point() +
    geom_vline(xintercept = -1, linetype = 2, color = "red") +
#    annotate(geom = "text", x = -1, y = -1, label = "Helpful annotation", color = "red",
#             angle = 90)
    annotate(geom = "label", x = -1, y = -1, label = "Helpful annotation", color = "red",
             angle = 90)

文本以白色背景显示,但没有旋转。

是否有其他方法可以旋转标签?

【问题讨论】:

  • geom_label()annotate(geom = "label"... 参数相同,不支持旋转。你可以使用ggtext::geom_richtext()。它使用相同的参数,在本例中为 angle

标签: r ggplot2


【解决方案1】:

目前无法使用 geom_label 完成此操作。来自帮助:“目前 geom_label() 不支持 check_overlap 参数或角度美学。”

但这可以通过ggtext 包中的相关函数来完成:

ggplot(data = fake, aes(x = x, y = y)) + 
    geom_point() +
    geom_vline(xintercept = -1, linetype = 2, color = "red") +
    ggtext::geom_richtext(x = -1, y = -1, label = "Helpful annotation", angle = 90)

【讨论】:

  • ... 或 annotate(geom = "richtext", ...) 如果已加载 ggtext
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2022-01-11
  • 2023-03-27
  • 2020-10-10
  • 2021-12-21
  • 2011-11-03
相关资源
最近更新 更多