r - 没有 geom 的 ggforce 注释?

标签 r ggplot2 ggforce

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

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

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 于 2021 年 9 月 2 日创建(v1.0.0)

最佳答案

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

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

enter image description here

编辑

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
  )

enter image description here

关于r - 没有 geom 的 ggforce 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69038139/

相关文章:

r - 查找列中具有不同值的对象的平均值

r - 为小组创建一个具有不同颜色的 180 度玫瑰图?

R/ggplot2 : how to match legend and plot colors in overlapping area plots?

r - 通过gganimate和ggforce进行动态小平面缩放的动画图?

两个特定模式之间的正则表达式(包括换行符)

r - 通过产生 NA 来捕捉错误

r - 具有时间点的分箱纵向数据的意大利面条图

r - 如何通过放大两个不同区域来创建两个插入图?原始情节是用 `ggplot2`制作的

R install.packages polyclip : where is config. 日志?在之前的帖子中没有答案

r - 将社区数据转换为纯素包的宽格式