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

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

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

问题

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

示例代码:

chosen_alpha = 0.2
ggplot() +
    theme_minimal() +
    geom_polygon(
        aes(
            fill = "A",
            x = c(1, 2, 2),
            y = c(1, 1, 2)
        ),
        alpha = chosen_alpha
    ) +
    geom_polygon(
        aes(
            fill = "A",
            x = c(3, 4, 4),
            y = c(3, 3, 4)
        ),
        alpha = chosen_alpha
    ) +
    guides(
        fill = guide_legend(
            override.aes = list(
                alpha = chosen_alpha
            ),
        )
    )

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

chosen_alpha = 0.2
ggplot() +
    theme_minimal() +
    geom_polygon(
        aes(
            fill = "A",
            x = c(1, 2, 2),
            y = c(1, 1, 2)
        ),
        alpha = chosen_alpha
    ) +
    guides(
        fill = guide_legend(
            override.aes = list(
                alpha = chosen_alpha
            ),
        )
    )
英文:

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:

chosen_alpha = 0.2
ggplot() +
    theme_minimal() +
    geom_polygon(
        aes(
            fill = "A",
            x = c(1, 2, 2),
            y = c(1, 1, 2)
        ),
        alpha = chosen_alpha
    ) +
    geom_polygon(
        aes(
            fill = "A",
            x = c(3, 4, 4),
            y = c(3, 3, 4)
        ),
        alpha = chosen_alpha
    ) +
    guides(
        fill = guide_legend(
            override.aes = list(
                alpha = chosen_alpha
            ),
        )
    )

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

chosen_alpha = 0.2
ggplot() +
    theme_minimal() +
    geom_polygon(
        aes(
            fill = "A",
            x = c(1, 2, 2),
            y = c(1, 1, 2)
        ),
        alpha = chosen_alpha
    ) +
    guides(
        fill = guide_legend(
            override.aes = list(
                alpha = chosen_alpha
            ),
        )
    )

答案1

得分: 2

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

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

library(ggplot2)

chosen_alpha <- 0.2

dat <- data.frame(
  x = c(c(1, 2, 2), c(2, 3, 3)),
  y = c(c(1, 1, 2), c(2, 2, 3)),
  fill = "A",
  group = rep(1:2, each = 3)
)
ggplot(dat) +
  theme_minimal() +
  geom_polygon(
    aes(
      fill = fill,
      x = x,
      y = y,
      group = group
    ),
    alpha = chosen_alpha
  )

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

ggplot() +
  theme_minimal() +
  geom_polygon(
    aes(
      fill = "A",
      x = c(1, 2, 2),
      y = c(1, 1, 2)
    ),
    alpha = chosen_alpha,
    key_glyph = "blank"
  ) +
  geom_polygon(
    aes(
      fill = "A",
      x = c(2, 3, 3),
      y = c(2, 2, 3)
    ),
    alpha = chosen_alpha
  )

如何防止在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.

library(ggplot2)

chosen_alpha &lt;- 0.2

dat &lt;- data.frame(
  x = c(c(1, 2, 2), c(2, 3, 3)),
  y = c(c(1, 1, 2), c(2, 2, 3)),
  fill = &quot;A&quot;,
  group = rep(1:2, each = 3)
)
ggplot(dat) +
  theme_minimal() +
  geom_polygon(
    aes(
      fill = fill,
      x = x,
      y = y,
      group = group
    ),
    alpha = chosen_alpha
  )

如何防止在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:

ggplot() +
  theme_minimal() +
  geom_polygon(
    aes(
      fill = &quot;A&quot;,
      x = c(1, 2, 2),
      y = c(1, 1, 2)
    ),
    alpha = chosen_alpha,
    key_glyph = &quot;blank&quot;
  ) +
  geom_polygon(
    aes(
      fill = &quot;A&quot;,
      x = c(2, 3, 3),
      y = c(2, 2, 3)
    ),
    alpha = chosen_alpha
  )

如何防止在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:

确定