r - 没有geom的ggforce注释?
问题描述
我想创建一个 ggforce 注释(通过geom_mark_*
函数),但我不希望几何图形的形状可见。我尝试将各种 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 包(v1.0.0)于 2021 年 9 月 2 日创建
解决方案
一种适用于一个点和多个点的替代方法是简单地设置linetype = 0
, 以跳过绘制几何图形的形状。
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 包于 2021-09-03 创建(v1.0.0)
推荐阅读
- python - 当另一列包含此子字符串时,Pandas 删除列中字符串的子字符串
- swift - 如何在 OSX App 中设置查看文件夹的权限?
- javascript - 我不会在上传 Javascript 时提交表单
- php - Laravel 中的干预图像大小调整和缓存
- raspberry-pi - Raspberry Pi 2 Raspbian 自动启动,用于 Kiosk 的全屏浏览器
- r - 在 R 中的 While 循环中创建表
- javascript - JavaScript 三元运算符和 if/else 语句
- java - SSL Websocket 使用 StompClient -- SSL 证书异常
- c# - C# + LibreOffice SDK
- algorithm - 对较大的 n 值使用匈牙利算法是否仍然有效?