[R] Putting frame around single panels in ggplot 2 and facet_grid()

hadley wickham h.wickham at gmail.com
Sun Jan 27 18:51:15 CET 2008


On Jan 27, 2008 4:23 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:
> Hi
>
> I want to highlight two panels in a grid created with facet_grid() by
> putting a box around it or usiong another background colour. Is this
> possible, and if yes, how?

It's possible, but not particularly easy.  You could do something like:

# Background colours
background <- data.frame(
  cyl = c(4, 6, 8),
  fill = c(NA, alpha("red", 0.2), alpha("black", 0.3))
)

# Points defining square region for background
square <- with(mtcars, data.frame(
  x = c(min(mpg), max(mpg), max(mpg), min(mpg)),
  y = c(min(wt), min(wt), max(wt), max(wt))
))

ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_polygon(aes(x=x,y=y, fill=fill), data=merge(background, square)) +
geom_point() +
scale_fill_identity() +
facet_grid(. ~ cyl)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list