X-axis rotation in R for hatching

I am trying to get the x-axis labels to rotate 45 degrees on a tablet with no luck. This is the code that 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) 
+57
r graph plot axis-labels bar-chart
Apr 23 '12 at 18:53
source share
5 answers

SEPARATE RESPONSE TO AN ANCIENT RESPONSE:

Here is some hacker way. I guess there is an easier way. But you can suppress the panel labels and label text, keeping the bar position from the barplot and adjusting up and down a bit. Here is an example with the mtcars dataset:

 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) 
+44
Apr 23 '12 at 19:08
source share
β€” -

use the optional parameter las = 2.

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

enter image description here

+142
Jan 11 '15 at 13:39 on
source share

If you want to rotate the x-axis labels with an angle equal to or less than 90, try the following approach:

It uses the argument barplot space=1 so that the column width is equal to the spacing of the column spacing.

In this way, it was possible to adapt the code presented at frequently asked frequencies , which was determined by @BenBarnes under Tyler Rinker's answer.

 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" (source: http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r) 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

+20
Feb 24 '14 at 3:54
source share

you can use

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

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

+5
Aug 19 '14 at 12:03 on
source share

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) 

Pay attention to the argument "xaxt". Without it, shortcuts are drawn twice, the first time without rotating 60 degrees.

+1
May 3, '16 at 22:14
source share



All Articles