如何让geom_curve在使用alpha时在箭头中显示恒定的透明度

问题描述 投票:0回答:1

我的目标是在地图上创建曲线(连接点),一侧有一个箭头。这些线将是半透明的。实现我的目标的最佳方法似乎是使用

geom_curve
(尽管其他
ggplot2
解决方案也很棒)。

因此,假设我有这个玩具情节:

library(tidyverse)
library(ggplot2)
data <- data.frame(x = 4, y = 20, xend = 7, yend = 15)

ggplot(data) + geom_curve(aes(x = x, y = y, xend = xend, yend = yend),
    arrow = arrow(length = unit(0.17, "npc"), type="closed", angle=20),
    colour = "red",
    linewidth = 5,
    angle = 90, 
    alpha=.2,
    lineend = "butt",
    curvature = -0.4,
) 

这会创建:

我面临的问题是,当将

geom_curve
与半透明线一起使用时,当我希望箭头保持一致时,箭头会以不同级别的透明度显示。我怎样才能防止这种情况发生?

这个问题与:Alpha 美学显示箭头的骨架而不是简单的形状 - 如何防止它?,除了它使用

geom_curve
而不是
geom_segment
。精彩的答案(https://stackoverflow.com/a/60587296/2049545)定义了一个新的几何形状(
geom_arrowbar
)。是否可以修改它以用于
geom_curve
?我还将标记另一个指向
geom_gene_arrow
的答案(https://stackoverflow.com/a/60655018/2049545) - 这可以与曲线一起使用吗?或者还有其他可行的解决方案吗?

r ggplot2 plot alpha-transparency
1个回答
0
投票

要真正正确地做到这一点,需要编写新版本的

curveGrob
和全新的 geom。

幸运的是,已经有人这么做了。 Teunbrand 未发布的 ggarrow 包包含一个

geom_arrow_curve
,所以你可以这样做:

remotes::install_github("teunbrand/ggarrow")

library(ggplot2)
library(ggarrow)

data <- data.frame(x = 4, y = 20, xend = 7, yend = 15)

ggplot(data) + 
  geom_arrow_curve(aes(x = x, y = y, xend = xend, yend = yend),
              colour = "red",
              linewidth = 5,
              angle = 90, 
              alpha = 0.2,
              lineend = "butt",
              curvature = -0.5)

© www.soinside.com 2019 - 2024. All rights reserved.