Quantcast

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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Rainer M Krug-6
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?

Thanks

Rainer

--
Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Plant Conservation Unit
Department of Botany
University of Cape Town
Rondebosch 7701
South Africa

Tel: +27 - (0)21 650 5776 (w)
Fax: +27 - (0)86 516 2782
Fax: +27 - (0)21 650 2440 (w)
Cell: +27 - (0)83 9479 042

Skype:       RMkrug
Google Chat: [hidden email]
email:     [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Putting frame around single panels in ggplot 2 and facet_grid()

hadley wickham
On Jan 27, 2008 4:23 AM, Rainer M Krug <[hidden email]> 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/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Putting frame around single panels in ggplot 2 and facet_grid()

Felipe Carrillo
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/
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Loading...