Rotated axis labels in R plots

[This article was first published on me nugget, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


It’s somehow amazing to me that the option for slanted or rotated axes labels is not an option within the basic plot() or axis() functions in R.  The advantage is mainly in saving plot area space when long labels are needed (rather than as a means of preventing excessive head tilting). The topic is briefly covered in this FAQ, and the solution is a bit tricky, especially for a new R user. Below is an example of this procedure.

To reproduce example:

# Example data
tmin <- as.Date("2000-01-01")
tmax <- as.Date("2001-01-01")
tlab <- seq(tmin, tmax, by="month")
lab <- format(tlab,format="%Y-%b")
set.seed(111)
x <- seq(tmin, tmax, , 100)
y <- cumsum(rnorm(100))
 
# Plot
png("plot_w_rotated_axis_labels.png", height=3, width=6, units="in", res=400)
op <- par(mar=c(6,4,1,1))
plot(x, y, t="l", xaxt="n", xlab="")
axis(1, at=tlab, labels=FALSE)
text(x=tlab, y=par()$usr[3]-0.1*(par()$usr[4]-par()$usr[3]),
labels=lab, srt=45, adj=1, xpd=TRUE)
par(op)
dev.off()






To leave a comment for the author, please follow the link and comment on their blog: me nugget.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)