我的数据集包含三列:ID(N = 1000),预期分数和实际分数。分数可以是1,2,3或4。
这是模拟数据
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)
我们可以使用
轻松创建列联表table(mydata$actual, mydata$expected)
我需要为每个ID创建一个这个数据的图。因此,想象一下该图将是1000倍1000的矩阵。
If Actual=Expected, the color of these cells will be white
If Actual < Expected, the color of these cells will be red
If Actual > Expected, the color of these cells will be blue
答案 0 :(得分:1)
每对实际和预期只有一个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)
但是如果你想要一个带有代表频率的颜色和方框的4x4矩阵,你可以这样做:
plot(t,col=col1)
编辑。 我想,你想要的是任何实际与任何预期的地图?这可以以更优雅的方式完成,但由于时间不够,我无法提供所需颜色的完整解决方案。这是一个基本颜色的快速解决方案(但颜色方案也编码)。假设你有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)
逻辑 - 创建一个NxN数组,其中包含3个级别的自定义颜色或自定义整数(1,2,3)并将其绘制为图像。如果时间允许,我会尝试在图像中定制颜色,但不能保证。