m [,1] [,-6ren">
gpt4 book ai didi

r - 字符类型的矩阵图

转载 作者:行者123 更新时间:2023-12-04 10:45:07 24 4
gpt4 key购买 nike

我正在寻找一种绘制字符类型矩阵的方法:

m=matrix(data=c("A","A","B","B","B","C","C","B"),nrow=4,ncol=2)
> m
[,1] [,2]
[1,] "A" "B"
[2,] "A" "C"
[3,] "B" "C"
[4,] "B" "B"

具有一组定义的颜色
A="Yellow"
B="Blue"
C="Green"

我应该从矩阵传递到 ascii 并使用 图像()来自 sp包 ?

我正在寻找这样的东西:

enter image description here

最佳答案

这更取决于您所说的“绘制矩阵”是什么意思:

 m2 <- m
m2[] <- c("yellow", "blue","green")[match(m, c("A","B","C"))]
m2
#------------
[,1] [,2]
[1,] "yellow" "blue"
[2,] "yellow" "green"
[3,] "blue" "green"
[4,] "blue" "blue"
#------------
plot(row(m2), col(m2), col=m2, pch=18, cex=4)

这将在由矩阵 m 的行和列确定的矩阵位置绘制指定颜色的实心菱形。另一种方式与 image :
m2[] <- match(m, c("A","B","C"))
mode(m2) <- "numeric"
m2
image(1:nrow(m2), 1:ncol(m2), m2, col=c("yellow", "blue","green"))

enter image description here

关于r - 字符类型的矩阵图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8682140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com