Plotting a heatmap out of common value matrix in R

Plotting a heatmap out of common value matrix in R



I have a tab-separated file like the following:


C1P C2P C3P C4P C5P
sam1 3 cp 3 cp 3 cp 3 cp 3 cp
sam2 S3c 4 cp 3 cp 3 cp S3c
sam3 3 cp 3 cp 3 cp 3 cp 3 cp
sam4 3 cp 3 cp LOH LOH 3 cp
sam5 3 cp 3 cp 3 cp 3 cp 3 cp
sam6 4 cp 4 cp UPD UPD UPT



Now I want to assign a color to each value [condition]...and let the color appear wherever there is the same value, thereby making a heatmap that represents occurences of a specific condition [values in the column] in each sample [row headers].



enter image description here



Right now I am assigning numeric values to each condition, then generating the heatmap through pheatmap. But I was looking for more robust way of doing it.



Appreciate any help.






Welcome to SO! It's better to put some usable to copy and past in R to have good answers. However you can look at geom_tile() in ggplot2 package, and, looking at your data, you'll have to put them from wide to long format before it.

– s_t
Sep 17 '18 at 11:34


geom_tile()


ggplot2






Have you tried image()

– HolgerBarlt
Sep 17 '18 at 11:35


image()






@s_t Thank You...I shall keep that in mind... ya I saw ggplots is working wonders around the labs....

– SangramJB
Sep 18 '18 at 6:15







@HolgerBarlt I haven't tried..but I tried to work with it before but I found it difficult to wrap my head around that package... so I switched to pheatmap and heatmap2 ... :(

– SangramJB
Sep 18 '18 at 6:19




1 Answer
1



This should get you fairly close:


library(tidyverse)
df %>%
rownames_to_column("row") %>%
gather(col, Value, -row) %>%
mutate(
row = factor(row, levels = rev(unique(row))),
Value = factor(Value)) %>%
ggplot(aes(col, row, fill = Value)) +
geom_tile() +
scale_fill_manual(values = c(
`3 cp` = "yellow",
`4 cp` = "red",
LOH = "blue",
S3c = "lightgreen",
UPD = "darkgreen",
UPT = "black")) +
labs(x = "", y = "") +
theme_minimal()



enter image description here



Explanation:


geom_tile


factor


df <- read.table(text =
" C1P C2P C3P C4P C5P
sam1 '3 cp' '3 cp' '3 cp' '3 cp' '3 cp'
sam2 'S3c' '4 cp' '3 cp' '3 cp' 'S3c'
sam3 '3 cp' '3 cp' '3 cp' '3 cp' '3 cp'
sam4 '3 cp' '3 cp' LOH LOH '3 cp'
sam5 '3 cp' '3 cp' '3 cp' '3 cp' '3 cp'
sam6 '4 cp' '4 cp' UPD UPD UPT", header = T)






...Thank you...thats exactly what I am looking for. Since I am not very affluent in R scripts :( , I have a few doubts: Q1: what is %>% doing ? Q2: mutate is a function that is converting the rows into a single column ? and I actually get a whole lot of conditions not just 4-5 of them, so how can i dynamically assign colors to them ?..............Thanks a lot for the help !

– SangramJB
Sep 18 '18 at 11:23


%>%






@SangramJB Q1. %>% is the magrittr pipe operator, that pipes the left hand side values into expressions on the right hand side (think x %>% f() instead of f(x)). Q2. mutate is one of the key dplyr functions to add/change variables in a data.frame or tibble. Generally, I'd recommend a basic dplyr/ggplot2 tutorial to get started. I'd do a terrible job trying to explain the details here in a comment.

– Maurits Evers
Sep 18 '18 at 12:09



%>%


magrittr


x %>% f()


f(x)


mutate


dplyr


data.frame


tibble


dplyr


ggplot2






:D...No that would be ok...I can definitely go through some tutorials.... dplyr/ggplot2 ...got it ...Thanks a lot....just one more thing..what if I had 15 fields to assign colors to instead of six as in example ??? how can I assign colors automatically ??

– SangramJB
Sep 18 '18 at 12:41







You're very welcome @SangramJB. If you remove the entire scale_fill_manual(...) block, ggplot will assign colours automatically. I only used scale_fill_manual to increase the aesthetic similarity with the plot you showed in your OP.

– Maurits Evers
Sep 18 '18 at 12:55


scale_fill_manual(...)


ggplot


scale_fill_manual



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế