[R] Sub- or superscript in factorial variable - possible?

David Winsemius dwinsemius at comcast.net
Mon Sep 17 01:02:48 CEST 2012


On Sep 16, 2012, at 2:58 PM, Peter Ehlers wrote:

> On 2012-09-16 08:32, David Winsemius wrote:
>> 
>> On Sep 16, 2012, at 4:40 AM, peter dalgaard wrote:
>> 
>>> 
>>> On Sep 16, 2012, at 07:48 , David Winsemius wrote:
>>> 
>>>> 
>>>> On Sep 15, 2012, at 7:15 PM, mcg wrote:
>>>> 
>>>>> Hello R-users,
>>>>> 
>>>>> I would like to use subscript in chemical formulas for the different treatments in a boxplot.
>>>>> Fot title, xlab and ylab sub- and superscript is no problem, but for the different treatments of the following example I cannot get subscript.
>>>>> 
>>>>> Example:
>>>>> weight <-  c(6,5,7,2,7,3,9,4,2,7,8,9,2,3,4,5)
>>>>> treatments <- as.factor(rep(c('Control', 'P2O5','K2SO4','CaSO4'),4))
>>>>> data <- data.frame(treatments,weight)
>>>>> boxplot(data$weight~data$treatments)
>>>>> 
>>>>> If I apply expression(P[2]...) I get  "unimplemented type 'expression' in 'HashTableSetup' ".
>>>>> If there is a solution for this in base graphics or ggplot please let me know.
>>>>> 
>>>> 
>>>> ?plotmath
>>>> boxplot(data$weight~data$treatments, xaxt="n")
>>>> axis(1, 1:4, labels=expression(Control, P[2]*O[5], K[2]*SO[4], CaSO[4]) )
>>>> 
>>>> I will admit that the need for the "*"'s was not apparent to me until I used the initial example as a starting point and made incremental changes until I gotsuccess. So I am not suggesting that RTM should have been enough.
>>> 
>>> Just remember that plotmath is designed to handle math expressions like alpha+beta*x and the logic should follow. For the same reason, although it makes little or no visual difference, you really should say
>> 
>> What I did not remember was that there had been prior rhelp questions about how to create a proper prefixed-superscript such as might be use to represent different isotopes of the same element and and solution had been to use  a postfixed subscript on a phantom() expression.
>> 
>> plot(1,1, xlab=expression(phantom()^32m*K) ) attempted but not successful.
>> 
>>> plot(1,1, xlab=expression(phantom()^32*K) )
>>> plot(1,1, xlab=expression(phantom()^32m*K) )
>> Error: unexpected symbol in "plot(1,1, xlab=expression(phantom()^32m"
>>> plot(1,1, xlab=expression(phantom()^"32m"*K) )  # succeeds
> 
> I think what PD was trying to say is that a preferred solution would be:
> 
> plot(1,1, xlab=expression(phantom()^{32*m}*K) )

I wasn't, so this adds further to my understanding. I had not understood the need for the use of curley-braces for grouping plotmath expressions from PD's comment, but do now see the "{items}" construct upon searching on the plotmath help page. 

I suppose you could argue that the framework of :

<symbol> {<open-operator><symbol><close-operator>} {unary-operator} | {<unary-operator><symbol>}  

... might be hinted at by PD's comment, but I was not getting it from the hint.  I guess I'm still too concrete in my thinking, although most of my friends and acquaintances might say the opposite. I do appreciate all of your help over the years to fill in the gaps in my knowledge. I have learned a great deal from each of you.

> 
> Peter Ehlers
> 
>> 
>> There is some sort of parsing that splits the numeric from the alpha characters even with no spaces intervening, so you need to "protect" the 32m with quotes to get error-free interpretation.
>> 
>> 
>>> 
>>> labels=expression(Control, P[2]*O[5], K[2]*S*O[4], Ca*S*O[4])
>>> 
>>> (Plotmath as of now doesn't actually do anything about kerning and such, but TeX afficionados will know that $different$ is quite different from \textit{different}, the former not being a word but identical to $dif^2e^2rnt$)
>>> 
>>> --
>>> Peter Dalgaard, Professor,
>>> Center for Statistics, Copenhagen Business School
>>> 
>> 
>> 
>> David Winsemius, MD
>> Alameda, CA, USA
>> 
>> ______________________________________________
>> 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.
>> 
> 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list