R 增加字符串标签值的轴标签空间

R 增加字符串标签值的轴标签空间,r,plot,R,Plot,我正在创建一个x轴包含字符串的绘图。我按照此图中的说明,成功创建了以下绘图: myDf <- cbind(Row.Names=rownames(mtcars), mtcars) plot(myDf$mpg, axes=F, xlab="Car", ylab="MPG") axis(2) axis(1, at=seq_along(myDf$mpg), labels=myDf$Row.Names, las=2, cex.axis=0.70) box() myDf我们需要在底部设置更大的边距。

我正在创建一个x轴包含字符串的绘图。我按照此图中的说明,成功创建了以下绘图:

myDf <- cbind(Row.Names=rownames(mtcars), mtcars)
plot(myDf$mpg, axes=F, xlab="Car", ylab="MPG")
axis(2)
axis(1, at=seq_along(myDf$mpg), labels=myDf$Row.Names, las=2, cex.axis=0.70)
box()

myDf我们需要在底部设置更大的边距。然后使用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:

库(ggplot2)
#我的数据

谢谢你。有没有办法通过编程而不是通过反复试验来确定边距(以及文本“line”参数)?@stackoverflowuser2010我不知道。为什么不使用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))