1

我的数据集包含三列: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
4

1 回答 1

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)

在此处输入图像描述

逻辑 - 创建一个包含 3 个级别的自定义颜色或自定义整数(1、2、3)的 NxN 数组,并将其绘制为图像。如果时间允许,我会尝试在图像中自定义颜色,但不能保证。

于 2015-04-30T18:27:24.183 回答