I am filling the cells of a matrix with a color according to the value of the number in the cell.
Some example data:
plotData <- structure(list(Dimension = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Dim 1", "Dim 2", "Dim 3",
"Dim 4", "Dim 5", "Dim 6", "Dim 7"), class = "factor"), R1 = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 1L, 1L, 4L, 4L, 4L, 4L, 2L,
2L, 4L, 4L, 1L, 1L, 5L, 5L, 2L, 2L, 5L, 5L, 1L, 1L, 2L, 2L, 6L,
6L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 5L, 5L, 6L, 6L, 3L,
3L, 7L, 7L, 3L, 3L, 5L, 5L, 6L, 6L, 4L, 4L, 7L, 7L, 4L, 4L, 6L,
6L, 5L, 5L, 7L,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 6L, 6L, 6L, 6L, 6L), .Label = c("Rater 1", "Rater 2", "Rater 3",
"Rater 4", "Rater 5", "Rater 6", "Rater 7"), class = "factor"),
kappa = c(0.607540983606557, 0.437185929648241, 0.731739272846381,
0.534769230769231, 0.498230088495575, 0.503184713375796,
0.189421015010722, 0.24248496993988, 0.196913787319691, 0.188524590163934,
0.71434460016488, 0.583883751651255, 0.676277476448758, 0.597444089456869,
0.630104083266613, 0.390125847047435, 0.265364008824456,
0.269453768690876, 0.12396449704142, 0.378727400798309, 0.567901234567901,
0.591351351351351, 0.630104083266613, 0.471698113207547,
0.185430463576159, 0.094765651727677, 0.348789131718842,
0.167259786476868, 0.784320438206094, 0.616555082166768,
0.130634774609016, 0.207547169811321, 0.160714285714286,
0.135011441647597, 0.533908754623921, 0.505882352941176,
0.198526950117174, 0.199491740787802, 0.121372031662269,
0.446559297218155, 0.313291139240506, 0.488774682174736,
0.607540983606557, 0.437185929648241, 0.731739272846381,
0.534769230769231, 0.498230088495575, 0.503184713375796,
0.189421015010722, 0.24248496993988, 0.196913787319691, 0.188524590163934,
0.71434460016488, 0.583883751651255, 0.676277476448758, 0.597444089456869,
0.630104083266613, 0.390125847047435, 0.265364008824456,
0.269453768690876, 0.12396449704142, 0.378727400798309, 0.567901234567901,
0.591351351351351, 0.630104083266613, 0.471698113207547,
0.185430463576159, 0.094765651727677, 0.348789131718842,
0.167259786476868, 0.784320438206094, 0.616555082166768,
0.130634774609016, 0.207547169811321, 0.160714285714286,
0.135011441647597, 0.533908754623921, 0.505882352941176,
0.198526950117174, 0.199491740787802, 0.121372031662269,
0.446559297218155, 0.313291139240506, 0.488774682174736,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(1L,
2L, 6L, 7L, 11L, 12L, 16L, 17L, 21L, 22L, 26L, 27L, 31L, 32L,
36L, 37L, 41L, 42L, 46L, 47L, 51L, 52L, 56L, 57L, 61L, 62L, 66L,
67L, 71L, 72L, 76L, 77L, 81L, 82L, 86L, 87L, 91L, 92L, 96L, 97L,
101L, 102L, 106L, 107L, 111L, 112L, 116L, 117L, 121L, 122L, 126L,
127L, 131L, 132L, 136L, 137L, 141L, 142L, 146L, 147L, 151L, 152L,
156L, 157L, 161L, 162L, 166L, 167L, 171L, 172L, 176L, 177L, 181L,
182L, 186L, 187L, 191L, 192L, 196L, 197L, 201L, 202L, 206L, 207L,
211L, 212L, 216L, 217L, 221L, 222L, 226L, 227L, 231L, 232L, 236L,
237L, 241L, 242L, 246L, 247L, 251L, 252L, 256L, 257L, 261L, 262L,
266L, 267L, 271L, 272L, 276L, 277L, 281L, 282L, 286L, 287L, 291L,
292L, 296L, 297L, 301L, 302L, 306L, 307L, 311L, 312L, 316L, 317L,
321L, 322L, 326L, 327L, 331L, 332L, 336L, 337L, 341L, 342L, 346L,
347L, 351L, 352L, 356L, 357L, 361L, 362L, 366L, 367L, 371L, 372L,
376L, 377L, 381L, 382L, 386L, 387L), class = "data.frame")
Now, this code should (I think) fill the cells with a color according to the kappa-value in the cell:
library(ggplot2)
library(ggpubr)
p <- ggplot(plotData,aes(x=R1,y=R2)) +
geom_tile(aes(fill = kappa)) +
geom_text(aes(label = round(kappa, 2))) +
labs(x="",y="",fill="Kappa") +
labs_pubr(base_size = 15) +
facet_grid(Dimension ~ .)
plot(p)
However, what I get is this:
so not all cells are coloured. I can't see why this would not work actually, but I am probably doing something stupid.
It happens because of wrong data formating - every combination (R1
& R2
is given twice, while one of the entries has NA
).
Here we see that R1 == "Rater 1" & R2 == "Rater 2"
combination is given twice with one of them having NA
's.
subset(plotData, R1 == "Rater 1" & R2 == "Rater 2")
Dimension R1 R2 kappa
1: Dim 1 Rater 1 Rater 2 0.1213720
2: Dim 2 Rater 1 Rater 2 0.4465593
3: Dim 1 Rater 1 Rater 2 NA
4: Dim 2 Rater 1 Rater 2 NA
geom_text
uses kappa
numeric value, however fill
takes NA
and that's why you're getting grey background.
To fix it use na.omit()
on your data when plotting (you still won't be able to use NA
values):
ggplot(na.omit(plotData), aes(R1, R2)) +
geom_tile(aes(fill = kappa)) +
geom_text(aes(label = round(kappa, 2))) +
facet_grid(Dimension ~ .)