Do it this way
tbl <- as.data.frame(table(A, B))
It should gave you three columns data.frame
: A
, B
, Freq
which should be sufficient for your heatmap with code similar to this sample working example
library(stringi)
library(dplyr)
library(ggplot2)
n <- 10000
A <- stri_rand_shuffle(stri_rand_strings(n, 1, '[a-z]'))
B <- stri_rand_shuffle(stri_rand_strings(n, 1, '[a-z]'))
tbl <- as.data.frame(table(A, B)) %>%
arrange(A, B) %>%
filter(Freq >= 10)
ggplot(data = tbl, aes(x = A, y = B, fill = Freq)) +
geom_tile()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…