Change names of facets in Facet_grid

18 views
Skip to first unread message

Rhvt

unread,
Feb 12, 2015, 8:01:36 AM2/12/15
to ggp...@googlegroups.com
Hi all,

I've a question considering a facet_grid.
For example the two variables that are in the facet_grid are:
'vs', with values 0 and 1
'am', with values 0 and 1

When I ask for a facet_grid am~vs and enter label_both I get the following facet-axis-titles;
vs: 0
vs: 1
am: 0
am: 1

How can I change these facet-axis-titles into:
 'Car engine: V' (0) 
 'Car engine: Straight' (1)
 'Transmission: Automatic' (0)
 'Transmission: Manual' (1)

??

Ron Crump

unread,
Feb 12, 2015, 8:38:11 AM2/12/15
to ggp...@googlegroups.com
Rhvt,

> I've a question considering a facet_grid.
> For example the two variables that are in the facet_grid are:
> 'vs', with values 0 and 1
> 'am', with values 0 and 1
>
> How can I change these facet-axis-titles into:
> 'Car engine: V' (0)
> 'Car engine: Straight' (1)
> 'Transmission: Automatic' (0)
> 'Transmission: Manual' (1)

If you look at ?facet_grid you will see labeller in the arguments. You
just need to define a custom labeller function. Something like I have
done below...

dd<-data.frame(vs=rep(c(0,1),each=5),am=rep(c(0,1),times=5),x=rnorm(10),y=rnorm(10))

mylabels <- function(variable,value){ll <-
list(vs=list("0"="V","1"="Straight"),
am=list("0"="Automatic","1"="Manual"));
return(ll[[variable]][as.character(value)])}
ggplot(dd,aes(x=x,y=y))+geom_point()+facet_grid(vs~am,labeller=mylabels)

Hope this helps,
Ron.
Reply all
Reply to author
Forward
0 new messages