different background colours for each rectangles in facet_grid

3,682 views
Skip to first unread message

conradin bolliger

unread,
Dec 1, 2010, 5:17:37 AM12/1/10
to ggp...@googlegroups.com
Hi
 
I would like to produce different background colours for each rectangles in
facet_grid. I use as example the one hadley gives under the following

link: https://github.com/hadley/ggplot2/wiki/Faceting-Attributes 
 
Example:
plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y))+
facet_grid(type ~ type2)
plot
## strip.background (rect)
plot + opts(strip.background = theme_rect(colour = 'purple', fill = 'pink', size
= 3, linetype='dashed'))
## if I change the fill argument to e.g. fill = c("green", "pink", "orange"),
all rectangles become green, but I would like to have for each rectangel an
other colour.
 
Can somebody help me out, how to solve this problem. Thanks a lot in advance.
Conradin


Brandon Hurr

unread,
Dec 1, 2010, 9:09:40 PM12/1/10
to conradin bolliger, ggp...@googlegroups.com
I thought maybe it was because there were actually 5 rectangles, but even if you supply 5 colors or take away one dimension they are always green. 

Seems like we'll need someone who knows the innards a bit more to get a definite answer or workaround. 

B

Kohske Takahashi

unread,
Dec 1, 2010, 10:33:58 PM12/1/10
to Brandon Hurr, conradin bolliger, ggp...@googlegroups.com
hi,

perhaps it is better to edit the grid objects after plot:

plot
mapply(
function(g, co)grid.edit(g$name, gp=gpar(fill=co)),
grid.gget(gPath("strip_h","strip.background"),grep=T),
c("green","blue","yellow")
)


--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

James McCreight

unread,
Dec 1, 2010, 11:30:21 PM12/1/10
to Kohske Takahashi, Brandon Hurr, conradin bolliger, ggp...@googlegroups.com
that's some serious magic! :)

Hadley Wickham

unread,
Dec 2, 2010, 12:15:13 AM12/2/10
to Kohske Takahashi, Brandon Hurr, conradin bolliger, ggp...@googlegroups.com
If you want to change the background colour, I think it's easier to
use geom_rect with xmin and ymin = -Inf and xmax and ymax = Inf.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Houtan N

unread,
Feb 27, 2017, 6:25:15 PM2/27/17
to ggplot2, takahash...@gmail.com, bhi...@gmail.com, cobol...@yahoo.de, had...@rice.edu
Hi Hadley,

I think it would be great to pass in the aes() in a facet_wrap/facet_grid in order to fill the background labels.  Do you know if this is possible in the new version?
Reply all
Reply to author
Forward
0 new messages