[R] Aligning Diagonally Oriented Labels Under Bar Chart

Jason Rupert jasonkrupert at yahoo.com
Wed Dec 2 15:29:08 CET 2009



Peter, 

Thank you for your reply. 

That got me a lot closer.  Now I just need to move the text down a bit and it will be perfect.  

Thank you for your suggestion of creating a reference to barplot and passing that as an argument into "text".  

That seems to have made the difference.  

Here is what I've ended up with so far:

#  Reference the following URL for information about rotating axis labels:
#  http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f
# mar A numerical vector of the form c(bottom, left, top, right) which gives the number of lines 
# of margin to be specified on the four sides of the plot. The default is c(5, 4, 4, 2) + 0.1. 
par(mar = c(7, 4, 4, 2) + 0.1)
barplot_reference<-barplot(WorldPhones[1,], 
                        ylim=c(0, 50000),
                        axes=FALSE, ann=FALSE, 
                        col=terrain.colors(length(WorldPhones[1,])), 
                        xaxt = "n",  xlab = "")
# xpd  A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, 
# all plotting is clipped to the figure region, and if NA, all plotting is clipped to 
# the device region.
                        
text(barplot_reference, par("usr")[3] - 0.25, srt = 45, adj = 1,
     labels = as.character(colnames(WorldPhones)), xpd = TRUE)

#axis(1, at = barplot_reference, labels = as.character(colnames(WorldPhones)), srt = 45, adj = 1) 
box()



Any suggestions on adjusting the text down a bit is also greatly appreciated. 

Thank you again. 



 


----- Original Message ----
From: Peter Alspach <Peter.Alspach at plantandfood.co.nz>
To: Jason Rupert <jasonkrupert at yahoo.com>; R-help at r-project.org
Sent: Tue, December 1, 2009 5:29:17 PM
Subject: RE: [R] Aligning Diagonally Oriented Labels Under Bar Chart

Tena koe Jason

Is this an example of what you want?

temp <- barplot(3:17)
text(temp, rep(-0.5, length(3:17)), LETTERS[3:17], srt=45, adj=1)

HTH ....

Peter Alspach 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Jason Rupert
> Sent: Wednesday, 2 December 2009 12:16 p.m.
> To: R-help at r-project.org
> Subject: [R] Aligning Diagonally Oriented Labels Under Bar Chart
> 
> I searched the forms (i.e., R Search) and come up with the 
> following suggested link:
>  
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-
> rotated-axis-labels_003f
> 
> I tried to implement what I believe was being implied by that 
> URL and came up with the below:
> 
> barplot(WorldPhones[1,],
>         ylim=c(0, 50000),
>         axes=FALSE, ann=FALSE,
>         col=terrain.colors(length(WorldPhones[1,])),
>         xaxt = "n",  xlab = "")
> 
> text(1:dim(WorldPhones)[1], par("usr")[3] - 0.1, srt = 45, adj = 1,
>      labels = as.character(colnames(WorldPhones)), xpd = TRUE)
> 
> box()
> 
> 
> Unfortunately the labels are not aligned with the appropriate 
> bars.  I believe I am missing something small in order to 
> have the text properly aligned underneath the appropriate bars.   
> 
> Are there any suggestions for correcting this mis-alignment?  
> 
> Thank you again for any additional feedback, links, hints or 
> insights.  
> 
> 
> 
> 
> ______________________________________________
> 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.
> 








More information about the R-help mailing list