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].
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.
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()
Explanation:
geom_tile
to draw a heatmap, where the fill colour is given by the factor
levels of your values. 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)
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.