Legend showing stroke-only for alpha/transparency (geom_point)

ggplot2

#1

Recently, I wanted to use two separate colour schemes on a chart, while keeping the information in the legend. In this case there was one colour scale for a set of points, and one for a set of lines.

I came up with a workaround whereby I mapped my chosen aesthetic to fill in the points instead, and used pch = 1 (circle with outline and fill) and stroke = 0. If this seems a bit confusing, refer to the reprex below.

However, another bit of data was mapped to the points' alpha (transparency)*. This leads to the points' alphs not being present in the legend (since the legend only shows the outline of the points, which is now stroke = 0).

library(ggplot2)

ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(fill = vs, alpha = wt), 
             pch = 21,                        # Make points into filled points
             stroke = 0) +                    # Remove outline from filled points
  ggtitle("Alpha in legend with stroke = 0")

no_legend

I should also note that this is with ggplot2_2.2.1.9000 (via sessionInfo()).

My questions are:

  • Is this a bug? I.e. should I file an issue on github?
  • Is there a handier workaround to the initial issue?

* If you're thinking this is a sign there's too much going on in the chart, I would probably agree with you. Still, please bear with me.


#2

For part one:

Yeah, legend problems are a known issue right now. I don't think you need to open a new issue, though, as I think this overlaps with existing open issues (see Upvoting issues post from yesterday :+1:).

Take a look at (the link above is to all open issues re. legends right now):


#3

Thank you, Mara. I had a look at the issues on github, and as you suggest, I wasn't sure how much I'd contribute by adding a new one. It's good to get a more qualified confirmation.

Thank you for the link, I'll take a look and upvote any that seem applicable.

I'll be honest, since this is at best an edge case (and more reasonably, a hack), it seems like it'd be a very low priority fix anyway. It seems like a better general solution is to use

ggplot(data) +
  geom_point(aes(colour = var1, aes = var2), pch = 1) +
  ...