简体   繁体   中英

Creating a heatmap from 2 columns

I'm trying to create a heatmap from how many times the variable 1 coincides with variable 2 in R.

Example:

Var1 | Var2
a   |  x
a   |  x
b  |   x
c  |   y

The combination a|x shows twice, so the heatmap should have the value 2 on row a, column x; value 1 on row b, column x; value 1 on row c, column y etc.

The main problem is that variable 1 can have 77 different possibilities (ie values) and variable 2 can have another 70 different possibilities (ie values), for a 77x70 matrix. Total rows goes beyond 1,000,000.

R should be able to handle that. Something like this?

library(tidyverse)

df = data.frame(Var1 = sample(1:70, 2000000, replace = T),
                Var2 = sample(1:70, 2000000, replace = T))

table(df) %>%
  as.data.frame() %>%
  ggplot() +
  aes(x=Var1, y=Var2, fill=Freq) %>%
  geom_tile()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM