r - ggplot中的热图,每组不同的颜色

标签 r ggplot2 colors heatmap

我正在尝试在 ggplot 中生成热图。我希望每个组都有不同的颜色渐变,但不知道该怎么做。我当前的代码如下所示:

## dummy data -----------------------------------------------------------------------------
data <- data.frame(
  group = sample(c("Direct Patient Care", "Indirect Patient Care", "Education", "Rounds", "Handoff", "Misce"), 30, replace = T),
  pct = rnorm(30, mean = 50, sd = 8)
)

## generate group id 
data <- data %>%
  group_by(group) %>%
  mutate(id = row_number())

data$grpid <- with(data, ifelse(group == "Direct Patient Care", 1, ifelse(group == "Indirect Patient Care", 2,
                                                                        ifelse(group == "Education", 3, 
                                                                               ifelse(group == "Rounds", 4,
                                                                                      ifelse(group == "Handoff", 5,6 ))))))

## draw graph ------------------------------------------------------------------------------                                                                                                                                                                           
library(ggplot2)

p <- ggplot(data, aes(x=id, y=group, fill = pct)) +
  theme(panel.background = element_rect(fill = "white", colour = "grey50"), aspect.ratio = 0.4) +
  theme(panel.grid.major = element_blank(),  
        panel.grid.minor = element_blank()
  )+
  # guides(fill = guide_legend("Time, %")) +
  geom_tile() +
  scale_x_continuous (name = " ", breaks = seq(1, 8, by = 1)) +
  scale_y_discrete(name = " ") +
  theme(axis.text.x = element_text(angle = 0,hjust = 1,vjust = 1), plot.title = element_text(hjust = 0.5) ) +
  ggtitle("Heatmap of time spent doing activities across 194 shifts")


p + scale_fill_gradient2(low = "white", high = "red", limits = c(0, 80), breaks = c(0, 10, 20, 30, 40, 50, 60, 70), guide = guide_legend("Time, %"))  ## change the color theme ##

结果图如下所示: enter image description here

如何更改每个组的颜色主题,例如“Rounds”为红色、“Misce”为蓝色、“Handoff”为绿色等...

非常感谢!

最佳答案

您可以通过在数据中创建自己的重新缩放值,然后稍微“破解” alpha 美学和 fill 美学来做到这一点:

library(tidyverse)

data %>%
  group_by(group) %>%
  mutate(rescale = scales::rescale(pct)) %>%
  ggplot(., aes(x = factor(id), y = group)) +
  geom_tile(aes(alpha = rescale, fill = group), color = "white") +
  scale_alpha(range = c(0.1, 1))

首先我们创建一个名为 rescale 的新列,它 rescalepct01 然后你强制 scale_alpha(range = c(0, 1)) [注意,在这种情况下,我使用 c(0.1, 1) 这样你仍然可以“看到”零分。

最后,您可能想要删除指南:

data %>%
  group_by(group) %>%
  mutate(rescale = scales::rescale(pct)) %>%
  ggplot(., aes(x = factor(id), y = group)) +
  geom_tile(aes(alpha = rescale, fill = group), color = "white") +
  scale_alpha(range = c(0.1, 1)) +
  theme(legend.position = "none")

Plot

注意通过使用 aes(x = factor(id)... 您可以手动设置您的 x-axis 因为在这种情况下,您似乎想将其视为因子不是数字比例。

最后,如果你真的想要花哨,你可以将 axis.text.y 颜色双重编码为​​你的 factor 级别的颜色(即, data$group) 变量:

data %>%
  group_by(group) %>%
  mutate(rescale = scales::rescale(pct)) %>%
  ggplot(., aes(x = factor(id), y = group)) +
  geom_tile(aes(alpha = rescale, fill = group), color = "white") +
  scale_alpha(range = c(0.1, 1)) +
  theme(legend.position = "none",
        axis.text.y = element_text(color = scales::hue_pal()(length(levels(data$group)))),
        axis.ticks = element_blank()) +
  labs(x = "", y = "")

Fancy Plot

关于r - ggplot中的热图,每组不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50338361/

相关文章:

r - 如何处理对数图中的零

大小映射到变量时的ggplot2 geom_point图例

Java:如何在子类中设置颜色?

delphi - 为什么我的自定义绘制 ListView 上会出现白色的列分隔符?

regex - 一次在字符串中的多个位置插入一个字符

r - 插入符号中的 closeZeroVar 函数

r - plyr::mapvalues() 函数

r - 带 R 的两个变量的多项式回归

r - 绘制嵌套维恩图

javascript - javascript中的伪随机颜色生成器