X-Axis Labels on a 45-Degree Angle using R

I’ve been trying to find a simpler bit of R code that will allow axis labels to be written in at an angle, and thanks to my obsessive scanning of the R-help mailing list I found a nice example (all credit to Uwe Ligges and Marc Schwartz for their approach). I’ve made a few cosmetic tweaks in moving the labels off of the x-axis and changing the labels.

The following code will produce the figure below:

labels <- c(“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”)
labels <- month.name[1:12]
mp <- barplot(1:12, axes = FALSE, axisnames = FALSE)
text(mp, par(“usr”)[3], labels = labels, srt = 45, adj = c(1.1,1.1), xpd = TRUE, cex=.9)
axis(2)

Histogram Example
Configure to your heart’s content. I know I will.

Updated code: R has a few built in constants and I think it makes things much simpler to take advantage of them (letters, LETTERS, month.name, month.abb and pi).

Advertisements

17 responses to “X-Axis Labels on a 45-Degree Angle using R

  1. Nice method used to create the month labels. I needed the ‘, srt = 45’ bit. Cheers!

  2. Brilliant! Thanks a lot. Nothing worse than R hiding important labels when they don’t fit and the documentation being difficult to follow.

  3. Pingback: X-Axis Labels on a 45-Degree Angle using R (PART II) « Justin Leinaweaver

  4. dangerouspenguin

    Thanks! Worked a charm and will reblog.

  5. Pingback: On An Angle « Over Exposed

  6. Fantastic example. Thanks very much.
    After much playing around, thanks to your example I came up with the following to produce text going the other way. It involves only three changes, but I think it seems rather like a hack than a solution. Any suggestions?

    labels <- month.name[1:12]
    mp <- barplot(1:12, axes = FALSE, axisnames = FALSE)
    text(mp, par("usr")[3]-.5, labels = labels, srt = 315, adj = c(0,0), xpd = TRUE, cex=.9)
    axis(2)

    sessionInfo() =
    R version 2.13.1 (2011-07-08)
    Platform: x86_64-pc-mingw32/x64 (64-bit)

    Thanks again,
    Cheers,
    Davy

  7. Hi Davy,

    Yeah, all of this feels somehow unsatisfying. A bit too klugey. As you’ve shown, par(“usr”) defines a vector (of length 4) to specify the plotting region. The third element refers to ‘y1’ so by subtracting from it you can adjust the label’s placement.

    Other than switching to ggplot (and learning that whole other set of commands) I think we’re stuck with playing with ‘adj’ once the plot is otherwise finished.

  8. doesn’t work if log=”y” is set

  9. Logarithmic y-Axis works if you set 10^par(‘usr’)[3]
    Example for boxplots:
    ########
    data <- data.frame(values=c(10^seq(1:5),10^seq(4,8),10^seq(7,11)),
    groups=factor(rep(c(1,2,3), each=5)))

    boxplot(data$values ~ data$groups, xaxt='n', log='y')
    axis(1,labels=rep('',length(levels(data$groups))), at=1:length(levels(data$groups)))
    text(as.numeric(levels(data$groups)), 10^par("usr")[3], labels = levels(data$groups),
    srt = 45, adj=c(2,2), xpd = TRUE, cex=0.9)

  10. Thank you! very useful!!!
    In my plot I have added a xlab by giving more margin at the bottom (in my case I had large names):
    par(mar=c(7.5,4.1,1.1,1.1))
    and using the mtext function to write the label
    mtext(“x axis name”,1,line=6) # you can play with line…

  11. Thanks a lot for the information, could work, but I can not do it with a variable with two levels. I got as a result one label per bar. I would like to have one label by group of bars. I would appreciate if someone could help me in that. Thanks a lot.

  12. Hi Lana,

    Your best bet is probably to add the labels separately and manually using mtext() rather than trying to do it all in one command.
    You may need to use par() to expand the lower margin space so you can fit the group labels.

  13. Rob Stevenson

    This example has been helpful but I have not be able to get the labels in my example to line up underneath the bars correctly. The x distance is a bit to small. There must be some scaling parameter that I do not understand.

  14. Rob Stevenson

    lablist <- c('Atmospheric Sciences', 'Bioinformatics','Chemistry', 'Computer Programming', 'Geology', 'GIS', 'Mathematics', 'Oceanography', 'Physics', 'Statistics')

    Barplot (as.matrix(q2[,2:ncol(q2)]), col=c("darkblue","blue", "lightblue","pink", "red"), ylab ='Faculty and graduate student responses',xaxt="n", las=1 )
    text(1:10,par('usr')[3], labels = lablist, srt = 45, adj = c(1,1), xpd = TRUE, cex=.8)

  15. Justin Leinaweaver

    Hi Rob, save your barplot as an object as in my code above, then replace the “x” parameter in your text() command with that object. This should ensure that your labels are placed correctly in case your barplot isn’t actually at 1:10. Make sense?

  16. Thank you! This helped me a lot!!

  17. Spot on with this write-up, I absolutely think this web site needs a great deal more attention. I’ll probably be returning to read more, thanks for the info!|

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s