如何在barplot lan中为每个条添加标签

时间:2017-08-26 14:58:03

标签: r

我想为Y值

的每个栏添加标签

dataset

Barplot

1 个答案:

答案 0 :(得分:3)

您可以在新行上为barplot()添加带有text()属性的y值,如下例所示:

library(tibble)
df <- data_frame(month=month.abb[1:12], Freq=c(12, 11, -5, 3, 4, 1, 43, 5,60,34,-4,3))
p<-barplot(df$Freq, col = rainbow(20), ylim=c(-10,60), names.arg = df$month, space=0, axes=F)
xval = seq(-5, 60, 5)
axis(side = 2, at = xval, labels = FALSE, xpd=T)
axis(side = 2, at = xval, tick = FALSE, labels = xval, xpd=T)
text(p, df$Freq + 2*sign(df$Freq), labels=df$Freq, xpd=TRUE) #this is the line of your interest in regards to the value labels

enter image description here