Skip to content

45 Degree labels on barplot? Help understanding code previously posted.

3 messages · Simon Kiss, David Winsemius, Jim Lemon

#
Dear colleagues,
i found a line or two of code in the help archives from Uwe Ligges about creating slanted x-labels for a barplot and it works well for my purposes (code below). However, I was hoping someone could explain to me precisely what the code is doing.  
I'm aware it's invoking the text command, and I know the first ttwo arguments to text are x and y co-ordinates.  I'm also aware that par("usr")[3] is grabbing the third element of the vector of plotting co-ordinates.  But I tried replacing par("usr")[3] with just "0" and that didn't work; all the labels got bunched up on the left.  Is it necessary to create a new object via "barplot" and then quote that in the x,y coordinates of text? 
Like I said, the code works great, but I'm trying to actually understand the rationale behind the elements so I can apply it in future.
Yours,  Simon Kiss

#Reproducible Code
mydat<-data.frame(countries=c("Canada", "Denmark", "Framce", "United Kingdom", "Germany", "Australia", "New Zealand", "Switzerland", "Belgium", "Netherlands"), stories_total=c(429, 25,
239, 99, 100, 96, 18, 21, 0, 6), avg=c(4.165048544, 6.25, 6.459459459, 0.908256881, 1.923076923, 1.103448276, 1.058823529, 1.615384615, 0, 0.107142857), steps=c(2, 2, 2, 0,1, 1, 1, 0,0,0), 
newspapers=c(103, 4, 37, 109, 52, 87, 17, 13, 10, 56))
mydat.sort1<-mydat[order(-mydat$avg), ]
myplot<-barplot(mydat.sort1$avg, col=c("black", "black", "black", "grey", "white", "grey", "grey", "white", "white", "white"), ylim=c(0,7), main="Regulatory Action On Bisphenol A By Newspaper Coverage")
col.vec=c("black", "grey", "white")
legend("topright", col=col.vec, fill=c("black", "grey", "white"), legend=c("Meaningful Ban", "Recommendations To Withdraw", "No Legislative Action"))
labels=mydat.sort1$countries
#These lines create the labels
text(myplot, par("usr")[3], labels=labels, srt=35, offset=1, adj=1, xpd=TRUE)
axis(2)
par("usr")[3]

*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 519 761 7606
#
On Dec 10, 2010, at 10:25 AM, Simon Kiss wrote:

            
More accurately the limits of the plot area in plot dimensions.
That was the "y" argument, not the rotation argument. (Which means I  
am surprised that it bunched things to the side ...  and for me it did  
nothing at all... same graphic.)  It is the srt argument that controls  
the angle.
That gives you appropriate positions for the labels in plot coordinate  
terms and the xpd argument allows these locations to be used outside  
the plot area.
What do you mean by "then quote it in the x,y,coordinates"? I don't  
see any quotes. You could of course just look at the plot area and  
supply your own locations. You would need to figure out what the  
unlabeled x-axis scale really was, but that too is documented.
#
On 12/11/2010 02:25 AM, Simon Kiss wrote:
Hi Simon,
The staxlab function will add an axis to an existing plot with either 
staggered or rotated labels. It is somewhat similar to Uwe's function. 
Looking at both the code and the examples on the help page might give 
you some idea why the function was written. It is principally to allow 
the user to add more labels than would be displayed on the default axes, 
and to decide whether staggering or rotating those labels will produce a 
better looking plot. The problem most often mentioned in adding custom 
labels to a plot produced by barplot is that the bars are centered on 
non-integer values, and so the user must get the return value of barplot 
and use that for the axis label positions.

Jim