r - 使用 ggplot2 自定义维恩重叠区域的颜色
问题描述
我正在使用 R,ggplot2 中的维恩图,我希望可以选择为不同的部分着色,包括两个圆圈之间的重叠。我仅限于使用 ggplot2。
这就是我已经走了多远。在这里,我为两个圆圈分配了自定义颜色,但无法控制重叠的颜色。
library(ggplot2)
df.venn <- data.frame(x = c(-0.5, 0.5),
y = c(0, 0),
labels = c("A", "B"),
stringsAsFactors = FALSE)
ggplot2::ggplot(data=df.venn) +
ggforce::geom_circle(
ggplot2::aes_string(x0 = "x", y0 = "y", r = 1.5, fill = "labels"),
alpha = 0.3,
size = 0.5,
colour = 'darkgray'
) +
ggplot2::coord_fixed() +
ggplot2::theme_void() +
ggplot2::scale_fill_manual(values = c("red", "blue"))
我希望能够选择性地为重叠区域着色,例如允许左右圆形部分为灰色,而重叠部分为纯红色。这可能吗?
我感谢任何输入!
解决方案
您可以通过计算重叠并将其作为 geom_polygon 来作弊。
由于您的圆的公式是 (x +/- 0.5)^2 + y^2 = 1.5,因此您可以计算 y 范围,因为交点必须发生在 y 轴上。
library(ggplot2)
df.venn <- data.frame(x = c(-0.5, 0.5),
y = c(0, 0),
labels = c("A", "B"),
stringsAsFactors = FALSE)
yvals <- seq(-sqrt(2), sqrt(2), 0.01)
xvals <- sqrt(2.25 - yvals^2) - 0.5
yvals <- c(yvals, rev(yvals))
xvals <- c(xvals, -xvals)
combo <- data.frame(x = xvals, y = yvals)
ggplot2::ggplot(data = df.venn) +
ggforce::geom_circle(
ggplot2::aes_string(x0 = "x", y0 = "y", r = 1.5, fill = "labels"),
alpha = 0.3,
size = 1,
colour = 'darkgray'
) +
ggplot2::geom_polygon(data = combo, aes(x = x, y = y), fill = "red") +
ggplot2::coord_fixed() +
ggplot2::theme_void() +
ggplot2::scale_fill_manual(values = c("gray50", "gray50"))
给你:
推荐阅读
- laravel - 测试用户可以查看登录表单
- java - Spring security UserDetails 和控制器(获取用户)
- swift - Swift 保存常量时 Enum.rawValue 和静态常量属性之间的性能差异是什么?
- python - 使用条件断点进行 IntelliJ/PyCharm 调试:未定义函数参数
- reactjs - 是否可以通过状态更新 react-hook-form defaultValue?
- javascript - Chrome(系统)function_data每帧内存使用情况
- python - 了解使用数组引用并将它们附加到字典时会发生什么
- java - 如何使用现有证书连接到服务器以在 Java 中通过 HTTPS 获取 JSON 响应?
- installation - 使用 pip 安装包我正在尝试在我的 envn 'acevision' 中安装包,但它显示来自其他 envn 'high-hrnet'
- java - Selenium :- 我想用 java 连接 selenium 中的两个 div 值。想要值作为(强度+25G 植物蛋白)