第6章 基本图形
1. 条形图
-
条形图
barplot(height)
1. 简单的条形图
- 示例,
height
是一个向量
library(vcd)
counts <- table(Arthritis$Improved)
barplot(counts, main="Simple Bar Plot",
xlab="Improvement", ylab="Frequency")
barplot(counts, main="Horizontal Bar Plot",
xlab="Frequency", ylab="Improvement",
horiz=TRUE)
- 默认生成垂直的条形图,
horiz=TRUE
生成水平条形图
2. 堆砌条形图和分组条形图
- 如果
height
是一个矩阵而不是一个向量,绘图结果将会是一幅堆砌条形图或分组条形图 - 默认生成堆砌条形图
beside=FALSE
,每一列生成一个条形,beside=TRUE
生成分组条形图,每一列表示一个分组 - 示例
library(vcd)
counts <- table(Arthritis$Improved, Arthritis$Treatment)
barplot(counts, main="Stacked Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow", "green"),
legend=rownames(counts))
barplot(counts, main="Grouped Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow", "green"),
legend=rownames(counts), beside=TRUE)
- 对于示例布局进行调整
没改好
opar <- par(no.readonly=TRUE)
par(mar=c(5, 5, 4, 12), las=0, cex.axis=.6)
barplot(counts, main="Grouped Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow", "green"))
legend(c(2.5, 5), c(20,45), rownames(counts), fill=c("red", "yellow", "green"), xpd=TRUE)
par(opar)
- 将图例绘制到图形外
xpd=TRUE
3. 均值条形图
- 使用数据整合函数计算数据传递给
barplot()
作图 - 示例