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 回答