每对实际和预期只有一个 ID,因此它将是一个线性图。您不想绘制实际值和预期值,对吗?
ID <- seq(from = 1, to = 1000, by=1)
actual <- round(runif(1000, min=1, max=4))
expected <- round(runif(1000, min=1, max=4))
mydata <- data.frame(ID, actual, expected)
View(mydata)
t = table(mydata$actual, mydata$expected)
attach(mydata)
col1 = ifelse(actual == expected , "white", ifelse(actual < expected, "red", "blue"))
plot(ID,col=col1)
![在此处输入图像描述](https://i.stack.imgur.com/4yYZB.png)
但是,如果您想要一个 4x4 矩阵,其中包含表示频率的颜色和框,您可以这样做:
plot(t,col=col1)
![在此处输入图像描述](https://i.stack.imgur.com/25STw.png)
编辑。我想,你想要的是一张任何实际与任何预期的地图?这可以以更优雅的方式完成,但由于时间不够,我无法为您提供所需颜色的完整解决方案。这是一个使用基本颜色的快速解决方案(但也编码了配色方案)。假设你有 N=5。
set.seed(12345)
ID <- seq(from = 1, to = 5, by=1)
actual <- round(runif(5, min=1, max=4))
expected <- round(runif(5, min=1, max=4))
mydata <- data.frame(ID, actual, expected)
> mydata
ID actual expected
1 1 3 1
2 2 4 2
3 3 3 3
4 4 4 3
5 5 2 4
colID = matrix("",5,5)
arr = matrix(0,5,5)
for (i in 1:5) {
for (j in 1:5) {
colID[i,j] = ifelse(actual[i] == expected[j] , "green", ifelse(actual[i] < expected[j], "red", "blue"))
arr[i,j] = ifelse(actual[i] == expected[j] , 1, ifelse(actual[i] < expected[j], 2, 3))
}
}
> arr
[,1] [,2] [,3] [,4] [,5]
[1,] 3 3 1 1 2
[2,] 3 3 3 3 1
[3,] 3 3 1 1 2
[4,] 3 3 3 3 1
[5,] 3 1 2 2 2
> colID
[,1] [,2] [,3] [,4] [,5]
[1,] "blue" "blue" "green" "green" "red"
[2,] "blue" "blue" "blue" "blue" "green"
[3,] "blue" "blue" "green" "green" "red"
[4,] "blue" "blue" "blue" "blue" "green"
[5,] "blue" "green" "red" "red" "red"
> image(arr)
![在此处输入图像描述](https://i.stack.imgur.com/CI9Cw.png)
逻辑 - 创建一个包含 3 个级别的自定义颜色或自定义整数(1、2、3)的 NxN 数组,并将其绘制为图像。如果时间允许,我会尝试在图像中自定义颜色,但不能保证。