I would like to plot each row of this matrix on separate plot in a graphical window.
mat <-
structure(c("g", "b", "c", "e", "g", "b", "g", "g", "e", "e",
"a", "b", "b", "e", "c", "f", "d", "f", "g", "c", "f", "g", "b",
"e", "a", "b", "c", "a", "c", "g", "c", "d", "e", "d", "b", "f",
"e", "f", "a", "f", "c", "f", "e", "f", "d", "d", "f", "a", "d",
"f"), .Dim = c(5L, 10L))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] "g" "b" "a" "f" "f" "b" "c" "f" "c" "d"
#[2,] "b" "g" "b" "d" "g" "c" "d" "e" "f" "f"
#[3,] "c" "g" "b" "f" "b" "a" "e" "f" "e" "a"
#[4,] "e" "e" "e" "g" "e" "c" "d" "a" "f" "d"
#[5,] "g" "e" "c" "c" "a" "g" "b" "f" "d" "f"
From the answer to my yesterday's post, I need to convert this matrix to numerical first.
v <- as.character(mat)
lev <- sort(unique(v)) ## sorted unique labels
# [1] "a" "b" "c" "d" "e" "f" "g"
mat_int <- matrix(match(v, lev), nrow = nrow(mat))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 7 2 1 6 6 2 3 6 3 4
#[2,] 2 7 2 4 7 3 4 5 6 6
#[3,] 3 7 2 6 2 1 5 6 5 1
#[4,] 5 5 5 7 5 3 4 1 6 4
#[5,] 7 5 3 3 1 7 2 6 4 6
Now I am using the following code to generate my graph.
par(mfrow=c(5,1))
matplot(t(mat_int)[, c(1)], yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)
matplot(t(mat_int)[, c(2)], yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)
matplot(t(mat_int)[, c(3)], yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)
matplot(t(mat_int)[, c(4)], yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)
matplot(t(mat_int)[, c(5)], yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)
But I have several issues:
- The label on the y-axis for each plot of the five contains only partial results (say the 2nd plot is missing "a"). Is there a way we can list all the categorical variables on y-axis for all five of the plots? (That is to say, every plot has labels: a,b,c,d,e,f,g.
- Right now I have to produce this plot on a large page, in order to display all y-axis labels clearly. Is there any way to arrange my plots more close together to save space, so that they could fit in a smaller page?
Thank you.