[英]Adding RGB channel to the 2D matrix [R]
Here is an example matrix:这是一个示例矩阵:
mymat <-matrix(runif(24*24), ncol=24)
I can plot this matrix using ggplot2, so I can obtain color representation of values:我可以使用 plot 这个矩阵使用 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)
I would like to add RGB channel to my data without converting them into an image.我想将 RGB 通道添加到我的数据中而不将它们转换为图像。 I would like to add another dimension to my mymat, so the dim(mymat) output would look 24, 24, 3. Unfortunately, I do not know, how to do so.我想为我的 mymat 添加另一个维度,因此 dim(mymat) output 看起来会是 24、24、3。不幸的是,我不知道该怎么做。
I would be grateful for your help.我会很感激你的帮助。
You can extract the colors used in your plot with ggplot_build()
, they are stored as hex colors, convert it to RGB colors with col2rgb()
and reshape it using array()
. You can extract the colors used in your plot with ggplot_build()
, they are stored as hex colors, convert it to RGB colors with col2rgb()
and reshape it using array()
. If the resulting array is not properly sorted you can fix it likely with transposing t()
onto the final results or one of the outputs throughout the steps will be enough.如果结果数组没有正确排序,您可以通过将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)
}
After some trials and errors I have found a following non-ggplottish solution:经过一些试验和错误后,我发现了以下非 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)))
I hope this might help someone someday.我希望有一天这对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.