Search code examples
rggplot2time-seriesheatmapgeom-bar

How do I create a one-row barplot/heatmap in which I can plot the values of my timepoints?


It's difficult to explain the plot challenge I'm facing. If someone has a better idea on how to write up the question, I'll change it.

My original R data frame is as follows:

structure(list(Gene = c("Actl6a_1", "Actl6a_1", "Actl6a_1", "Actl6a_2", 
"Actl6a_2", "Actl6a_2", "Cebpa_1", "Cebpa_1", "Cebpa_1", "Cebpa_2", 
"Cebpa_2", "Cebpa_2", "Cebpa_3", "Cebpa_3", "Cebpa_3"), Time.point = c("T1", 
"T2", "T3", "T1", "T2", "T3", "T1", "T2", "T3", "T1", "T2", "T3", 
"T1", "T2", "T3"), Lin_score = c(6.62286740798228, 0.725529973292214, 
1.45277082126036, 7.58080470281066, 2.23746335327345, 0.912864932488962, 
4.57106792646335, 3.45482367414755, 2.00747227550368, 9.5569448291242, 
1.19180459071481, 4.84044979652114, 1.77470104454199, 2.36007115400718, 
7.3450372293307), Deple_Score = c(24.4059246224385, 1.4681178665796, 
3.80340636569322, 5.35461394207936, 1.54776909412289, 1.35977608722976, 
3.94031620390256, 1.03991947913739, 1.53296584765398, 3.91215174555478, 
0.180873007077615, 3.7132386172666, 1.61370679951173, 2.77858577375842, 
4.07592722173201)), class = "data.frame", row.names = c(NA, -15L)) here

I've have scaled the df with:

df <- as.data.frame(apply(table_stack[, 3:4], 2, scale))
df$Gene <- table_stack$Gene
df$Time.point <- table_stack$Time.point

So, I end up with the following R data frame

dput(df)
structure(list(Lin_score = c(1.00804079166467, -1.07987355614156, 
-0.822398616374401, 1.34719232306691, -0.544583277782937, -1.01354882002391, 
0.281614396070452, -0.11358469434566, -0.626010138033161, 2.04683201109546, 
-0.914792030026438, 0.376987312706364, -0.708421293620525, -0.501174775363001, 
1.26372036710774), Deple_Score = c(3.49732264514564, -0.443296466495975, 
-0.0421036851306225, 0.224387278312042, -0.429612719271858, -0.461909130332555, 
-0.0185831484567453, -0.51685915568804, -0.432155855002777, -0.0234216844168561, 
-0.66443974094702, -0.0575941265876234, -0.418284897706412, -0.21816356973797, 
0.00471425631676927), Gene = c("Actl6a_1", "Actl6a_1", "Actl6a_1", 
"Actl6a_2", "Actl6a_2", "Actl6a_2", "Cebpa_1", "Cebpa_1", "Cebpa_1", 
"Cebpa_2", "Cebpa_2", "Cebpa_2", "Cebpa_3", "Cebpa_3", "Cebpa_3"
), Time.point = c("T1", "T2", "T3", "T1", "T2", "T3", "T1", "T2", 
"T3", "T1", "T2", "T3", "T1", "T2", "T3")), row.names = c(NA, 
-15L), class = "data.frame")

I'd like to plot a continuous-color-scaled "barplot" or "tile", in which I can indicate where does my Time points fall into. For each of the Genes. Ideally each gene would have its own "heatmap" or its own barplot. But for each Gene, I am expecting to plot both scores.

Something similar to: One row barplot/heatmap

The best I've have done so far is:

ggplot(df, aes(x = Time.point, y =1 , fill = Lin_score)) +
geom_tile+
ylab("")

So, I don't know how code in R this simple plot and that it looks like the image I've attached. I need it because I have to do it programatically for number (hundred) of different genes. I've scaled the data based on columns, I know I should scale only based on each genes' scores, otherwise it's misleading.

Many thanks


Solution

  • Does this work for you?

     library(tidyverse)
    
      df <- structure(list(Lin_score = c(1.00804079166467, -1.07987355614156, 
                                         -0.822398616374401, 1.34719232306691, -0.544583277782937, -1.01354882002391, 
                                         0.281614396070452, -0.11358469434566, -0.626010138033161, 2.04683201109546, 
                                         -0.914792030026438, 0.376987312706364, -0.708421293620525, -0.501174775363001, 
                                         1.26372036710774), Deple_Score = c(3.49732264514564, -0.443296466495975, 
                                                                            -0.0421036851306225, 0.224387278312042, -0.429612719271858, -0.461909130332555, 
                                                                            -0.0185831484567453, -0.51685915568804, -0.432155855002777, -0.0234216844168561, 
                                                                            -0.66443974094702, -0.0575941265876234, -0.418284897706412, -0.21816356973797, 
                                                                            0.00471425631676927), Gene = c("Actl6a_1", "Actl6a_1", "Actl6a_1", 
                                                                                                           "Actl6a_2", "Actl6a_2", "Actl6a_2", "Cebpa_1", "Cebpa_1", "Cebpa_1", 
                                                                                                           "Cebpa_2", "Cebpa_2", "Cebpa_2", "Cebpa_3", "Cebpa_3", "Cebpa_3"
                                                                            ), Time.point = c("T1", "T2", "T3", "T1", "T2", "T3", "T1", "T2", 
                                                                                              "T3", "T1", "T2", "T3", "T1", "T2", "T3")), row.names = c(NA, 
                                                                                                                                                        -15L), class = "data.frame")
    
      df <- df %>% 
        pivot_longer(cols =c('Lin_score','Deple_Score'),values_to = 'Score')
      df %>% 
        ggplot(aes(x = Score, fill = Score)) +
        geom_point(y = 1,color = 'green') + 
        geom_point(y = 0.9,color = 'green',fill = 'green',pch = 25) +
        geom_text(y = 1.2, aes(label = Time.point),color = 'green') +
        ylim(0,1.5) +
        geom_tile(data = tibble(Score = seq(min(df$Score),max(df$Score),0.01)),aes(y = 0.5),height = 0.5) +
        facet_wrap(Gene ~ name) +
        theme_void()
    

    enter image description here

    You will obviously have to play around with the scaling a bit to get it exactly as you want it...