[R] Putting subscript in facet_grid label of ggplot

David Winsemius dwinsemius at comcast.net
Fri Feb 12 08:08:07 CET 2016


> On Feb 11, 2016, at 7:41 PM, Harun Rashid via R-help <r-help at r-project.org> wrote:
> 
> Hello,
> I am having trouble with putting subscript in facet_grid label. Here is 
> an example of the work I have been trying to do.
> 
>> 
> df<-data.frame(species=gl(2,10,labels=c('sp1','sp2')),age=sample(3:12,40,replace=T),variable=gl(2,20,labels=c('N1P1 
> var','N2P1 var')),value=rnorm(40))
> 
>> 
> test.plot<-ggplot(data=df,aes(x=age,y=value))+geom_point()+facet_grid(variable~species)

The default evaluation of labels in facet_grid is just as character. To get an expression (successfully) parsed you need to change the `labeller` argument and you need a valid expression (which at the moment you do not provide.)

See ?plotmath and ?facet_grid for worked examples:

 Try this instead:

df<-data.frame(species=gl(2,10,
   labels=c('sp1','sp2')),
   age=sample(3:12,40,replace=T),
   variable=gl(2,20,labels=c('N[1]*P[1]~var','N[2]*P[1]~var')), 
   value=rnorm(40) )

test.plot <-ggplot(data=df,aes(x=age,y=value)) + geom_point() + facet_grid(variable~species, labeller=label_parsed)

test.plot

-- 
David.

> 
> Now I want to make by vertical facet label as 'N[1]P[1] var' and so on, 
> where the numbers in the squared bracket means subscript.
> Thanks in advance for your  help.
> Regards,
> Harun
> -- 
> 
> <mailto:mhrashidbau at yahoo.com>
> <mailto:mhrashidbau at yahoo.com>
> 
> 	[[alternative HTML version deleted]]


David Winsemius
Alameda, CA, USA



More information about the R-help mailing list