在ggplot2中为重叠区域设置统一的alpha透明度
问题需求
我希望绘制一组区域后,整体区域显示统一的单一alpha透明度,而非重叠区域出现透明度叠加的效果。
当前实现及效果
library(tidyverse) dat <- tribble( ~xmin, ~xmax, ~ymin, ~ymax, 10, 30, 10, 30, 20, 40, 20, 40, 15, 35, 15, 25, 10, 15, 35, 40 ) ggplot() + geom_rect(data = dat, aes( xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax ), alpha = 0.5)
当前效果:
期望效果

Created on 2022-07-26 by the reprex package (v2.0.1)
我觉得解决方案可能和类似思路有关,但没完全搞懂,不确定。另外需要注意:示例里用了geom_rect(),但最终方案要能适配ggforce::geom_circle()。
编辑1
Quinten的第一个回答提到scale_alpha(range = ..., limits = ...),但没解决问题,因为这样只能得到完全不透明的区域。
编辑2
Quinten更新后的回答对矩形示例是可行的变通方案,但我需要方案适配ggforce::geom_circle(),补充以下示例:
library(ggforce) #> Lade nötiges Paket: ggplot2 dat <- data.frame( x = c(1, 1.3, 1.6), y = c(1, 1, 1), circle = c("yes", "yes", "no") ) ggplot() + coord_equal() + theme_classic() + geom_circle( data = subset(dat, circle == "yes"), aes(x0 = x, y0 = y, r = 0.5, alpha = circle), fill = "grey", color = NA, show.legend = TRUE ) + geom_point( data = dat, aes(x, y, color = circle) ) + scale_color_manual( values = c("yes" = "blue", "no" = "red") ) + scale_alpha_manual( values = c("yes" = 0.25, "no" = 0) )
示例效果:
Created on 2022-08-17 by the reprex package (v2.0.1)
内容的提问来源于stack exchange,提问作者Paul Schmidt
相关产品推荐
相关产品推荐

