Home > Software engineering >  Heatmap using geom_tile in a loop/function then save the output figures
Heatmap using geom_tile in a loop/function then save the output figures

Time:09-05

I would like to make heatmaps using the following data:

dt <- data.frame(
  h = rep(LETTERS[1:7], 7), 
  j = c(rep("A", 7), rep("B", 7), rep("C", 7), rep("D", 7), rep("E", 7), rep("F", 7), rep("G", 7)),
  Red = runif(7, 0, 1), 
  Yellow = runif(7, 0, 1), 
  Green = runif(7, 0, 1),
  Blue = runif(7, 0, 1),
  Black = runif(7, 0, 1)
)

For each of the heatmaps, the x and y axes stay as the first 2 columns of df. The values that fill in each heatmap will be each of the remaining columns, e.g., Red, Yellow, ...

I borrowed this example to produce the following code:

loop = function(df, x_var, y_var, f_var) {
  
  ggplot(df, aes(x = .data[[x_var]], y = .data[[y_var]], fill = .data[[f_var]]))   
    geom_tile(color = "black")  
    scale_fill_gradient(low = "white", high = "blue")  
    geom_text(aes(label = .data[[f_var]]), color = "black", size = 4)  
    coord_fixed()  
    theme_minimal()  
    labs(x = "",
         y = "",
         fill = "R", # Want the legend title to be each of the column names that are looped
         title = .data[[f_var]])
  
  ggsave(a, file = paste0("heatmap_", f_var,".png"), device = png, width = 15, height = 15, units = "cm")
}


plot_list <- colnames(dt)[-1] %>% 
  map( ~ loop(df = dt, 
              x_var = colnames(dt)[1], 
              y_var = colnames(dt)[2], 
              f_var = .x))

# view all plots individually (not shown)
plot_list

Problems I encountered when ran this chunk of code:

  1. Error: Discrete value supplied to continuous scale
  2. Step ggsave didn't work. I would like to save each plot by the names of the changing columns.

CodePudding user response:

There are some minor issues with your code. You get the first error as you included the second column of your dataset (which is a categorical, i.e. discrete variable) in the loop. Second, title = .data[[f_var]] will not work. Simply use title = f_var to add the variable name as the title. Finally, you are trying to save an object called a which however is not defined in your code, i.e. you have to assign your plot to a variable a and to return the plot I added a return(a):

set.seed(123)

library(ggplot2)
library(purrr)

loop = function(df, x_var, y_var, f_var) {
  a <- ggplot(df, aes(x = .data[[x_var]], y = .data[[y_var]], fill = .data[[f_var]]))   
    geom_tile(color = "black")  
    scale_fill_gradient(low = "white", high = "blue")  
    geom_text(aes(label = .data[[f_var]]), color = "black", size = 4)  
    coord_fixed()  
    theme_minimal()  
    labs(x = "",
         y = "",
         fill = "R", # Want the legend title to be each of the column names that are looped
         title = f_var)
  
  ggsave(a, file = paste0("heatmap_", f_var,".png"), device = png, width = 15, height = 15, units = "cm")
  
  return(a)
}

plot_list <- colnames(dt)[-c(1, 2)] %>% 
  map( ~ loop(df = dt, 
              x_var = colnames(dt)[1], 
              y_var = colnames(dt)[2], 
              f_var = .x))

# view all plots individually (not shown)
plot_list[c(1, 5)]
#> [[1]]

#> 
#> [[2]]

  • Related