geom_rect and alpha - does this work with hard coded values?
Asked Answered
D

4

78

Same title, completely reworded the question though.

Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame it works as expected?

mtcars$cyl <- factor(mtcars$cyl)
mtcars$am <- factor(mtcars$am)

ggplot(mtcars) +
    geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
    geom_rect(data=data.frame(xmin=100, xmax=200, ymin=0, ymax=Inf), aes(xmin=xmin, xmax=xmax, ymin=ymin,ymax=ymax), fill="red", alpha=0.2) 

ggplot(mtcars) +
    geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
    geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2) 
Doublure answered 8/7, 2013 at 7:48 Comment(0)
D
166

This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples).

Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...

ggplot() + 
  geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
  geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")

... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:

ggplot(mtcars) + 
  geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
  annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red") 

The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.

Both methods return identical plots:

enter image description here

Diaspora answered 11/7, 2013 at 3:51 Comment(5)
Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.Doublure
This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.Alti
@Stuart, the geom_rect() call will not produce the desired alpha value because, as @Diaspora explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())All
The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?Ministrant
I couldn't get a combination of ggplot(), geom_rect(aes()), geom_histogram(aes()), and facet_wrap() to work (it was working up to facet_wrap, but alpha fails when I get to facet_wrap OR facet_wrap doesn't work because it doesn't have access to the dataframe). But annotate did the trick! (if you ever figure out how to make geom_rect with alpha and facet_wrap work together, please let me know)Spiegleman
B
28

Another workaround is to give geom_rect() a single row data object to ensure only one rectangle is drawn:

ggplot(mtcars) +
  geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
  geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)

enter image description here

Bouncing answered 21/12, 2013 at 15:34 Comment(0)
C
3
ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
  geom_rect(alpha=.2) + 
  geom_rect(data=data.frame(xmin=3, xmax=6, ymin=3, ymax=5), 
            aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax), 
            fill="green", alpha=.2)
Conformist answered 8/7, 2013 at 8:35 Comment(1)
Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.Filagree
H
2

For those attempting to specify fill and alpha values in geom_rect while faceting, I found I had to specify the a row in the dataframe that accorded with each facet to make the rectangle appear in that facet. For a three faceted point plot and two rectangles to extend across the 3 facets:

plotpnts = ggplot(SHDates, aes(x=Order, y=NewMean))
Fig2 = plotpnts + 

# Rectangles for time periods   
geom_rect(data=SHDates[1,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red",    alpha=0.4) +
geom_rect(data=SHDates[11,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
geom_rect(data=SHDates[22,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
geom_rect(data=SHDates[1,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) + 
geom_rect(data=SHDates[11,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) + 
geom_rect(data=SHDates[22,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) + 
Heartwood answered 11/2, 2019 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.