R-如何使用color2d.matplot为值指定颜色

R-如何使用color2d.matplot为值指定颜色,r,plot,plotrix,R,Plot,Plotrix,我的问题涉及来自plotrix包的color2d.matplot函数。该功能已记录在案 我有以下输出: 由该代码生成: library(plotrix) # model parameters spaces <- 400 agents<- 300 prop_black = 0.5 prop_white = 1 - prop_black tolerance <- 0.6 # creating matrix of types group<-c(rep(0,spaces-a

我的问题涉及来自
plotrix
包的
color2d.matplot
函数。该功能已记录在案

我有以下输出:

由该代码生成:

library(plotrix)

# model parameters
spaces <- 400
agents<- 300
prop_black = 0.5
prop_white = 1 - prop_black
tolerance <- 0.6

# creating matrix of types
group<-c(rep(0,spaces-agents),rep(1,prop_black*agents),rep(2,prop_white*agents))
grid<-matrix(sample(group,400,replace=F), ncol=20)

# plotting
color2D.matplot(grid, ylab="", xlab = "", axes=F)
plot(runif(100,0,1),ylab="Happy",xlab="Time",col="white",ylim=c(0,1))
库(plotrix)
#模型参数

空格我能想到的一件事是为每个单元格指定颜色

cellcolors <- unlist(grid)
cellcolors <- ifelse(cellcolors == 0, "white", ifelse(cellcolors == 1, "red", "blue"))
color2D.matplot(grid, ylab="", xlab = "", axes=F, cellcolors = cellcolors)

cellcolors您可以使用矩阵中的值为颜色向量编制索引。一个较小的例子:

color2D.matplot(m, cellcolors = c("white", "red", "blue")[m + 1])


数据:

set.seed(7)
m <- matrix(sample(0:2, 9, replace = TRUE), nrow = 3, ncol = 3)
m
#      [,1] [,2] [,3]
# [1,]    2    0    1
# [2,]    1    0    2
# [3,]    0    2    0
set.seed(7)

m第二个
图与你的问题有什么关系?;)