Michel Bergel
Michel Bergel

Reputation: 31

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 (i.e. values) and variable 2 can have another 70 different possibilities (i.e. values), for a 77x70 matrix. Total rows goes beyond 1,000,000.

Upvotes: 0

Views: 2218

Answers (1)

Erin
Erin

Reputation: 386

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()

enter image description here

Upvotes: 3

Related Questions