gpt4 book ai didi

r - 使用 alpha 和 geom_polygon 时防止混合

转载 作者:行者123 更新时间:2023-12-04 01:03:37 24 4
gpt4 key购买 nike

我正在使用 geom_polygon 叠加数据,但无法区分颜色,因为它们合并在一起。如果层数更多,情况会更糟。

如何确保颜色不会混合?

示例数据

newdat <- structure(list(x = c(0, 0.77, 1.54, 2.31, 3.08, 3.85, 4.62, 5.38, 
6.15, 6.92, 7.69, 8.46, 9.23, 10, 10.77, 11.54, 12.31, 13.08,
13.85, 14.62, 15.38, 16.15, 16.92, 17.69, 18.46, 19.23, 20, 20.77,
21.54, 22.31, 23.08, 23.85, 24.62, 25.38, 26.15, 26.92, 27.69,
28.46, 29.23, 30, 0, 0.77, 1.54, 2.31, 3.08, 3.85, 4.62, 5.38,
6.15, 6.92, 7.69, 8.46, 9.23, 10, 10.77, 11.54, 12.31, 13.08,
13.85, 14.62, 15.38, 16.15, 16.92, 17.69, 18.46, 19.23, 20, 20.77,
21.54, 22.31, 23.08, 23.85, 24.62, 25.38, 26.15, 26.92, 27.69,
28.46, 29.23, 30), y = c(0, 0, 0, 0, 0, 0, 0.01, 0.01, 0.02,
0.03, 0.04, 0.05, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.05, 0.04,
0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.01, 0.02, 0.03, 0.05,
0.06, 0.08, 0.09, 0.1, 0.1, 0.1, 0.1, 0.09, 0.08, 0.07, 0.06,
0.05, 0.05, 0.05, 0.04, 0.03, 0.02, 0.01, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), type = c("a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b")), .Names = c("x", "y", "type"
), row.names = c(NA, -80L), class = "data.frame")

代码

library(ggplot2)

# Using polygon blends colors
ggplot(newdat, aes(x = x, y = y, fill = type)) + geom_polygon(alpha = 0.1)

# But using line shows they are different
ggplot(newdat, aes(x = x, y = y, color = type)) + geom_line()

情节

enter image description here

enter image description here

最佳答案

参数 alpha 设置透明度,这就是为什么你看到它们几乎相同;但是,解决方案不是将其设置为 1,因为它会导致看不到 a 类型。

您可以将颜色设置为可区分的颜色,同时增加alpha:

   ggplot(newdat, aes(x = x, y = y, fill = type )) + geom_polygon(alpha = 0.3) +
scale_fill_manual(values = c('red','lightblue'))

哪些地 block :

              /image/3v7XI.png

关于r - 使用 alpha 和 geom_polygon 时防止混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44789886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com