ggplot2每列热图 [英] Heat map per column with ggplot2

查看:103
本文介绍了ggplot2每列热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个R脚本:

  tableau<  -  read.table(
text = $ (b)
a 1.88 0.15 0.60 10.00 90.00
b 2.05 0.23 0.51 55.00 80.00
c 2.09 0.29 0.40 58.00 88.00
d 2.07 0.52 0.36 80.00 84.00
e 2.13 0.30 0.27 7.00 90.00,
header = TRUE)

library(plyr)
library(reshape)
library(ggplot2)
library(比例)
tableau.m< - melt(tableau)
tableau.m< - ddply(tableau.m,。(变量),transform,rescale = rescale(value) )

(p <-ggplot(tableau.m,aes(variable,Net))+
geom_tile(aes(fill = rescale),color =white)+
scale_fill_gradient(low =white,high =steelblue))

base_size< - 9
p + theme_grey(base_size = base_size)+
labs(x = ,y =)+ scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0))+
theme (legend.position =none,axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size * 0.8,angle = 0,
hjust = 0,color = grey50))

tableau.s< - ddply(tableau.m,。(variable),transform,rescale = scale(value))

last_plot()% +%tableau.s

我得到这个图:




其中较深的蓝色代表较高的值,白色代表较低的值。白色代表较低的值。



如果可能的话,我可以如何更改此代码:


  1. 表中的值显示在矩阵图的每个对应单元中?
  2. 热图的范围不计算在整个矩阵,而是为每一列。因此,对于每个类别:B,C,D,E(e)和F(f),白色表示此列的较低值,而较深的蓝色表示列的较高值?

感谢!

解决方案

c> value 作为每个单元格的文本标签,您可以使用 geom_text

  p < -  ggplot(tableau.m,aes(variable,Net))+ 
geom_tile(aes(fill = rescale),color =white)+
scale_fill_gradient(low =white,high =steelblue)+
geom_text(aes(label = value))

#添加主题格式
base_size< - 9
p + theme_grey(base_size = base_size)+
labs(x =,y =)+ scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0))+
theme(legend.position =none,axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size * 0.8,
angle = 0,hjust = 0,co你的第二个问题,你现在的代码已经考虑到了这一点。变量 rescale 分别缩放每列,因为您已经执行了由变量分组的操作。由于 rescale fill 变量,所以为了设置颜色值,每列值都从0重新调整为1。您不需要 tableau.s ... last.plot ... 代码。



以下是在运行上面的代码之后,情节看起来像。请注意,在每列中,最低值是白色,最高值是钢蓝色。 (您可能希望将边框颜色从白色更改为gray90,以便相邻白色方块之间会有边框):

< img src =https://i.stack.imgur.com/JaLL6.pngalt =在这里输入图片描述>


I'm using this R script:

tableau <- read.table(
  text = 
    "Net    B   C   D   E.(e)   F.(f)
a   1.88    0.15    0.60    10.00   90.00
b   2.05    0.23    0.51    55.00   80.00
c   2.09    0.29    0.40    58.00   88.00
d   2.07    0.52    0.36    80.00   84.00
e   2.13    0.30    0.27    7.00    90.00",
  header = TRUE)

library(plyr)
library(reshape)
library(ggplot2)
library(scales)
tableau.m <- melt(tableau)
tableau.m <- ddply(tableau.m, .(variable), transform, rescale = rescale(value))

(p <- ggplot(tableau.m, aes(variable, Net)) + 
    geom_tile(aes(fill = rescale), colour = "white") + 
    scale_fill_gradient(low = "white", high = "steelblue"))

base_size <- 9
p + theme_grey(base_size = base_size) + 
  labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
  scale_y_discrete(expand = c(0, 0)) + 
  theme(legend.position = "none", axis.ticks = element_blank(), 
        axis.text.x = element_text(size = base_size * 0.8, angle = 0, 
                                   hjust = 0, colour = "grey50"))

tableau.s <- ddply(tableau.m, .(variable), transform, rescale = scale(value))

last_plot() %+% tableau.s

And I obtain this plot:

Where darker blue means higher values and white means lower values.

How, if possible, could I change this code so that:

  1. the values from the table are displayed in each corresponding cell of the matrix plot?
  2. the range of the heat map isn't calculated on the whole matrix, but rather for each column. So that, for each category: B, C, D, E(e), and F(f), white means the lower value for this column, and darker blue means the higher value of the column?

Thanks!

解决方案

To add value as a text label to each cell, you can use geom_text:

p <- ggplot(tableau.m, aes(variable, Net)) + 
      geom_tile(aes(fill = rescale), colour = "white") + 
      scale_fill_gradient(low = "white", high = "steelblue") +
      geom_text(aes(label=value))

# Add the theme formatting
base_size <- 9
p + theme_grey(base_size = base_size) + 
  labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
  scale_y_discrete(expand = c(0, 0)) + 
  theme(legend.position = "none", axis.ticks = element_blank(), 
        axis.text.x = element_text(size = base_size * 0.8, 
                                   angle = 0, hjust = 0, colour = "grey50"))

For your second question, your current code already takes care of that. The variable rescale scales each column separately, because you've performed the operation grouped by variable. Since rescale is the fill variable, each column's values are rescaled from zero to one for the purposes of setting color values. You don't need the tableau.s ... last.plot... code.

Here's what the plot looks like after running the code above. Note that in each column, the lowest value is white and the highest value is steel blue. (You might want to change the border color from "white" to, say, "gray90", so that there will be a border between adjacent white squares):

这篇关于ggplot2每列热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆