将 RGB 通道添加到 2D 矩阵 [R]
Adding RGB channel to the 2D matrix [R]
这是一个示例矩阵:
mymat <-matrix(runif(24*24), ncol=24)
我可以使用 ggplot2 绘制此矩阵,因此我可以获得值的颜色表示:
plot_mat <- function(mat){
mat_df<- reshape2::melt(mat)
plt <- mat_df %>% ggplot2::ggplot(aes(Var2, Var1)) + geom_raster(aes(fill = value)) +scale_fill_gradientn(colours = c('blue', 'green', 'yellow', 'red')) + theme_bw()
return(plt)
}
plot_mat(mymat)
但是我想在我的数据中添加 RGB 通道而不将它们转换为图像。我想为我的 mymat 添加另一个维度,因此 dim(mymat) 输出看起来是 24、24、3。不幸的是,我不知道该怎么做。
非常感谢你的帮助。
您可以使用 ggplot_build()
提取绘图中使用的颜色,它们存储为十六进制颜色,使用 col2rgb()
将其转换为 RGB 颜色并使用 array()
重新整形。如果生成的数组未正确排序,您可以通过将 t()
转置到最终结果中来修复它,或者整个步骤中的输出之一就足够了。
set.seed(123) # For reproducibility
mymat <-matrix(runif(24*24), ncol=24)
plot_mat <- function(mat){
mat_df<- reshape2::melt(mat)
plt <- mat_df %>% ggplot2::ggplot(aes(Var2, Var1)) + geom_raster(aes(fill = value)) +scale_fill_gradientn(colours = c('blue', 'green', 'yellow', 'red')) + theme_bw()
pg <- ggplot_build(plt) # Extract data from the plot
RGBcolors <- col2rgb(pg$data[[1]]$fill, alpha = FALSE) # Transform the fill column to RGB 2D matrix
RGB_array <- array(t(RGBcolors), c(24,24,3)); # create new array reshaping to 24x24x3
# t() to transpose RGBcolors matrix to fit new third dimension
RGB_array <- DescTools::Rev(RGB_array, margin=1) # RGB_array array is flipped
return(RGB_array)
}
经过一些试验和错误后,我找到了以下非 ggplottish 解决方案:
#values in matrix converted to hex colors
colored_mymat <- paintmap::color_matrix(mymat, colors=rainbow(100))
#the rest is the same as in Robertos answer
RGB_mymat <- grDevices::col2rgb(colored_mymat, alpha = FALSE)
ar <- array(t(RGB_mymat), c(24,24,3))
#and this is for plotting purposes
flipped <- DescTools::Rev(ar, margin=1)
#rescaling values in matrix so the image would accept an input
rescaled_mymat <- apply(flipped, MARGIN = 2, FUN = function(X) (X - min(X))/diff(range(X)))
numbers_mymat <- unlist(apply(rescaled_mymat, 2, rev))
par(mar=c(0, 0, 0, 0))
image(numbers_mymat, useRaster=TRUE, axes=TRUE, col = grey(seq(0, 1, length = 256)))
我希望有一天这会对某人有所帮助。
这是一个示例矩阵:
mymat <-matrix(runif(24*24), ncol=24)
我可以使用 ggplot2 绘制此矩阵,因此我可以获得值的颜色表示:
plot_mat <- function(mat){
mat_df<- reshape2::melt(mat)
plt <- mat_df %>% ggplot2::ggplot(aes(Var2, Var1)) + geom_raster(aes(fill = value)) +scale_fill_gradientn(colours = c('blue', 'green', 'yellow', 'red')) + theme_bw()
return(plt)
}
plot_mat(mymat)
但是我想在我的数据中添加 RGB 通道而不将它们转换为图像。我想为我的 mymat 添加另一个维度,因此 dim(mymat) 输出看起来是 24、24、3。不幸的是,我不知道该怎么做。
非常感谢你的帮助。
您可以使用 ggplot_build()
提取绘图中使用的颜色,它们存储为十六进制颜色,使用 col2rgb()
将其转换为 RGB 颜色并使用 array()
重新整形。如果生成的数组未正确排序,您可以通过将 t()
转置到最终结果中来修复它,或者整个步骤中的输出之一就足够了。
set.seed(123) # For reproducibility
mymat <-matrix(runif(24*24), ncol=24)
plot_mat <- function(mat){
mat_df<- reshape2::melt(mat)
plt <- mat_df %>% ggplot2::ggplot(aes(Var2, Var1)) + geom_raster(aes(fill = value)) +scale_fill_gradientn(colours = c('blue', 'green', 'yellow', 'red')) + theme_bw()
pg <- ggplot_build(plt) # Extract data from the plot
RGBcolors <- col2rgb(pg$data[[1]]$fill, alpha = FALSE) # Transform the fill column to RGB 2D matrix
RGB_array <- array(t(RGBcolors), c(24,24,3)); # create new array reshaping to 24x24x3
# t() to transpose RGBcolors matrix to fit new third dimension
RGB_array <- DescTools::Rev(RGB_array, margin=1) # RGB_array array is flipped
return(RGB_array)
}
经过一些试验和错误后,我找到了以下非 ggplottish 解决方案:
#values in matrix converted to hex colors
colored_mymat <- paintmap::color_matrix(mymat, colors=rainbow(100))
#the rest is the same as in Robertos answer
RGB_mymat <- grDevices::col2rgb(colored_mymat, alpha = FALSE)
ar <- array(t(RGB_mymat), c(24,24,3))
#and this is for plotting purposes
flipped <- DescTools::Rev(ar, margin=1)
#rescaling values in matrix so the image would accept an input
rescaled_mymat <- apply(flipped, MARGIN = 2, FUN = function(X) (X - min(X))/diff(range(X)))
numbers_mymat <- unlist(apply(rescaled_mymat, 2, rev))
par(mar=c(0, 0, 0, 0))
image(numbers_mymat, useRaster=TRUE, axes=TRUE, col = grey(seq(0, 1, length = 256)))
我希望有一天这会对某人有所帮助。