R中barplot中的文本 [英] Text in barplot in R

查看:66
本文介绍了R中barplot中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R的小节中添加值时遇到问题.问题是我无法将值放在每个小节的中间

I have a problem when adding values in a barplot in R. The problem is that I cannot place the values in the middle of each bar

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 返回的条形图的x位置.

All you need is to save the x positions of the bars, returned by barplot.

您还可以使 col 参数更简单.使用 ifelse ,即 if 的矢量化版本.

You can also make the col argument much simpler. Use ifelse, the vectorized version of if.

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))

这篇关于R中barplot中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆