3

を使用してバーに値を書き込む方法はmtext?

# Grouped Bar Plot
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",xlab="Number of Gears", col=c("darkblue","red"),legend = rownames(counts), beside=TRUE, horiz=TRUE)

mtext(counts )   # But position is not at each bar. 

各棒グラフに対応する値を配置するにはどうすればよいですか?

4

1 に答える 1

19

まあ、mtext一般的に余白に使用されます。使いたくない理由はありますtextか?

 bplt <- barplot(counts, 
                 main="Car Distribution by Gears and VS", xlab="Number of Gears", 
                 col=c("darkblue","red"), legend = rownames(counts), 
                 beside=TRUE, horiz=TRUE)

# variable 'bplt' is now a matrix of vertical bar positions on the y-axis

text(x= counts+0.3, y= bplt, labels=as.character(counts), xpd=TRUE)
# Needed to use xpd=TRUE because the xlimits were too narrow.

ここに画像の説明を入力

于 2012-08-11T03:17:39.227 に答える