Skip to content

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

Closed
@davidhodge931

Description

@davidhodge931

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions