Rajan Raju
Rajan Raju

Reputation: 123

How to create heatmap with tree in R or Python?

Example dataset has been given below. First column denotes ID and other three columns represent feature 1, feature 2, feature 3 respectively. N.B. Features values will be discrete always.

Id,f1,f2,f3
abc,1,1,2
def,1,1,3
ghi,2,3,1

I want to create a heatmap with tree. And similar ones will group together.

Example figure:

enter image description here

Upvotes: 0

Views: 216

Answers (1)

Kra.P
Kra.P

Reputation: 15123

You may try

library(tidyverse)


df <- read.table(text = "Id,f1,f2,f3
abc,1,1,2
def,1,1,3
ghi,2,3,1", header = T, sep =',')

df %>% column_to_rownames(var = "Id") %>% as.matrix() %>% heatmap

enter image description here

Add

I'm not sure it will work with your data, but using mtcars dataset,

data <- as.matrix(mtcars)
heatmap(data, col= colorRampPalette(RColorBrewer::brewer.pal(8, "Blues"))(3), Colv = NA)

enter image description here

(3) next at colorRampPalette(RColorBrewer::brewer.pal(8, "Blues"))(3) part indicates how many colors you will use.

Colv = NA options removes upper tree and Rowv will control tree on the left.

Upvotes: 2

Related Questions