[R] expression() and axis labels

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Jul 16 19:22:30 CEST 2011



On 16.07.2011 19:10, David Winsemius wrote:
>
> On Jul 16, 2011, at 12:09 PM, Thomthom wrote:
>
>> Hi everyone!
>>
>> I tried to look up in the previous topics whether there was something
>> similar to my question but I think there's nothing corresponding...
>>
>> Anyway! Here is my problem:
>>
>> I have a plot (qplot()) in which I want to insert axis labels with the
>> following text:
>>
>> kg NH4+ N-equiv. * ha-1 yr-1 (well... 4 smaller and down and + as a power
>> (the same for the -1) but I am sure you understood what I meant).
>>
>> So, in my code, I typed :
>>
>> qplot(...., xlab="Amount of N-fertilizer" expression(kg NH[4]^+
>> N-equiv. *
>> ha^-1 yr^-1))
>
> I have never see a successful concatenation of regular text in quotes
> with expression(...), so my efforts were focussed on making a
> sytactically correct expression:
>
> plot(1~1, xlab=expression(Amount~of~N-fertilizer~kg~NH[4]^"+"*"*"*N-equiv.~
> ha^-1*yr^-1) )
>
>
> You need to forget about spaces as separators since they are ignored,
> and use either "~" or "*" as connectives and that also means that if you
> want "*" to appear, it needs to be quoted.
>
> plot(1~1, xlab=expression(Amount~of~N-fertilizer~kg~ NH[4]^ "+" * "*" *
> N-equiv.~
> ha^-1 * yr^-1) ) # spaces added bak to show grouping, but they are still
> ignored.

You can use spaces if you just quote your text, of course:

plot(1~1, xlab = expression("Amount of N-fertilizer kg " * NH[4]^"+" * 
"*" * " N-equiv. " * ha^-1 * yr^-1) )

Best,
Uwe


>
> I'm guessing you want the last part in parens:
>
> plot(1~1, xlab=expression(Amount~of~N-fertilizer~kg~ NH[4]^ "+" *
> group("(", list( N-equiv.~ ha^-1 * yr^-1), ")" ))
>



More information about the R-help mailing list