如何防止在ggplot2中停止额外几何图形更改关键alpha值?

huangapple go评论130阅读模式
英文:

How can I stop additional geoms chaning the key alpha in ggplot2?

问题

我有两个使用部分透明颜色填充的ggplot2::geom_polygon图形。
我希望图例显示相同的透明度,但实际上它并没有。
理想的解决方案是,随着我添加具有相同填充和透明颜色的其他多边形,匹配的透明度也会缩放。

示例代码:

  1. chosen_alpha = 0.2
  2. ggplot() +
  3. theme_minimal() +
  4. geom_polygon(
  5. aes(
  6. fill = "A",
  7. x = c(1, 2, 2),
  8. y = c(1, 1, 2)
  9. ),
  10. alpha = chosen_alpha
  11. ) +
  12. geom_polygon(
  13. aes(
  14. fill = "A",
  15. x = c(3, 4, 4),
  16. y = c(3, 3, 4)
  17. ),
  18. alpha = chosen_alpha
  19. ) +
  20. guides(
  21. fill = guide_legend(
  22. override.aes = list(
  23. alpha = chosen_alpha
  24. ),
  25. )
  26. )

如果我只有一个多边形,这是有效的,如下所示。

  1. chosen_alpha = 0.2
  2. ggplot() +
  3. theme_minimal() +
  4. geom_polygon(
  5. aes(
  6. fill = "A",
  7. x = c(1, 2, 2),
  8. y = c(1, 1, 2)
  9. ),
  10. alpha = chosen_alpha
  11. ) +
  12. guides(
  13. fill = guide_legend(
  14. override.aes = list(
  15. alpha = chosen_alpha
  16. ),
  17. )
  18. )
英文:

I have two ggplot2::geom_pologons filled with a partially transparent colour.
I would like the key to display with the same alpha, but it does not.
The solution would ideally scale such that as I add additional polygons with the same fill and alpha colour the matching holds.

Example code:

  1. chosen_alpha = 0.2
  2. ggplot() +
  3. theme_minimal() +
  4. geom_polygon(
  5. aes(
  6. fill = "A",
  7. x = c(1, 2, 2),
  8. y = c(1, 1, 2)
  9. ),
  10. alpha = chosen_alpha
  11. ) +
  12. geom_polygon(
  13. aes(
  14. fill = "A",
  15. x = c(3, 4, 4),
  16. y = c(3, 3, 4)
  17. ),
  18. alpha = chosen_alpha
  19. ) +
  20. guides(
  21. fill = guide_legend(
  22. override.aes = list(
  23. alpha = chosen_alpha
  24. ),
  25. )
  26. )

This does work if I only have one of the polygons, as follows.

  1. chosen_alpha = 0.2
  2. ggplot() +
  3. theme_minimal() +
  4. geom_polygon(
  5. aes(
  6. fill = "A",
  7. x = c(1, 2, 2),
  8. y = c(1, 1, 2)
  9. ),
  10. alpha = chosen_alpha
  11. ) +
  12. guides(
  13. fill = guide_legend(
  14. override.aes = list(
  15. alpha = chosen_alpha
  16. ),
  17. )
  18. )

答案1

得分: 2

问题在于每个 geom 都会绘制一个键图形,也就是说,每添加一个 geom 就会有多个略微透明的矩形图层叠在一起,因此随着添加更多 geom,颜色会变得更加深色。

解决这个问题的最干净方法是只使用一个 geom_polygon 来绘制多边形。为此,将您的数据放入一个数据框中,并通过 group aes 来区分不同的多边形。

  1. library(ggplot2)
  2. chosen_alpha <- 0.2
  3. dat <- data.frame(
  4. x = c(c(1, 2, 2), c(2, 3, 3)),
  5. y = c(c(1, 1, 2), c(2, 2, 3)),
  6. fill = "A",
  7. group = rep(1:2, each = 3)
  8. )
  9. ggplot(dat) +
  10. theme_minimal() +
  11. geom_polygon(
  12. aes(
  13. fill = fill,
  14. x = x,
  15. y = y,
  16. group = group
  17. ),
  18. alpha = chosen_alpha
  19. )

第二个选项是将 key_glyph 设置为除一个 geom 之外的所有 geom 都为 "blank"。这样,键图形只会绘制一次:

  1. ggplot() +
  2. theme_minimal() +
  3. geom_polygon(
  4. aes(
  5. fill = "A",
  6. x = c(1, 2, 2),
  7. y = c(1, 1, 2)
  8. ),
  9. alpha = chosen_alpha,
  10. key_glyph = "blank"
  11. ) +
  12. geom_polygon(
  13. aes(
  14. fill = "A",
  15. x = c(2, 3, 3),
  16. y = c(2, 2, 3)
  17. ),
  18. alpha = chosen_alpha
  19. )

如何防止在ggplot2中停止额外几何图形更改关键alpha值?(第一种方法)

如何防止在ggplot2中停止额外几何图形更改关键alpha值?(第二种方法)

英文:

The issue is that for each geom a key glyph is drawn, i.e. you end up with multiple layers of slightly transparent rects which are drawn on top of each other and hence you end up with a darker color the more geoms are added.

The cleanest approach to fix your issue would be to use only one geom_polygon to draw your polygons. To this end put your data in a data.frame and account for the different polygons via the group aes.

  1. library(ggplot2)
  2. chosen_alpha &lt;- 0.2
  3. dat &lt;- data.frame(
  4. x = c(c(1, 2, 2), c(2, 3, 3)),
  5. y = c(c(1, 1, 2), c(2, 2, 3)),
  6. fill = &quot;A&quot;,
  7. group = rep(1:2, each = 3)
  8. )
  9. ggplot(dat) +
  10. theme_minimal() +
  11. geom_polygon(
  12. aes(
  13. fill = fill,
  14. x = x,
  15. y = y,
  16. group = group
  17. ),
  18. alpha = chosen_alpha
  19. )

如何防止在ggplot2中停止额外几何图形更改关键alpha值?<!-- -->

Second option would be to set the key_glyph to &quot;blank&quot; for all but one geom. This way the key glyph is only drawn once:

  1. ggplot() +
  2. theme_minimal() +
  3. geom_polygon(
  4. aes(
  5. fill = &quot;A&quot;,
  6. x = c(1, 2, 2),
  7. y = c(1, 1, 2)
  8. ),
  9. alpha = chosen_alpha,
  10. key_glyph = &quot;blank&quot;
  11. ) +
  12. geom_polygon(
  13. aes(
  14. fill = &quot;A&quot;,
  15. x = c(2, 3, 3),
  16. y = c(2, 2, 3)
  17. ),
  18. alpha = chosen_alpha
  19. )

如何防止在ggplot2中停止额外几何图形更改关键alpha值?<!-- -->

huangapple
  • 本文由 发表于 2023年8月10日 23:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定