Re: [R] Formatting names.arg

From: Marc Schwartz <marc_schwartz_at_me.com>
Date: Mon, 23 May 2011 16:48:46 -0500

On May 23, 2011, at 4:31 PM, David Winsemius wrote:

> 
> On May 23, 2011, at 4:32 PM, Marc Schwartz wrote:
> 
>> On May 23, 2011, at 3:13 PM, Adrienne Keller wrote:
>> 

>>> I am making a barplot using barplot2 from gplots and would like to
>>> format the names of my categorical variables (tree species) on the x-
>>> axis so that the genus name is above the species name (to save room).
>>> My code so far is:
>>>
>>> speciesnames<-c("Dialium guianensis", "Inga alba", "Tachigali
>>> versicolor", "Brosimum utile", "Caryocar costaricense", "Castilla
>>> tunu", "Otoba novagranatensis", "Pourouma bicolor", "Socratea
>>> exorrhiza")
>>> barplot2(meanapAprilactivity, names.arg=speciesnames, col=columncolor,

>>> xlab="Species", ylab="Soil acid phosphatase activity (nmol/h/g)",
>>> plot.ci=T, ci.l=apAprilminusordered, ci.u=apAprilplusordered,
>>> cex.lab=1.5)
>>>
>>> For example, I want 'Dialium' to be located above 'guianensis' on the
>>> x-axis. Is there a way to do this?
>>>
>>> Thanks,
>>>
>>> Adrienne Keller
>> 
>> 
>> Hi,
>> 
>> Just replace the space between the names with a newline character ('\n'):
>> 
>> barplot(1:9, names.arg = gsub(" ", "\\\n", speciesnames),
>>         cex.names = 0.5)
>> 
>> That will put the labels on two lines.
>> 
>> See ?gsub and note that I used 3 '\' preceding the 'n'.
> 
> Except that tripling of "\" is not necessary with the second argument to gsub or sub, only with the first pattern argument.
> 
> speciesnames<-c("Dialium guianensis", "Inga alba", "Tachigali versicolor", "Brosimum utile", "Caryocar costaricense", "Castilla tunu", "Otoba novagranatensis", "Pourouma bicolor", "Socratea exorrhiza")
> 
> sub(" ", "\n", speciesnames)
> 
> [1] "Dialium\nguianensis"    "Inga\nalba"
> [3] "Tachigali\nversicolor"  "Brosimum\nutile"
> [5] "Caryocar\ncostaricense" "Castilla\ntunu"
> [7] "Otoba\nnovagranatensis" "Pourouma\nbicolor"
> [9] "Socratea\nexorrhiza"
> 


Thanks for catching that David, you are correct. That's what I get for answering the post in the middle of formatting R output for LaTeX and having tunnel vision on the backslashes...

:-)

Regards,

Marc



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. Received on Mon 23 May 2011 - 21:53:17 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 23 May 2011 - 22:10:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive