可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)
回答1:
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
回答2:
use optional parameter las=2 .
barplot(mytable,main="Car makes",ylab="Freqency",xlab="make",las=2)
回答3:
If you want to rotate the x axis labels with angle equal or smaller than 90, try the following approach:
It uses the barplot's argument space=1
to make the columns' width equal to the columns' interval space.
This way, it was possible to adapt the code provided in the R FAQ that was pinpointed by @BenBarnes under the 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)
回答4:
You may use
par(las=2) # make label text perpendicular to axis
It is written here: http://www.statmethods.net/graphs/bar.html
回答5:
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.
回答6:
You can simply pass your data frame into the following function:
rotate_x
Usage:
rotate_x(mtcars, 'mpg', row.names(mtcars), 45)

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