0) "blue" else -6ren">
gpt4 book ai didi

r - R中条形图中的文本

转载 作者:行者123 更新时间:2023-12-03 22:12:48 25 4
gpt4 key购买 nike

在 R 中的条形图中添加值时出现问题。问题是我无法将值放在每个条形的中间

balance<- c(-4.3963714,0.2335795,-0.2777250,-2.0037130,-1.2526801, -6.4556516)
barnames<-c("E1","E11","E12","E5","E7","E9")
barplot(balance,ylim=c(-8,2),col=c(if ((balance[1])>0) "blue" else "red",(if ((balance[2])>0) "blue" else "red"),(if ((balance[3])>0) "blue" else "red"), (if ((balance[4])>0) "blue" else "red"),(if ((balance[5])>0) "blue" else "red"), (if ((balance[6])>0) "blue" else "red")),main="Balance del Stock de Carbono",names.arg= barnames,ylab="Variacion del Stock de C kg/m2")
abline(h=0)
text((balance/2),labels=round(balance,digits=2))

这是条形图:

Barplot

最佳答案

您所需要的只是保存由 barplot 返回的柱线的 x 位置。 .

您也可以制作 col论证简单多了。使用 ifelseif 的矢量化版本.

bp <- barplot(balance, ylim = c(-8, 2), 
col = ifelse(balance > 0, "blue", "red"),
main = "Balance del Stock de Carbono",
names.arg = barnames,
ylab="Variacion del Stock de C kg/m2")

abline(h=0)
text(bp, balance/2, labels = round(balance, digits = 2))

enter image description here

关于r - R中条形图中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53134623/

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