Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
            ),
        )
    )

>Solution :

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 <- 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
  )

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

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
  )

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading