当颜色按行缩放时的热图

时间:2018-04-05 13:44:51

标签: r ggplot2 heatmap

你好stackoverflowers!

由于标题表示我想制作热图,但颜色缩放应该跨越每行和彼此的个体。

以下示例将指出我想要的内容:

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)) 

这将产生:

basic heat map

虽然我想要的是图表的右侧是红色的,因为每行有最大的值。

2 个答案:

答案 0 :(得分:5)

解决方案:

使用每个组scale())的函数data_heat$y缩放值。

代码:

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))

结果:

enter image description here

数据(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))

答案 1 :(得分:1)

更基本的数据处理方法:

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))