How to create a more pleasant plot for my categorical data series in the matrix?

I would like to build each row of this matrix on a separate chart in the graphics 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" 

In response to my post yesterday, I need to first convert this matrix to numeric.

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

enter image description here

But I have a few questions:

  • The y-axis label for each of the five graphs contains only partial results (for example, β€œa” is missing in the second section). Is there a way we can list all categorical variables along the y axis for all five graphs? (That is, each plot has labels: a, b, c, d, e, f, g.
  • Now I have to create this graph on a large page to clearly display all the labels on the y axis. Is there a way to arrange my sites closer together to save space so that they can fit on a smaller page?

Thanks.

0
source share
1 answer

I would like to point out 3 questions.

Use plot here

Yesterday in R-graphs: how to build a sequence of characters (pure categorical time series) , you tried to build 2 or more rows of a matrix on one graph, so I suggest matplot . But now you want to separate different lines separately, so a normal plot will suffice.

As you upgrade you can use

  plot(mat_int[2,], yaxt="n", type = "l", ylim = seq_along(lev), xlab = "time", ylab = "category") 

Set generic ylim

As soon as you decide to create separate graphs, you want to set a common ylim so that the y axis is comparable between different graphs. Placed

 ylim = c(1, length(lev)) 

inside each plot . Note that ylim takes a vector of length 2, giving min and max, so ylim = 1:length(lev) is incorrect.

Adjust your site’s margin and / or chart on a larger page

Schedule

R has two fields. One of them is the outer edge of the graphics window, the other is the inner edge. Fields are measured in two units: lines and inches. Related graphic options:

 oma: *o*uter *ma*rgin in lines omi: *o*uter *m*argin in *i*nches mar: inner *mar*gin in lines mai: inner *ma*rgin in *i*nches 

It is often more convenient to use the lines as a unit, since the labels are on the X axis, the names of the plots, etc. placed in lines, so using oma and mar instead of omi and mai gives us a better understanding of how to set the fields according to our need. All parameters take a vector of length 4, specifying the edge "bottom", "left", "top", "right", i.e. Clockwise from the bottom.

Usually you do not need to do anything with external fields, and they default to all zeros. You can check this on par(c("oma","omi")) . Note that a new graphics window will open, but you simply ignore it or close it if you want. It is impossible to request graphical parameters without waking up such a window; see capturing the nominal values ​​without opening the graphical device? .

We want to set the inner edge in the "upper", "lower" to 0, so that all the graphs are vertically combined. In doing this, we need to set the outer edge in the upper and lower layers to leave additional space for the axes and headers (if necessary).

 new_par <- old_par <- par(c("mar", "oma")) new_par$mar[1] <- 0; new_par$mar[3] <- 0 ## inner bottom and top margin to 0 new_par$oma[1] <- 3; new_par$oma[3] <- 3 ## outer bottom and top margin to 3 par(new_par) ## set new par par(mfrow = c(5,1)) plot(mat_int[1, ], yaxt = "n", type = "l", xlab = "time", ylab = "category", xaxt = "n", ylim = c(1, length(lev))) axis(3, axTicks(3)) ## place an x-axis on the top axis(2, seq_along(lev), labels = lev) axis(1, axTicks(1), labels = NA) ## draw ticks, but no labels plot(mat_int[2, ], yaxt = "n", type = "l", xlab = "time", ylab = "category", xaxt = "n", ylim = c(1, length(lev))) axis(2, seq_along(lev), labels = lev) axis(1, axTicks(1), labels = NA) plot(mat_int[3, ], yaxt = "n", type = "l", xlab = "time", ylab = "category", xaxt = "n", ylim = c(1, length(lev))) axis(2, seq_along(lev), labels = lev) axis(1, axTicks(1), labels = NA) plot(mat_int[4, ], yaxt = "n", type = "l", xlab = "time", ylab = "category", xaxt = "n", ylim = c(1, length(lev))) axis(2, seq_along(lev), labels = lev) axis(1, axTicks(1), labels = NA) plot(mat_int[5, ], yaxt = "n", type = "l", xlab = "time", ylab = "category", ylim = c(1, length(lev))) axis(2, seq_along(lev), labels = lev) 

enter image description here

0
source

All Articles