如何为矩阵中的分类时间序列数据生成更好的图? [英] How to produce a nicer plot for my categorical time series data in a matrix?

查看:108
本文介绍了如何为矩阵中的分类时间序列数据生成更好的图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此矩阵的每一行绘制在图形窗口中的单独图上.

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)

但是我有几个问题:

  1. 五个图的每个图在y轴上的标签仅包含部分结果(例如,第二个图缺少"a").有没有一种方法可以列出所有五个图的y轴上的所有分类变量? (也就是说,每个地块都有标签:a,b,c,d,e,f,g.
  2. 现在,我必须在较大的页面上显示此图,以便清楚地显示所有y轴标签.有什么办法可以使我的地块更紧密地排列在一起以节省空间,以便它们可以放在较小的页面中?

谢谢.

推荐答案

我想指出3个问题.

在此处使用plot

Use plot here

昨天在 R图形中:如何绘制要绘制的字符序列(纯分类时间序列)在单个图形上矩阵的2行或更多行,所以我建议matplot.但是现在您只想分别绘制不同的行,因此普通的plot就足够了.

Yesterday in R graphics: How to plot a sequence of characters (pure categorical time series) you were trying to plot 2 or more rows of a matrix on a single graph so I suggest matplot. But now you only want to plot different rows separately, hence the normal plot will be sufficient.

更新后,您可以使用

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

设置通用的ylim

Set a common ylim

一旦决定生成单独的图形,就希望设置一个公共的ylim,以便y轴在不同图形之间具有可比性.放

Once you decide to produce separate graphs, you want to set a common ylim so that the y-axis will be comparable between different plots. Put

ylim = c(1, length(lev))

每个plot内的

.请注意,ylim的长度为2的向量给出了最小值和最大值,因此ylim = 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 wrong.

调整绘图边距和/或在较大页面上绘图

R图有两个边距.一个是图形窗口的外部边界,另一个是内部边界.边距以两种单位度量:线和英寸.相关的图形参数为:

R plot has two margins. One is the outer margin for a graphical window, another is the inner margin. Margins are measured in two units: lines and inches. The related graphical parameters are:

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

通常将线作为单位使用会更方便,因为x轴标签,绘图标题等是由线放置的,所以使用omamar代替omimai给我们一个更好的主意如何根据我们的需要设置边距.所有参数都采用长度为4的向量,在底部",左侧",顶部",右侧"(即从底部开始顺时针)处留有边距.

Often it is more convenient to use lines as unit, as x-axis labels, plot titles, etc are placed by lines, so using oma and mar instead of omi and mai gives us a better idea how to set margins according to our need. All parameters take a vector of length 4, giving margin on "bottom", "left", "top", "right", i.e., clockwise from bottom.

通常,您不需要对外部边距进行任何处理,默认情况下,它们为全零.您可以通过par(c("oma","omi"))进行检查.请注意,将打开一个新的图形窗口,但是您可以忽略它,也可以根据需要将其关闭.要在不唤醒此类窗口的情况下查询图形参数是不可能的,请参见不打开图形设备就获取par值?.

Usually you don't need to do anything with outer margins, and they are by default all zeros. You could check this by par(c("oma","omi")). Note that a new graphical window will be opened, but you just ignore it or close it if you want. It is impossible to enquire graphical parameters without awaken such window, see grab par values without opening a graphics device?.

我们希望将顶部",底部"的内部页边距设置为0,以便所有图垂直合并在一起.这样,我们必须在顶部"和底部"设置外边距,以便为轴和标题留出一些额外的空间(如果需要).

We want to set inner margin at "top", "bottom" to 0, so that all plots will be vertically joint together. By doing this, we have to set outer margin at "top" and "bottom" to leave some extra space for axes and titles (if needed).

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)

这篇关于如何为矩阵中的分类时间序列数据生成更好的图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆