[R] Heat maps containing two types of variables

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Tue Dec 10 14:35:26 CET 2024


You will need to add code to tell R which variables to plot in reverse color order. In this code, I chose variable B to plot in reverse order. 


library(ggplot2)
library(tidyr)
library(dplyr)

dat <- data.frame(
  date = seq.Date(as.Date("2024-01-01"), as.Date("2024-06-01"), by = "month"),
  A = c(1, 3, 3, 4, 2, 6),
  B = c(3, 5, 6, 4, 8, 9),
  C = c(10, 8, 17, 19, 26, 22)
)
normalize <- function(x) {
  (x - min(x)) / (max(x) - min(x))
}
dat_long <- dat %>%
  mutate(across(2:4, normalize)) %>% 
  pivot_longer(cols = A:C, names_to = "variable", values_to = "norm_value") %>%
  left_join(
    dat %>%
      pivot_longer(cols = A:C, names_to = "variable", values_to = "value"),
    by = c("date", "variable")
  )

dat_long <- dat_long %>%
  mutate(adjusted_norm_value = ifelse(variable == "B", 1 - norm_value, norm_value))

heatmap <- ggplot(dat_long, aes(x = date, y = variable, fill = adjusted_norm_value)) +
  geom_tile() +
  geom_text(aes(label = as.character(value)), color = "black", size = 2.5) +
  labs(title = "REPREX", x = "", y = "") +
  scale_fill_gradient2(low = "#E94A26", mid = "white", high = "#A1D385", midpoint = 0.5) +
  scale_x_continuous(
    breaks = seq.Date(as.Date("2024-01-01"), as.Date("2024-06-01"), by = "month"),
    labels = function(x) format(x, "%b\n%Y"),
    position = "top"
  ) +
  theme(legend.position = "none")

heatmap

-----Original Message-----
From: phil using philipsmith.ca <phil using philipsmith.ca> 
Sent: Tuesday, December 10, 2024 7:59 AM
To: Ebert,Timothy Aaron <tebert using ufl.edu>
Cc: R-help using r-project.org
Subject: Re: [R] Heat maps containing two types of variables

[External Email]

Thank you for the suggestion. I tried it, but could not get it to work.
When I added a second ggplot statement, I hit an error saying that one cannot add a ggplot to a ggplot object. So I added a second geom_tile statement instead. That worked, except that it warned that since a scale for fill was already present, the new fill would replace the old one. In other words, the colour scale was changed not just for the target, B, but also for the other two variables. So I am still searching for a solution.

Philip

On 2024-12-09 23:33, Ebert,Timothy Aaron wrote:
> What happens if you switch the colors in this line:
>    scale_fill_gradient2(low = "#E94A26", mid = "white", high = 
> "#A1D385", midpoint = 0.5) + to be the following
>    scale_fill_gradient2(low = "# A1D385", mid = "white", high = "# 
> E94A26", midpoint = 0.5) +
>
> That said, a red-green heat map may be unhelpful to color blind people.
>
> So then you need two ggplot statements, one with each
> scale_fill_gradient2 and then specify which version to plot for each 
> variable.
>
> Tim
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of 
> phil using philipsmith.ca
> Sent: Monday, December 9, 2024 7:56 PM
> To: R-help using r-project.org
> Subject: [R] Heat maps containing two types of variables
>
> [External Email]
>
> I am working with a heat map, as in the REPREX below. The code works 
> fine as long as "bigger numbers imply greener and smaller numbers 
> imply redder". These are time series where bigger numbers are 
> "better", like total employment for example. But I also have cases 
> within the heat map where "bigger numbers imply redder and smaller numbers imply greener".
> These are time series where bigger numbers are "worse", like total 
> unemployment for example. So suppose column B in dat is of the second 
> type, i.e. "bigger numbers imply redder and smaller numbers imply 
> greener". I would like the colour coding to be the reverse of what it 
> is for columns A and C. How can I modify the code to accomplish this? 
> I have tried different approaches with no success. Thanks for your help.
> Philip
>
> # REPREX
> library(ggplot2)
> library(tidyr)
> library(dplyr)
> dat <- data.frame(
>
> date=seq.Date(as.Date("2024-01-01"),as.Date("2024-06-01"),by="month"),
>    A=c(1,3,3,4,2,6),
>    B=c(3,5,6,4,8,9),
>    C=c(10,8,17,19,26,22)
> )
> dat_long <- 
> pivot_longer(dat,2:4,names_to="variable",values_to="value")
> normalize <- function(x) { y <- (x-min(x))/(max(x)-min(x)) } dat_norm
> <- mutate(dat,across(2:4,normalize)) dat_long_norm <-
> pivot_longer(dat_norm,2:4,names_to="variable",values_to="norm_value")
> dat_long <- inner_join(dat_long,dat_long_norm,by=c("date","variable"))
> heatmap <- ggplot(dat_long, aes(x = date, y =
> variable,fill=norm_value))
> +
>    geom_tile() +
>    geom_text(aes(label = as.character(value)),
>      color = "black", size = 2.5) +
>    labs(title="REPREX",x="",y="")+
>    scale_fill_gradient2(low = "#E94A26", mid = "white", high = 
> "#A1D385", midpoint = 0.5) +
>    scale_x_continuous(breaks=seq.Date(as.Date("2024-01-01"),
>      as.Date("2024-06-01"),by="month"),
>      labels=function(x) format(x,"%b\n%Y"),position="top")+
>    theme(legend.position="none")
> heatmap
> ggsave("REPREXHeatmap.png",heatmap,height=3.5,width=4.9,dpi=200)
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl.edu
> %7C1a08885d2b61452a502308dd191a7c05%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638694323735326272%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGki
> OnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ
> %3D%3D%7C0%7C%7C%7C&sdata=AyPt4o4LzEivT0D6JfeB%2Bav7N2wnZM%2BxPkrLF9B3
> LvI%3D&reserved=0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.
> r-project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu%7C1
> a08885d2b61452a502308dd191a7c05%7C0d4da0f84a314d76ace60a62331e1b84%7C0
> %7C0%7C638694323735345552%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRy
> dWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%
> 3D%7C0%7C%7C%7C&sdata=bH1aZLifelBywJO42%2BWGcyU1O6tmJbQi3DPC%2FjxWIKo%
> 3D&reserved=0 and provide commented, minimal, self-contained, 
> reproducible code.



More information about the R-help mailing list