R:在一个图中绘制2个矩阵

时间:2014-10-09 21:41:38

标签: r plot visualization

我有2个矩阵如下:

M1:

FY1301    FY1302   FY1303  FY1304   FY1305  FY1306
 146       56       159     129       54     535
 137      113       337     140      160     777
 281      111       331     198      231     875
 273       55       480     205      356     887

M2:

FY1301    FY1302   FY1303  FY1304   FY1305  FY1306
 34        5        99       82      121    180
 89       98       252       33      311    310
101       77       252       45      284    265
170       64       125       33      187    288

我试图在同一个情节上绘制两个矩阵。

我目前正在做的是给我两个不同的情节。以下是代码:

par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
matplot(M1, type = c("b"), pch = 1, col=1:6, xlab = "Month", ylab = "Total Sessions With Med1")
legend("topright", legend = c("FY13 01","FY13 02","FY13 03","FY13 04","FY13 05","FY13 06"), inset=c(-0.11,0), col=1:6, pch=1, cex = 0.5, bty = "n")

par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
matplot(M2, type = c("b"), pch = 1, col=1:6, xlab = "Month", ylab = "Total Sessions Without Med1")
legend("topright", legend = c("FY13 01","FY13 02","FY13 03","FY13 04","FY13 05","FY13 06"), inset=c(-0.11,0), col=1:6, pch=1, cex = 0.5, bty = "n")

由于我将比较这两个图,我希望在一个单独的图中绘制两个矩阵。 (矩阵M1和M2是我代码的输出)

我需要对当前代码进行哪些修改?

非常感谢!!!

1 个答案:

答案 0 :(得分:2)

尝试:

par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)

matplot(M1, type = c("b"), pch = 1, col=1:6, xlab = "Month", ylab = "Total Sessions With Med1")
    legend("topright", legend = c("FY13 01","FY13 02","FY13 03","FY13 04","FY13 05","FY13 06"), inset=c(-0.11,0), col=1:6, pch=1, cex = 0.5, bty = "n")

par(new = TRUE)

matplot(M2, type = c("b"), pch = 1, col=1:6, xlab = "Month", ylab = "Total Sessions Without Med1")
    legend("topright", legend = c("FY13 01","FY13 02","FY13 03","FY13 04","FY13 05","FY13 06"), inset=c(-0.11,0), col=1:6, pch=1, cex = 0.5, bty = "n")