mandy
mandy

Reputation: 503

heatmap in ggplot, different color for each group

I am trying to produce a heatmap in ggplot. I want each group to have different color gradient, but don't know how to do that. My current code looks like this:

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

And the resulting figure looks like this: enter image description here

How can I change the color theme for each group, like red for 'Rounds', blue for 'Misce', green for 'Handoff' etc...

Many thanks!

Upvotes: 4

Views: 5516

Answers (1)

JasonAizkalns
JasonAizkalns

Reputation: 20463

You can do this by creating your own rescaled value in your data and then slightly "hacking" the alpha aesthetic combined with the fill aesthetic:

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

First we create a new column called rescale which rescales the pct from 0 to 1 then you force the scale_alpha(range = c(0, 1)) [note, in this case I used c(0.1, 1) so that you can still "see" the zero points.

Finally, you probably want to remove the guides:

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

N.B. by using aes(x = factor(id)... you can get around manually setting your x-axis since in this case it appears you want to treat it as a factor not a numeric scale.

Finally, if you really want to get fancy, you could double-encode the axis.text.y colors to that of the levels of your factor (i.e., data$group) variable:

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

Upvotes: 10

Related Questions