简体   繁体   English

使用R中的ggplot生成热图表的每个类别的渐变颜色

[英]Gradient color for each category to generate a heatmap table using ggplot in R

I am trying to generate a heatmap table using ggplot in R . 我正在尝试使用R中的 ggplot生成热图表。 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 plot looks like this: 情节看起来像这样:

热图表

The colors should be gradient in each category. 每个类别的颜色应该是渐变的。 I really appreciate any help. 我非常感谢任何帮助。

Assuming I'm interpreting your question correctly, I changed the variable that is mapped to fill. 假设我正确地解释了你的问题,我改变了映射到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---eg how does this value compare to all others in group A. 而不是原始值,我按Var2分组并计算相对值,因此每个值都与其组中的其他值进行比较 - 例如,该值与A组中的其他值相比如何。

I also took out the 3 from your geom_text aes because it seemed like a typo. 我也从你的geom_text aes取出3,因为它看起来像是一个错字。 That lets the text geoms each take the same positions as the corresponding tiles. 这使得文本geoms每个都采用与相应tile相同的位置。

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. 这种方法的一个缺点是图例上的标签现在没有很多含义,或者至少需要一些解释来说明值是针对它们的组进行缩放的。

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. 编辑 :我将颜色渐变的中点更改为0.5而不是之前的0.05,因此缩放值后仍会显示最小颜色。

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

Created on 2018-04-16 by the reprex package (v0.2.0). reprex包 (v0.2.0)于2018-04-16创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM