-2

I have 2 tables: table 1: rows- samples, columns- bacterias- square in place (i,j) describes the frequency of the bacteria j in sample i.

enter image description here image.png

table2 : rows- samples, columns- type (3 options: type1/type2/type3)

image.png

I have created an heatmap from the first table with this code:

heatmap<-pheatmap(data.for.heatmap2,color = brewer.pal(9,"Blues"),
             show_rownames = F, cluster_cols = F)

Now, I want to add the classification from the second table to the heatmap. The classification is something like this:

enter image description here

How can I do it? Thanks!

chucha
  • 7
  • 2
  • 1
    [Can you please provide a reproducible example of your data?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – user438383 Jul 24 '22 at 08:09

1 Answers1

0

Here is a basic approach to what you want:

After joining by rownames, we transform into long format then use geom_tile:

library(tidyverse)

df <- left_join(rownames_to_column(table1), 
          rownames_to_column(table2), by="rowname") %>% 
  pivot_longer(cols = starts_with("bact"))

ggplot(df, aes(name, rowname, fill= log(value), color = type)) + 
  geom_tile(size =1) +
  scale_y_discrete(limits=rev) +
  scale_colour_manual(values = c("red", "blue", "gold"))+
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank())

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you! I have used : 'pheatmap'. I added a code to the question, not sure how to do the same as in pheatmap? – chucha Jul 24 '22 at 17:34