条形图并不一定要基于计数数据或频率数据。你可以使用数据整合函数并将结果传递给
barplot()函数,来创建表示均值、中位数、标准差等的条形图。
> states <- data.frame(state.region, state.x77)
> means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)
> means
Group.1 x
1 Northeast 1.00
2 South 1.74
3 North Central 0.70
4 West 1.02
> means <- means[order(means$x),]
> means
Group.1 x
3 North Central 0.70
1 Northeast 1.00
4 West 1.02
2 South 1.74
> barplot(means$x, names.arg=means$Group.1)
> title("Mean Illiteracy Rate")
# 增加y边界的大小
par(mar=c(5,8,4,2))
# 旋转条形的标签
par(las=2)
counts <- table(Arthritis$Improved)
# 缩小字体大小,让标签更合适
barplot(counts,
main="Treatment Outcome",
horiz=TRUE,
cex.names=0.8,
names.arg=c("No Improvement", "Some Improvement",
"Marked Improvement"))