每个类别的渐变颜色,以使用R中的ggplot生成热图表 [英] Gradient color for each category to generate a heatmap table using ggplot in R

查看:114
本文介绍了每个类别的渐变颜色,以使用R中的ggplot生成热图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 R 中的ggplot生成热图表.我的数据看起来像这样:

I am trying to generate a heatmap table using ggplot in R. My data looks this:

dt <- cbind(rbinom(1:10,100,0.9), rbinom(1:10,100,0.5), rbinom(1:10,100,0.2), rbinom(1:10,100,0.05)
colnames(dt) <- c("A","B","C","D")

我想为每个类别使用不同的渐变颜色,以强调每个值的重要性.由于值的范围较大,因此较小的数字将被着色为几乎相同的颜色.这就是为什么我要使用不同的颜色.我的代码在这里:

I want to use different gradient colors for each category to emphasize the importance of each value. Since the range of values is large, the smaller number will be colored almost same. That's why, I want to use different colors. My code is here:

library(dplyr)
library(ggplot2)
library(reshape2)
library(scales)

dt <- cbind(rbinom(1:10,100,0.9), rbinom(1:10,100,0.5), rbinom(1:10,100,0.2), rbinom(1:10,100,0.01))
colnames(dt) <- c("A","B","C","D")

dt <- melt(dt)

dt %>%
    ggplot(aes(Var2,Var1)) +
    geom_tile(aes(fill = value), colour = "white") +
    geom_text(aes(fill = dt$value, label = dt$value, 3), size = 4) +
    scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 0.05) +
theme(panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.background = element_rect(fill = "white"),
    axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, size = 10, face = "bold"),
    plot.title = element_text(size = 20, face = "bold"),
    axis.text.y = element_text(size = 10, face = "bold")) +
ggtitle("Heatmap Table") +
theme(legend.title = element_text(face = "bold", size = 14)) +
scale_x_discrete(name = "") +
scale_y_discrete(name = "") +
labs(fill = "Heatmap") 

情节看起来像这样:

每个类别中的颜色均应为渐变色.我非常感谢您的帮助.

The colors should be gradient in each category. I really appreciate any help.

推荐答案

假设我正确地解释了您的问题,我更改了映射为fill的变量.我用Var2分组并计算了一个相对值,而不是原始值,因此将每个值与其组中的其他值进行比较-例如.该值与A组中的所有其他值相比如何?

Assuming I'm interpreting your question correctly, I changed the variable that is mapped to fill. Instead of the raw value you have, I grouped by Var2 and calculated a relative value, so each value is scaled compared to the other values in its group---e.g. how does this value compare to all others in group A.

我也从您的geom_text aes中取出了3,因为它看起来像是一个错字.这样一来,文本几何图形就可以与相应的图块占据相同的位置.

I also took out the 3 from your geom_text aes because it seemed like a typo. That lets the text geoms each take the same positions as the corresponding tiles.

这种方法的一个缺点是,图例上的标签现在没有太多含义,或者至少需要一些解释才能说明值是根据组进行缩放的.

One drawback to this approach is that the labels on the legend don't have a lot of meaning now, or at least would need some explanation to say that values are scaled against their group.

编辑:我将颜色渐变的中点更改为0.5,而不是之前的0.05,因此在缩放值后仍显示最小颜色.

Edit: I'm changing the midpoint in the color gradient to 0.5 instead of the previous 0.05, so the minimum colors still show after scaling values.

library(dplyr)
library(ggplot2)
library(reshape2)
library(scales)

dt <- cbind(rbinom(1:10,100,0.9), rbinom(1:10,100,0.5), rbinom(1:10,100,0.2), rbinom(1:10,100,0.01))
colnames(dt) <- c("A","B","C","D")

dt <- melt(dt)

dt %>%
    group_by(Var2) %>%
# make a variable for the value scaled in relation to other values in that group
    mutate(rel_value = value / max(value)) %>% 
    ggplot(aes(Var2,Var1)) +
    geom_tile(aes(fill = rel_value), colour = "white") +
# take out dt$...
# take out 3 from aes
    geom_text(aes(label = value), size = 4) +
    scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 0.5) +
    theme(panel.grid.major.x = element_blank(),
                panel.grid.minor.x = element_blank(),
                panel.grid.major.y = element_blank(),
                panel.grid.minor.y = element_blank(),
                panel.background = element_rect(fill = "white"),
                axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, size = 10, face = "bold"),
                plot.title = element_text(size = 20, face = "bold"),
                axis.text.y = element_text(size = 10, face = "bold")) +
    ggtitle("Heatmap Table") +
    theme(legend.title = element_text(face = "bold", size = 14)) +
    scale_x_discrete(name = "") +
    scale_y_discrete(name = "") +
    labs(fill = "Heatmap")                      

reprex软件包(v0.2.0)创建于2018-04-16.

Created on 2018-04-16 by the reprex package (v0.2.0).

这篇关于每个类别的渐变颜色,以使用R中的ggplot生成热图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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