没有geom的ggforce注释?

ggforce annotation without the geom?

我想创建一个 ggforce 注释(通过 geom_mark_* 函数),但我不想让 geom 的形状可见。我试过将各种 alpha 级别设置为零但无济于事。换句话说,我想保留注释线和标签 但不保留下面 reprex 中围绕点 的圆圈。我该如何隐藏它?

编辑:针对以下一种可能的解决方案,我无法使用颜色参数,因为我有不止一种背景颜色。

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.0.5
#> Warning: package 'dplyr' was built under R version 4.0.5
library(ggforce)
#> Warning: package 'ggforce' was built under R version 4.0.4

df2 <- tibble(
  x = 1:10,
  y = rnorm(10),
  z = LETTERS[1:10]
)

ggplot(df2, aes(x, y)) +
  geom_point() +
  geom_mark_circle(
    aes(label = z, filter = x == 5)
  ) +
  annotate(
    "rect",
    xmin = 0, 
    xmax = 5,
    ymin = -Inf,
    ymax = Inf,
    fill = "steelblue",
    alpha = 0.3
  ) +
  annotate(
    "rect",
    xmin = 5, 
    xmax = Inf,
    ymin = -Inf,
    ymax = Inf,
    fill = "firebrick",
    alpha = 0.3
  )

reprex package (v1.0.0)

于 2021-09-02 创建

我在工作中使用的一个技巧是将背景设置为与标记相同的颜色:

ggplot(df2, aes(x, y)) +
  geom_point() +
  geom_mark_circle(
    aes(label = z, filter = x == 5), color = "grey90"
  ) + 
  theme(panel.background = element_rect("grey90"))

编辑

ggplot(df2, aes(x, y)) +
  geom_point() +
  geom_mark_circle(
    aes(label = z, filter = x == 5), expand = unit(0, "mm")
  ) +
  annotate(
    "rect",
    xmin = 0, 
    xmax = 5,
    ymin = -Inf,
    ymax = Inf,
    fill = "steelblue",
    alpha = 0.3
  ) +
  annotate(
    "rect",
    xmin = 5, 
    xmax = Inf,
    ymin = -Inf,
    ymax = Inf,
    fill = "firebrick",
    alpha = 0.3
  )

一种适用于一个点和多个点的替代方法是简单地设置 linetype = 0,以跳过绘制 geom 的形状。

library(tidyverse)
library(ggforce)

df2 <- tibble(
  x = 1:10,
  y = rnorm(10),
  z = LETTERS[1:10]
)

p <- 
  ggplot(df2, aes(x, y)) +
  annotate(
    "rect",
    xmin = c(0, 5), xmax = c(5, Inf),
    ymin = c(-Inf, -Inf), ymax = c(Inf, Inf),
    fill = c("steelblue", "firebrick"),
    alpha = 0.3
  ) +
  geom_point()

单点:

p +
  geom_mark_circle(
    aes(label = z, filter = x == 5),
    linetype = 0
  )

对于多个点:

p +
  geom_mark_circle(
    aes(label = z, filter = x < 7.5 & x > 2.5),
    linetype = 0
  )

reprex package (v1.0.0)

于 2021-09-03 创建