EsBee
EsBee

Reputation: 241

R: Plotting 2 Matrices in one plot

I have 2 Matrices as follows:

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

I am trying to plot both the matrices on same plot.

What I am currently doing is giving me 2 different plots. The following is the code:

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")

Since I would be comparing the two plots, I am looking to plot both the matrices in one single plot. (The matrices M1 and M2 are outputs of my code)

What modifications do I need to do to my current code?

Thanks a lot!!!

Upvotes: 0

Views: 293

Answers (1)

Ujjwal
Ujjwal

Reputation: 3168

Try:

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")

Upvotes: 2

Related Questions