我们需要在底部设置更宽的边距。然后使用mtext()添加x轴标签。
# set the margins
par(mar = c(10, 4.1, 4.1, 2.1))
plot(myDf$mpg, axes=F, xlab="", ylab="MPG")
axis(2)
axis(1, at = seq_along(myDf$mpg), labels = myDf$Row.Names, las = 2, cex.axis = 0.70)
box()
# add xlabel, "line" arguement controls vertical position
mtext("Car", side = 1, line = 6)
使用ggplot:
library(ggplot2)
#my data
myDf <- cbind(car = rownames(mtcars), mtcars)
#to keep ordering as in data, set custom levels (default is alphabetical)
myDf$car <- factor(myDf$car, levels = myDf$car)
#plot
ggplot(myDf, aes(x = car, y = mpg)) +
geom_point() +
# rotate car names by 90 degrees, adjust vertically and horizontally
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))