[R] 45 Degree labels on barplot? Help understanding code previously posted.
Simon Kiss
simonjkiss at yahoo.ca
Fri Dec 10 16:25:48 CET 2010
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
More information about the R-help
mailing list