减少条形图中names.arg和轴(框)之间的空间

时间:2021-03-24 10:01:53

标签: r bar-chart whitespace

我创建了一个简单的 barplot,周围有一个盒子。有什么办法可以让我的名字靠近方框(蓝色标记的空格)?

MWE:

set.seed(1)
count <- runif(n = 3, min = 0, max = 10)
names <- letters[seq(from = 1, to = 3)]

barplot(height = count,
        names.arg = names,
        horiz = TRUE,
        las = 1)
box()

enter image description here

1 个答案:

答案 0 :(得分:1)

这里有两种方法可以做到这一点。您可以使用 rect 而不是 box 将框边界向左移动:

barplot(height=count, names.arg=names, horiz=TRUE, las=1)
bounds <- par("usr")
rect(bounds[1]-.1, bounds[3], bounds[2], bounds[4], xpd=NA)

Change Box

或者您可以单独添加 y 轴,以控制标签的绘制位置:

x <- barplot(height=count, horiz=TRUE, las=1)
box()
axis(2, x, names, las=1, tick=FALSE, mgp=c(2, .35, 0))

Set y-axis

调整 mgp 中的中间值以定位标签(参见 ?par