【问题标题】:Prevent blending when using alpha and geom_polygon使用 alpha 和 geom_polygon 时防止混合
【发布时间】:2017-06-27 21:06:39
【问题描述】:

我正在使用 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()

【问题讨论】:

    标签: r plot ggplot2 transparency polygon


    【解决方案1】:

    参数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')) 
    

    哪些情节:

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 2010-09-16
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多