我正在尝试旋转 R 中 ggplot 上的注释,类似于此问题,但使用带有背景的标签几何图形。
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)
Run Code Online (Sandbox Code Playgroud)
文本以白色背景显示,但不旋转。
是否有其他方法可以旋转标签?
目前无法使用 来完成此操作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)
Run Code Online (Sandbox Code Playgroud)