Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_rect and NULL

Tags:

null

r

ggplot2

I've been looking at the geom_rect example in section 5.10 of the ggplot2 book and don't understand the purpose of the NULL's in the aes function. For example, using the mpg data:

g = ggplot(data=mpg, aes(x=displ, y=hwy)) + geom_point()

#Produces a plot with a transparent filled region
g + geom_rect(aes(NULL, NULL), alpha=0.1,xmin=5, xmax=7, ymin=10,
ymax=45, fill="blue")

#Solid filled region (v0.9) or nothing in v0.8
g + geom_rect(alpha=0.1,xmin=5, xmax=7, ymin=10, ymax=45, fill="blue")

My understanding is that the NULL's are resetting the x & y mapping, but I don't see why this should affect the transparency.

like image 873
csgillespie Avatar asked Nov 13 '22 12:11

csgillespie


1 Answers

I've just made Hadley's comment a community wiki answer

The reason why the specification matters is that multiple transparent rectangles stacked on top of each other will look solid - because you haven't reset the data, you get one rectangle for each row in mtcars. You probably want to use annotate instead.

like image 113
csgillespie Avatar answered Nov 16 '22 18:11

csgillespie