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-boun...@r-project.org> On Behalf Of p...@philipsmith.ca
Sent: Monday, December 9, 2024 7:56 PM
To: R-help@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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to