Rotating x axis labels in R for barplot

RGraphPlotBar ChartAxis Labels

R Problem Overview


I am trying to get the x axis labels to be rotated 45 degrees on a barplot with no luck. This is the code I have below:

barplot(((data1[,1] - average)/average) * 100,
        srt       = 45,
        adj       = 1,
        xpd       = TRUE,
        names.arg = data1[,2],
        col       = c("#3CA0D0"),
        main      = "Best Lift Time to Vertical Drop Ratios of North American Resorts",
        ylab      = "Normalized Difference",
        yaxt      = 'n',
        cex.names = 0.65,
        cex.lab   = 0.65)

R Solutions


Solution 1 - R

use optional parameter las=2 .

barplot(mytable,main="Car makes",ylab="Freqency",xlab="make",las=2)

enter image description here

Solution 2 - R

EDITED ANSWER PER DAVID'S RESPONSE:

Here's a kind of hackish way. I'm guessing there's an easier way. But you could suppress the bar labels and the plot text of the labels by saving the bar positions from barplot and do a little tweaking up and down. Here's an example with the mtcars data set:

x <- barplot(table(mtcars$cyl), xaxt="n")
labs <- paste(names(table(mtcars$cyl)), "cylinders")
text(cex=1, x=x-.25, y=-1.25, labs, xpd=TRUE, srt=45)

Solution 3 - R

Rotate the x axis labels with angle equal or smaller than 90 degrees using base graphics. Code adapted from the R FAQ:

par(mar = c(7, 4, 2, 2) + 0.2) #add room for the rotated labels

#use mtcars dataset to produce a barplot with qsec colum information
mtcars = mtcars[with(mtcars, order(-qsec)), ] #order mtcars data set by column "qsec"

end_point = 0.5 + nrow(mtcars) + nrow(mtcars) - 1 #this is the line which does the trick (together with barplot "space = 1" parameter)

barplot(mtcars$qsec, col = "grey50", 
        main = "",
        ylab = "mtcars - qsec", ylim = c(0,5 + max(mtcars$qsec)),
        xlab = "",
        space = 1)
#rotate 60 degrees (srt = 60)
text(seq(1.5, end_point, by = 2), par("usr")[3]-0.25, 
     srt = 60, adj = 1, xpd = TRUE,
     labels = paste(rownames(mtcars)), cex = 0.65)

enter image description here

Solution 4 - R

You can simply pass your data frame into the following function:

rotate_x <- function(data, column_to_plot, labels_vec, rot_angle) {
    plt <- barplot(data[[column_to_plot]], col='steelblue', xaxt="n")
    text(plt, par("usr")[3], labels = labels_vec, srt = rot_angle, adj = c(1.1,1.1), xpd = TRUE, cex=0.6) 
}

Usage:

rotate_x(mtcars, 'mpg', row.names(mtcars), 45)

enter image description here

You can change the rotation angle of the labels as needed.

Solution 5 - R

You may use

par(las=2) # make label text perpendicular to axis

It is written here: http://www.statmethods.net/graphs/bar.html

Solution 6 - R

You can use ggplot2 to rotate the x-axis label adding an additional layer

theme(axis.text.x = element_text(angle = 90, hjust = 1))

Solution 7 - R

In the documentation of Bar Plots we can read about the additional parameters (...) which can be passed to the function call:

...	   arguments to be passed to/from other methods. For the default method these can 
       include further arguments (such as axes, asp and main) and graphical 
       parameters (see par) which are passed to plot.window(), title() and axis.

In the documentation of graphical parameters (documentation of par) we can see:

las
    numeric in {0,1,2,3}; the style of axis labels.

    0:
      always parallel to the axis [default],

    1:
      always horizontal,

    2:
      always perpendicular to the axis,

    3:
      always vertical.

    Also supported by mtext. Note that string/character rotation via argument srt to par does not affect the axis labels.

That is why passing las=2 makes the labels perpendicular, although not at 45°.

Solution 8 - R

Andre Silva's answer works great for me, with one caveat in the "barplot" line:

barplot(mtcars$qsec, col="grey50", 
    main="",
    ylab="mtcars - qsec", ylim=c(0,5+max(mtcars$qsec)),
    xlab = "",
    xaxt = "n", 
    space=1)

Notice the "xaxt" argument. Without it, the labels are drawn twice, the first time without the 60 degree rotation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - RMichael ZView Answer on Stackoverflow
Solution 2 - RTyler RinkerView Answer on Stackoverflow
Solution 3 - RAndre SilvaView Answer on Stackoverflow
Solution 4 - RCyberneticView Answer on Stackoverflow
Solution 5 - RvladView Answer on Stackoverflow
Solution 6 - Ruser5947894View Answer on Stackoverflow
Solution 7 - Rbanan3'14View Answer on Stackoverflow
Solution 8 - RAlvaro GonzalezView Answer on Stackoverflow