Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should alpha in geoms that have polygons and points only affect the interior of polygons? #6006

Closed
davidhodge931 opened this issue Jul 17, 2024 · 4 comments

Comments

@davidhodge931
Copy link

davidhodge931 commented Jul 17, 2024

alpha in ggplot2 for geoms that have polygons and points affects any polygon interiors and points.

This only affects geom_boxplot and geom_sf (to my knowledge).

Conceptually, points and lines are generally treated consistently in ggplot2, apart from this (that I'm aware of). E.g. in how colour is applied to points and lines, or in in geom_pointrange where alpha affects both point and line.

So I think it'd be most intuitive, if in geoms with polygon interiors and points/lines, alpha only affected polygon interiors.

The most common use-case of the alpha argument in geom_sf is to affect the fill of polygons. It'd is nice to be able to set this once using update_geom_defaults. However, you can't do this, if you only want it to only affect polygons and not the points.

To fix this:

  • in geom_boxplot, outlier.alpha = NULL update to default to NA instead of inheriting from alpha
  • in geom_sf, update so that it only affects polygon interiors, regardless of the sf object geometry type

Related past issues to this were discussed in #1371 and #1523.

Thanks for your consideration, and sorry to bombard with issues :)

library(tidyverse)
library(sf)

d1 <- sf::st_read(system.file("shape/nc.shp", package = "sf")) 

d2 <- sf::st_read(system.file("shape/nc.shp", package = "sf")) |>
  sf::st_centroid()

bind_rows(d1, d2) |> 
  ggplot() + 
  geom_sf(fill = "red", alpha = 0.1)

palmerpenguins::penguins |>
  ggplot() +
  geom_boxplot(
    aes(x = flipper_length_mm,
        y = species,
        col = sex, 
        fill = sex),
    alpha = 0.5,
  )
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_boxplot()`).

Created on 2024-07-18 with reprex v2.1.0

@davidhodge931 davidhodge931 changed the title Should alpha in geoms that include polygons and points/lines _only_ affect the fill of polygons? Should alpha in geoms that include polygons and points/lines only affect the interior of polygons? Jul 17, 2024
@davidhodge931 davidhodge931 changed the title Should alpha in geoms that include polygons and points/lines only affect the interior of polygons? Should alpha in geoms that include polygons and points only affect the interior of polygons? Jul 17, 2024
@davidhodge931 davidhodge931 changed the title Should alpha in geoms that include polygons and points only affect the interior of polygons? Should alpha in geoms that have polygons and points only affect the interior of polygons? Jul 17, 2024
@teunbrand
Copy link
Collaborator

Thanks for your suggestions David!

I'm not sure I agree with the premise that in compound polygon/point layers alpha should only be applied to the polygon fill.
This mostly has to do with how data is represented for geoms.

In geom_boxplot() we have the polygons (boxes) and points combined in a single row. It seems natural to me that alpha applies to the entire row, points included, unless intervened with outlier.apha.

In geom_sf() we have separate rows for points and polygons. If their alphas should be different, you should set the alpha per row instead of globally.

geom_sf() has a complex default borrowing from other geoms. If you need different logic surrounding its defaults, I suggest building an extension and overriding the GeomSf$use_defaults() method.

@davidhodge931
Copy link
Author

Ahh, I see, thanks @teunbrand

Assume there is no enthusiasm for having sub-alpha concepts?

So:

  • alpha_colour: the alpha of points, lines and outlines in the geom
  • alpha_fill: the alpha of all interior fill of polygons in the geom

It could be hierarchial, so if alpha_colour is specified it over-rides alpha for the relevant components - and likewise for alpha_fill

@teunbrand
Copy link
Collaborator

There was some discussion around specialised sub-aesthetics in #5454 (comment). But the TL;DR is: that is fine for extension packages but we didn't want to set this as a precedent in ggplot2.
I also think there was related discussion in another issue, but I can't seem to find it back at the moment.

@davidhodge931
Copy link
Author

davidhodge931 commented Jul 18, 2024

Awesome, thanks for the TLDR. Thought you guys had already considered it somewhere. Yeah, that makes sense. Definitely want to keep the complexity manageable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants