[英]How to produce a nicer plot for my categorical time series data in a matrix?
我想将此矩阵的每一行绘制在图形窗口中的单独图上。
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"
从我昨天的帖子的答案中,我需要首先将此矩阵转换为数值。
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
现在,我正在使用以下代码生成图形。
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)
但是我有几个问题:
谢谢。
我想指出3个问题。
在这里使用plot
昨天在R图形中:如何绘制字符序列(纯分类时间序列),您试图在单个图形上绘制矩阵的2行或更多行,所以我建议使用matplot
。 但是现在您只想分别绘制不同的行,因此普通plot
就足够了。
更新后,您可以使用
plot(mat_int[2,], yaxt="n", type = "l", ylim = seq_along(lev), xlab = "time", ylab = "category")
设置共同的ylim
一旦决定生成单独的图形,便要设置一个公共ylim
以便y轴在不同图之间可比。 放
ylim = c(1, length(lev))
在每个plot
里面。 请注意, ylim
采用长度为2的矢量,给出了最小值和最大值,因此ylim = 1:length(lev)
是错误的。
调整绘图边距和/或在较大页面上绘图
R图有两个边距。 一个是图形窗口的外部边界,另一个是内部边界。 边距以两种单位度量:线和英寸。 相关的图形参数为:
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轴标签,绘图标题等是由线放置的,所以使用oma
和mar
而不是omi
和mai
使我们可以更好地了解如何根据需要设置边距。 所有参数均采用长度为4的向量,在“底部”,“左侧”,“顶部”,“右侧”(即从底部开始顺时针)处留有余量。
通常,您不需要对外部边距进行任何处理,并且默认情况下它们为全零。 您可以通过par(c("oma","omi"))
。 请注意,将打开一个新的图形窗口,但是您可以忽略它,也可以根据需要关闭它。 在不唤醒此类窗口的情况下查询图形参数是不可能的,请参见在不打开图形设备的情况下获取参数值吗? 。
我们希望将“顶部”,“底部”的内部边距设置为0,以便所有图垂直合并在一起。 通过这样做,我们必须在“顶部”和“底部”设置外边距,以便为轴和标题留出一些额外的空间(如果需要)。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.