Skip to content

Allow select alpha to be applied on fill (default), colour, or both - #3485

Closed
atusy wants to merge 3 commits into
tidyverse:masterfrom
atusy:alpha
Closed

atusy wants to merge 3 commits into
tidyverse:masterfrom
atusy:alpha

Conversation

@atusy

@atusy atusy commented Aug 15, 2019

Copy link
Copy Markdown
Contributor

This PR is related to #1371

In the discussion in #1371, a change was attempted to apply alpha both on colour and fill.
In this PR, we can select alpha to be applied on fill (default), colour, or both.
The default selection is fill and thus is safe in terms of backward compatibility.

Currently, alpha_to is implemented for geom_polygon, geom_rect, and geom_ribbon.
It works in some more (e.g., geom_density), but not all (geom_bar).
So I need some more to work on.

Before I update other functions, I'd like to ask @hadley if you like my PR.

Examples

Varying alpha_to by fill, colour, and both

library(ggplot2)
d <- data.frame(xmin = 0, xmax = 1, ymin = 0:4, ymax = 1:5)
g <- ggplot(d) +
  aes(
    xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax,
    colour = ymin, alpha = ymin
  )
g + geom_rect(size = 3) # alpha_to = "fill"

g + geom_rect(size = 3, alpha_to = "colour")

g + geom_rect(size = 3, alpha_to = "both")

Created on 2019-08-15 by the reprex package (v0.3.0)

@clauswilke

Copy link
Copy Markdown
Member

I feel certain this is the wrong API. It's better (and easier to implement!) to set up a hierarchy of aesthetics. This is done e.g. in ggridges, where point_alpha can be used to override the application of alpha specifically to points.

library(ggplot2)
library(ggridges)

# without `point_alpha`
ggplot(iris, aes(x=Sepal.Length, y=Species)) +
  geom_density_ridges(
    jittered_points = TRUE,
    position = position_points_jitter(width = 0.05, height = 0),
    point_shape = '|', point_size = 3, alpha = 0.4,
  )
#> Picking joint bandwidth of 0.181

# with `point_alpha`
ggplot(iris, aes(x=Sepal.Length, y=Species)) +
  geom_density_ridges(
    jittered_points = TRUE,
    position = position_points_jitter(width = 0.05, height = 0),
    point_shape = '|', point_size = 3, point_alpha = 1, alpha = 0.4,
  )
#> Picking joint bandwidth of 0.181

Created on 2019-08-15 by the reprex package (v0.3.0)

Whether we want this in ggplot2 proper is a separate question though. I don't see a strong need. For all geoms provided by ggplot2 one can always achieve the same effect by using two separate geoms (e.g., geom_area() and geom_line()).

Another relevant discussion: #1523

@atusy

atusy commented Aug 16, 2019 •

Copy link
Copy Markdown
Contributor Author

better (and easier to implement!) to set up a hierarchy of aesthetics

Sound like you are right.
Your idea is much more flexible because alpha of color and fill can be based on different variables.

achieve the same effect by using two separate geoms

For geom_density, its true that geom_area() and geom_line() works.
What about for geom_rect?
Is there any geom which just draws border of rectangles with a support of alpha?
Maybe geom_path is the one, but is not as easy as using geom_rect.

@yutannihilation

Copy link
Copy Markdown
Member

Maybe geom_path is the one, but is not as easy as using geom_rect.

But, it's still possible. I agree it would be nice to have some option to make alpha affect on lines, but I don't see a good reason to do so, considering the difficulty of making changes on these fundamental geoms and maintaining them. As we saw on #1371, I think it's too late to change....

@atusy

atusy commented Aug 16, 2019

Copy link
Copy Markdown
Contributor Author

I see...

@atusy atusy closed this Aug 16, 2019
@yutannihilation

Copy link
Copy Markdown
Member

You can create an extension package, though.

@lock

lock Bot commented Feb 12, 2020

Copy link
Copy Markdown

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock Bot locked and limited conversation to collaborators Feb 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants