-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
I have struggled to use transparent colors with geom_rect
and geom_ribbon
. I haven't been able to find a consistent way to get the legend colors match the fill color in the plot. I haven't measured the inconsistency though, it's based on my visual impression, but let me know if you notice the problem too. My workaround has been to tweak the value of alpha with override.aes
inside guide_legend
, but it's not ideal. Is there a better workaround?
In the plot below, something between guides(fill = guide_legend(override.aes = list(alpha = 0.1)))
and guides(fill = guide_legend(override.aes = list(alpha = 0.2)))
seems to give a better result. Am I seeing this properly?
Note that below I have used adjustcolor()
function, but the same problem is the same if I use "#FF000080"
and "#0000FF80"
, which are obtained from blue and red by setting an alpha to 0.5. I checked online that the hex values are correct.
library(ggplot2)
ggplot() + theme_bw() +
geom_ribbon(aes(x = 0:5, ymin = 5, ymax = 0, fill = "red")) +
geom_ribbon(aes(x = 5:10, ymin = 5, ymax = 10, fill = "red")) +
geom_ribbon(aes(x = 0:5, ymin = 5, ymax = 10, fill = "blue")) +
geom_ribbon(aes(x = 5:10, ymin = 0, ymax = 5, fill = "blue")) +
scale_fill_manual(name = NULL,
values = adjustcolor(c("red", "blue"), alpha.f = 0.5)) +
guides(fill = guide_legend(override.aes = list(alpha = 0.5)))
Created on 2018-04-21 by the reprex package (v0.2.0).