Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heat map when the color is scaled rowwise

Tags:

r

ggplot2

heatmap

Hello stackoverflowers!

As the title is indicating I want to make a heat map but the color scaling should be across each row and individual of each other.

The following example will indicate what I want exactly:

library(tidyverse)
library(data.table)
data_heat <- expand.grid(y = letters[seq( from = 1, to = 6 )],x = LETTERS[ seq( from = 1, to = 10 )]) %>% as.data.table()
data_heat %>% setkey(y)
data_heat[, fill_value := seq(from = 1,to =  nrow(data_heat))]


data_heat%>% ggplot(aes(x = x, y = y)) + 
  geom_tile(aes(fill = fill_value), colour = "black") + scale_fill_gradient(low = "green",
                                                                              high = "red") + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +    geom_text(aes(label = fill_value)) 

this is going to generate:

basic heat map

while what I want is that the right side of the graph is red because there are the largest values per row.

like image 471
George Sotiropoulos Avatar asked Apr 05 '18 13:04

George Sotiropoulos


People also ask

Should you use a heatmap color scale?

Yet, like anything else, the heatmap color scale can backfire when mistakenly chosen. Either it causes confusion or dizziness, or “heat” of annoyance to your readers. We really do not want to turn a heatmap into a literal “heat” map in this way, do we? Here’s a couple of dos and don’ts for a heatmap color scale.

What do the columns in a heat map represent?

Each cell in the heatmap is associated with one row in the data table. The first two columns specify the ‘coordinates’ of the heat map cell, while the third column indicates the cell’s value. … … … Color is a core component of this chart type, so it’s worth making sure that you choose an appropriate color palette to match the data.

Why switch between color scales of a choropleth or heat map?

In a nutshell: switching between color scales of a Choropleth or Heat Map helps you to understand your data. Whilst Ron was developing his great palette of color schemes for using them on a Choropleth Map, they are equally helpful for a much simpler visualization technique: a Heat Map of a table (or range) of numbers.

Why is the color of zero in a heatmap white?

@shujaa I think now it is better. The color is white because your heatmap command scales the rows before drawing the heatmap. So the row c (0.3, 0.3, 0.3) becomes a row of zeroes and zero is denoted by white in this color scheme.


2 Answers

Solution:

Scale values using function scale() per each group (data_heat$y).

Code:

library(ggplot2)
library(data.table)
data_heat[, fillScaled := scale(fill_value), y]
ggplot(data_heat, aes(x, y)) + 
    geom_tile(aes(fill = fillScaled), colour = "black") + 
    scale_fill_gradient(low = "green", high = "red") + 
    geom_text(aes(label = fill_value)) +
    theme(axis.text.x = element_text(angle = 30, hjust = 1))

Result:

enter image description here

Data (data_heat):

structure(list(y = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L), .Label = c("a", "b", "c", "d", "e", "f"), class = "factor"), 
    x = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J"), class = "factor"), fill_value = 1:60), .Names = c("y", 
"x", "fill_value"), out.attrs = structure(list(dim = structure(c(6L, 
10L), .Names = c("y", "x")), dimnames = structure(list(y = c("y=a", 
"y=b", "y=c", "y=d", "y=e", "y=f"), x = c("x=A", "x=B", "x=C", 
"x=D", "x=E", "x=F", "x=G", "x=H", "x=I", "x=J")), .Names = c("y", 
"x"))), .Names = c("dim", "dimnames")), class = c("data.table", 
"data.frame"), row.names = c(NA, -60L))
like image 103
pogibas Avatar answered Oct 03 '22 23:10

pogibas


A more basic data manipulation approach:

library(dplyr)
data_heat_summary <- data_heat %>%
  group_by(y) %>%
  summarize(mx = max(fill_value), mn = min(fill_value)) 

data_heat %>% 
  inner_join(data_heat_summary) %>%
  mutate(p = (fill_value - mn ) / (mx - mn)) %>%
  ggplot(aes(x = x, y = y)) + 
  geom_tile(aes(fill = p), colour = "black") + 
  scale_fill_gradient(low = "green", high = "red") + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +    geom_text(aes(label = fill_value)) 
like image 37
Andrew Lavers Avatar answered Oct 04 '22 00:10

Andrew Lavers