Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

R graphics: How to plot a sequence of characters (pure categorical time series)

I have a matrix in which each element is a pure categorical variable "a","b","c","d",... Each column of the matrix is a chronological entry and now I want to plot the matrix by row and I hope the y-axis is the sequence of characters.

Here is the original matrix:

Graph with legend

Here is what I wanted the plot to be: Graph with legend

The red plot is first row of the matrix and the blue plot is the fifth.

I have tried some existing packages but mostly they require me to transfer the categorical variables to numerical variables. So I wonder if anyone could help me with this. Many thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Without your data, I have to generate some toy one, called mat, with 5 rows and 10 columns, filled with letters[1:7].

set.seed(0); mat <- matrix(sample(letters[1:7], 5 * 10, TRUE), nrow = 5)
#     [,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"  

Basically you need first re-represent you character matrix mat with integers.

## flatten your object into a vector first
if (is.matrix(mat)) v <- as.character(mat)
if (is.data.frame(mat)) v <- as.character(unlist(mat, use.names = FALSE))
lev <- sort(unique(v))    ## sorted unique labels

## re-representation
mat_int <- matrix(match(v, lev), nrow = nrow(mat))
## or: mat_int <- matrix(as.integer(factor(v, levels = 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

Then you just plot (the whole or some rows of) this matrix using matplot. Disable the y-axis first then add it later using axis so that you can customize axis labels.

## this plots the whole matrix
matplot(t(mat_int), yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)

## this plots 1st and 5th rows
matplot(t(mat_int)[, c(1,5)], yaxt = "n", type = "l", xlab = "time", ylab = "category")
axis(2, seq_along(lev), labels = lev)

Plot of the selected two rows:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...