Individual_Layout_Matrix
我要制作的最后一个图将具有以下布局矩阵,每个布局矩阵中的每个面板本身都由我的4板矩阵组成。
(Overall_Layout_Matrix <- matrix(c(1, 1, 2, 2, 8, 3, 4, 5, 6, 8, 0, 7, 7, 0, 8), byrow = T, ncol = 5))
# > (Overall_Layout_Matrix <- matrix(c(1, 1, 2, 2, 8, 3, 4, 5, 6, 8, 0, 7, 7, 0, 8), byrow = T, ncol = 5))
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 1 2 2 8
# [2,] 3 4 5 6 8
# [3,] 0 7 7 0 8
因此,如果每个图是连续生成的,并按顺序放置在最终的绘图布局矩阵中,则最终的布局矩阵将具有以下形式。
(Final_Layout_Matrix <- matrix(c(1, 1, 1, 1, 5, 5, 5, 5, 29, 29, 2, 2, 3, 3, 6, 6, 7, 7, 29, 29, 4, 4, 4, 4, 8, 8, 8, 8, 29, 29, 9, 9, 13, 13, 17, 17, 21, 21, 30, 31, 10, 11, 14, 15, 18, 19, 22, 23, 30, 31, 12, 12, 16, 16, 20, 20, 24, 24, 30, 31, 0, 0, 25, 25, 25, 25, 0, 0, 32, 32, 0, 0, 26, 26, 27, 27, 0, 0, 32, 32, 0, 0, 28, 28, 28, 28, 0, 0, 32, 32), byrow = T, ncol = 10))
# > (Final_Layout_Matrix <- matrix(c(1, 1, 1, 1, 5, 5, 5, 5, 29, 29, 2, 2, 3, 3, 6, 6, 7, 7, 29, 29, 4, 4, 4, 4, 8, 8, 8, 8, 29, 29, 9, 9, 13, 13, 17, 17, 21, 21, 30, 31, 10, 11, 14, 15, 18, 19, 22, 23, 30, 31, 12, 12, 16, 16, 20, 20, 24, 24, 30, 31, 0, 0, 25, 25, 25, 25, 0, 0, 32, 32, 0, 0, 26, 26, 27, 27, 0, 0, 32, 32, 0, 0, 28, 28, 28, 28, 0, 0, 32, 32), byrow = T, ncol = 10))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 1 1 1 1 5 5 5 5 29 29
# [2,] 2 2 3 3 6 6 7 7 29 29
# [3,] 4 4 4 4 8 8 8 8 29 29
# [4,] 9 9 13 13 17 17 21 21 30 31
# [5,] 10 11 14 15 18 19 22 23 30 31
# [6,] 12 12 16 16 20 20 24 24 30 31
# [7,] 0 0 25 25 25 25 0 0 32 32
# [8,] 0 0 26 26 27 27 0 0 32 32
# [9,] 0 0 28 28 28 28 0 0 32 32
我有两个问题。 首先,如何编写一个将
Individual_Layout_Matrix
和Overall_Layout_Matrix
Final_Layout_Matrix
作为输出?
秒,我如何检查一下提供的
Overall_Layout_Matrix
是合适的?例如,
Overall_Layout_Matrix
中的每个唯一数字都必须在一起并矩形形状 - 例如,诸如
matrix(c(1, 1, 1, 2, 2, 3, 4, 4, 5, 4, 4, 4), byrow = T, ncol = 3)
无法使用之类的东西,因为数字不出现在独立的矩形中。
4
提前感谢您的帮助!
# > matrix(c(1, 1, 1, 2, 2, 3, 4, 4, 5, 4, 4, 4), byrow = T, ncol = 3)
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 2 2 3
# [3,] 4 4 5
# [4,] 4 4 4
案例1
layout_augment <- function(x, y) {
y_dim <- dim(y)
y_max <- max(y)
mat <- res <- kronecker(x, array(1, y_dim))
for(i in unique(x[x != 0])) {
z <- which(mat == i, arr.ind = TRUE)
z_dim <- apply(z, 2, \(x) length(unique(x)))
r <- z_dim / y_dim
y2 <- y[rep(seq_len(y_dim[1]), each = r[1]),
rep(seq_len(y_dim[2]), each = r[2])] + (i-1)*y_max
if(nrow(z) != length(y2)) {
stop("number ", i, " does not appear in a standalone rectangle")
}
res[z] <- y2
}
return(res)
}
casten2layout_augment(Overall_Layout_Matrix, Individual_Layout_Matrix)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 1 1 1 1 5 5 5 5 29 29
# [2,] 2 2 3 3 6 6 7 7 29 29
# [3,] 4 4 4 4 8 8 8 8 29 29
# [4,] 9 9 13 13 17 17 21 21 30 31
# [5,] 10 11 14 15 18 19 22 23 30 31
# [6,] 12 12 16 16 20 20 24 24 30 31
# [7,] 0 0 25 25 25 25 0 0 32 32
# [8,] 0 0 26 26 27 27 0 0 32 32
# [9,] 0 0 28 28 28 28 0 0 32 32