gpt4 book ai didi

r - 在 r 中的条形图中居中条形标签

转载 作者:行者123 更新时间:2023-12-01 08:35:24 26 4
gpt4 key购买 nike

如何使 x 轴上的标签居中以与条形匹配?另外,如何将 x 轴标签进一步向下定位,使其不会被 x 轴标签遮挡?谢谢!

par(mar= c(15,4,4,2) + 0.1)
barplot(58:1,xaxt="n",xlab="",ylab="Frequency", col=gray(5:0/5))
axis(1, labels=FALSE)
text(1:58, par("usr")[3] - 0.25, srt = 90, adj = 1,
labels = rep("Long Species Name",58), xpd = TRUE)
mtext(1, text = "Species", line=6)

enter image description here

最佳答案

检查 barplot() 的返回值(通过阅读 ?barplot)。在那里,我们发现条形的中点由函数作为向量返回。因此,只需分配返回的对象(此处为对象 bar),然后在调用 axis() 时使用它来定位刻度线。

axis() 调用中,请注意我们同时指定 labels 参数 at 参数, at 设置为存储在 bar 中的条形中点。 las = 2 用于将标签相对于轴旋转,cex.axis = 0.6 用于减小标签尺寸。

您问题的第二部分由 title()line 参数处理。首先请注意,当您设置 mar 参数时,您将边距大小设置为“行”,因此边 1(底部)的边距为 15 行。 title() 中的 line 参数指定要绘制轴标签的边缘线。

把这个和我们有一个修改过的例子放在一起:

op <- par(mar= c(15,4,4,2) + 0.1)
bar <- barplot(58:1, xaxt="n", xlab="", ylab="Frequency", col=gray(5:0/5))
axis(1, labels = paste("Long Species Name", 1:58), at = bar,
las = 2, cex.axis = 0.6)
title(xlab = "Species", line=11)
par(op)

产生:

barplot produced using example code

关于r - 在 r 中的条形图中居中条形标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com