[R] Barplot2 Title

Marc Schwartz MSchwartz at mn.rr.com
Tue May 31 14:49:34 CEST 2005


On Tue, 2005-05-31 at 10:22 +0200, Navarre Sabine wrote:
> Hello,
> I would like to know if it's possible to modify the name of groups of
> bar because on my barplot2, I have  5 groups of bars and one of them
> is called "User Contributes" and when I save the plot "User
> contributes" is to big so I don't have it on my plot! Is it pssible to
> put the name vertically!
>  
> Thanks!
> 
> Sabine

Sabine,

You can rotate the bar labels, by using something like:

par(mar = c(10, 4, 4, 2))
barplot2(1:5, names.arg = paste("Long Bar Label", 1:5), las = 2)

Note that I first increase the margin area for the x axis by setting par
("mar"). This provides sufficient room for the vertical labels. Setting
par("las") results in rotated labels on both axes.

See ?par for more information. 

Also see R FAQ 7.27 on creating rotated axis labels for an example of
creating labels that are rotated 45 degrees.


Another option is to have the long labels wrap to multiple lines:

bar.names <- paste("Long Bar Label", 1:5)

> bar.names
[1] "Long Bar Label 1" "Long Bar Label 2" "Long Bar Label 3"
[4] "Long Bar Label 4" "Long Bar Label 5"

# Here I use strwrap() to split the longer names into groups of
# around 10 characters each and then paste() them together using
# a newline character
short.names <- sapply(bar.names, 
                       function(x) paste(strwrap(x, 10), 
                                         collapse = "\n"), 
                       USE.NAMES = FALSE) 

> short.names
[1] "Long Bar\nLabel 1" "Long Bar\nLabel 2" "Long Bar\nLabel 3"
[4] "Long Bar\nLabel 4" "Long Bar\nLabel 5"

# Now do the barplot with the wrapped labels
barplot2(1:5, names.arg = short.names)


See ?strwrap for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list