Home > Enterprise >  Heatmap with one column in R
Heatmap with one column in R

Time:08-16

I have a dataframe with scores associated with every cell and I have the result of clustering (not related to the score) in one column of my dataframe:

>head(clust.labs)
type value cell
1    1   0.3    1
2    1   0.5    2
3    1  -0.3    3
4    1   0.5    4
5    1   0.3    5
6    1   0.3    6

I want to make a heatmap with one column representing the cells, samples coming in order and colors represent the scores(value). Currently I have made a heatmap that looks like below, I want the colored parts squished to one column and. I want a rectangle to be on the left representing samples. How can I do that?

 ggplot(data = clust.labs, mapping = aes(x = type,
                                         y = cell,
                                         fill = value))  
    geom_tile()  
    xlab(label = "Sample")

enter image description here

CodePudding user response:

I am not completely sure how the output should look, but I decided to give it a try. Since you wanted to make a single column plot, you should add a variable that has the same value for all samples, which in this case I named dummy. Then, you can do the heatmap and add the rectangle using geom_rect. Finally, you can adjust the x-axis breaks to avoid showing the -0.5 and 0.5 labels.

library(ggplot2)
library(dplyr)

df |>
  mutate(dummy = 1) |>
  ggplot(aes(x = factor(dummy),
             y = cell,
             fill = value))  
  #Add rectangle
  geom_rect(aes(xmin=factor(-0.5),
                xmax=factor(0.5),
                ymin=0.5,
                ymax=1.5),
            colour = "black",
            fill = "transparent")  
  geom_tile()  
  # Change breaks for x axis
  scale_x_discrete(breaks = c(0,1))  
  xlab(label = "Sample")

plot

CodePudding user response:

This can be done using plot_ly. We have to convert the dataframe to a matrix and then run

as.matrix(as.numeric(clust.labs$value))->my.mat
colnames(my.mat)<-"KS.score"
rownames(my.mat)<-as.character(seq(1, length(my.mat[,1])))
cbind(my.mat, as.numeric(clust.labs$type))->my.mat
colnames(my.mat)<-c("KS.score", "Cluster")

plot_ly(z=my.mat, type="heatmap")
  • Related