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

Felipe Carrillo mazatlanmexico at yahoo.com
Sun Jan 27 21:26:47 CET 2008


What about using geom_vline and geom_hline to draw
borders. I just couldn't find the way to set the
intercept max value for geom_vline and hardcoded it to
5.5

d<- ggplot(mtcars, aes(x=mpg, y=wt)) + geom_point()  +
facet_grid(. ~ cyl)+
 scale_y_continuous(limits=c(0,NA))+
scale_x_continuous(limits=c(0,NA))
   mtcars2<-data.frame((mtcars),intercept=0)
 dd<- d +
geom_hline(data=mtcars2,colour="red",size=3)+
geom_vline(data=mtcars2,colour="red",size=3)
mtcars3<-data.frame((mtcars),intercept=5.5)
 ddd <- dd +
geom_hline(data=mtcars3,colour="red",size=3)
 ddd


> > 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/
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
> 


Felipe D. Carrillo
  Fishery Biologist
  US Fish & Wildlife Service
  California, USA



      ____________________________________________________________________________________
Be a better friend, newshound, and



More information about the R-help mailing list