[英]x-axis labels do not match bars
Hello I am trying to create a stacked barplot using the following code: 您好,我正在尝试使用以下代码创建堆叠式barplot:
test <- as.matrix(read.csv(file="test4.csv",sep=",",head=TRUE))
test <- test[,2:ncol(test)]
pdf(file="test.pdf", height=4, width=6)
par(lwd = 0.3)
barplot(test, space=0.4, xaxt='n', ann=FALSE)
axis(1, cex.axis=0.25, las=2, at=1:ncol(test), space=0.4, labels=colnames(test))
dev.off()
And I get: 我得到:
As you can see the labels in the x-axis do not match the bars in the plot. 如您所见,x轴上的标签与图中的条形图不匹配。 Also, the ticks are huge. 此外,滴答声是巨大的。 Can you guys help me beautify the x axis? 你们可以帮我美化x轴吗? Thanks so much 非常感谢
Try storing the returned value of the call to barplot()
in a named object, and then passing that in to the at=
argument of axis()
: 尝试barplot()
的调用返回的值存储在命名对象中,然后将其传递给axis()
的at=
参数:
xLabLocs <- barplot(test, space=0.4, xaxt='n', ann=FALSE)
axis(1, cex.axis=0.25, las=2, at=xLabLocs,
space=0.4, labels=colnames(test))
This may look odd, but it is explained in the Value
section of the ?barplot
help file: 这可能看起来很奇怪,但是在?barplot
帮助文件的“ Value
部分中对此进行了解释:
Value: A numeric vector (or matrix, when 'beside = TRUE'), say 'mp', giving the coordinates of _all_ the bar midpoints drawn, useful for adding to the graph.
You just made the (easy enough to make) mistake of assuming that the x-axis coordinates of the bar centers are at 1:n, where n is the number of bars. 您只是犯了一个错误(容易犯),即假设钢筋中心的x轴坐标为1:n,其中n是钢筋的数量。 That's not necessarily true, so it's nice that a single call to barplot()
will both: (a) plot the bar plot as its side effect; 不一定是正确的,因此一次调用barplot()
会很好:(a)绘制条形图作为其副作用; and (b) return the necessary x-axis coordinates as its return value. (b)返回必要的x轴坐标作为其返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.