Disabled alpha ggplot

I am trying to build a translucent rectangle of the same color as the background above the density curve, so it creates a lighter vertical area of ​​the latter (hacking to visualize a range of interests such as rush hours). As you can see, alpha fails. I wonder if anyone can get the following code?

I know that there are other reports of ggplot alpha channel problems ( for example ), but no one seems to be resolving this, and its not clear what the current situation is version-wise.

Thank you in advance:)

enter image description here

d <- data.frame(rnorm(100, mean = 0, sd = 100)); names(d) <- 'data' ggplot(d) + geom_density(aes(x=data),col=NA, fill='grey30') + opts(panel.background=NULL) + geom_rect(aes(xmin=-30, xmax=30, ymin=0, ymax=0.005), fill='white',alpha=0.2) ggplot(d) + geom_density(aes(x=data),col=NA, fill='grey30') + opts(panel.background=NULL) + geom_rect(aes(xmin=-30, xmax=30, ymin=0, ymax=0.005), fill='#FFFFFF40') 
+4
source share
1 answer

It looks like you have chosen alpha too low, try for example 1/256 , which represents the maximum amount of transparency:

 ggplot(d) + geom_density(aes(x = data), col = NA, fill = 'grey30') + theme(panel.background = NULL) + geom_rect(aes(xmin = -30, xmax = 30, ymin = 0, ymax = 0.005), fill = 'white', alpha = 1/256) 

enter image description here

This is an unexpected decision for me too, because taking alpha = I(1/d) means that d is the number of points that need to be crossed out to get a solid color, so we expect another result with 1/256 . As you said, this is a fairly common geom_rect related geom_rect .

+8
source

Source: https://habr.com/ru/post/1433239/


All Articles