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

David Winsemius dwinsemius at comcast.net
Fri Dec 10 19:41:38 CET 2010


On Dec 10, 2010, at 10:25 AM, Simon Kiss wrote:

> 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.

More accurately the limits of the plot area in plot dimensions.

>  But I tried replacing par("usr")[3] with just "0" and that didn't  
> work; all the labels got bunched up on the left.

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.

>  Is it necessary to create a new object via "barplot"

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.

> and then quote that in the x,y coordinates of text?

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.

-- 
David.

> 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
>
> ______________________________________________
> 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
West Hartford, CT



More information about the R-help mailing list