首页 > 解决方案 > 减少 barplot 中 names.arg 和轴(框)之间的空间

问题描述

我创建了一个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()

在此处输入图像描述

标签: rbar-chartwhitespace

解决方案


这里有两种方法可以做到这一点。您可以使用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)

零钱箱

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

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

设置 y 轴

调整中间值mgp以定位标签(请参阅?par


推荐阅读