Search code examples
rggplot2heatmapgeom-text

R: How to overwrite text on heatmap from different data?


I've been asked to plot a heatmap to show tons of p-values in a simple way. The issue is that my boss wants to color the plot tiles with the log(p) values and overwrite them with the p-values. I managed to overwrite the values (log or p) on the heatmap, but can't do it with data from another dataframe. These are my data (shortened) and code:

covariate   HbA1c   FPG     Weight  SBP 
Age         0.867   0.928   0.001   0.001   
Sex         0.428   0.565   0.001   0.790   
BMI         0.491   0.435   0.001   0.001   


data <- read.delim(file="hm.txt", sep = "\t")    
df.log <- log(data[,2:12])         #the real data has 12 columns
df.log <- round(df.log, digits=3)

data.cov <- data$covariate
df <- cbind(data.cov,df.log)
df$data.cov = factor(df$data.cov, unique(df$data.cov))

df <- gather(df, key = "factor", value = "pvalue", -data.cov)
df$factor = factor(df$factor, unique(df$factor))

heatmap <- ggplot(df) +
  aes(x = data.cov, y = factor, fill = pvalue, label=pvalue) +
  geom_tile() +
  scale_fill_gradient(low = "#ffffff", high = "#595959",limits=c(-7,0)) + 
  theme_linedraw(base_size = 15) +
  geom_text() +
  theme(text=element_text(family="Roboto"))+
  theme(legend.position = "top", legend.key.width = unit(1.7, "cm"),legend.title=element_blank())+
  theme(axis.text.x=element_text(angle=90,vjust =0.2))
    

It is probably just a geom_text problem, but it seems I can't work it out successfully


Solution

  • This could be easily achieved by first reshaping your data to long and second adding a column with the log values. Doing so you end up with a df containing a column with the logged pvalues which you map on fill and a column with the original p-values which you map on label:

    library(tidyverse)
    
    data <- read.table(text = "covariate   HbA1c   FPG     Weight  SBP 
    Age         0.867   0.928   0.001   0.001   
    Sex         0.428   0.565   0.001   0.790   
    BMI         0.491   0.435   0.001   0.001", header = TRUE)   
    
    
    df <- gather(data, key = "factor", value = "pvalue", -covariate)
    df <- mutate(df, pvalue.log = log(pvalue))
                 
    ggplot(df) +
      aes(x = covariate, y = factor, fill = pvalue.log, label=pvalue) +
      geom_tile() +
      scale_fill_gradient(low = "#ffffff", high = "#595959",limits=c(-7,0)) + 
      theme_linedraw(base_size = 15) +
      geom_text() +
      #theme(text=element_text(family="Roboto"))+
      theme(legend.position = "top", legend.key.width = unit(1.7, "cm"),legend.title=element_blank())+
      theme(axis.text.x=element_text(angle=90,vjust =0.2))