0

I have a heat map which has a gradient from green to red and the values range from -2 to 2.

Now I want to give a value to each block of the heat map, is there any way we can do it?

Artem
  • 3,304
  • 3
  • 18
  • 41
  • 5
    Welcome to SO! Your question is unclear, please read and edit your question according to: [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – pogibas Sep 05 '18 at 18:51
  • `Ggplot2`'s `geom_tile()` could be helpful. However to have more help listen to @PoGibas – s__ Sep 05 '18 at 20:47
  • 1
    Possible duplicate of [heatmap with values (ggplot2)](https://stackoverflow.com/questions/14290364/heatmap-with-values-ggplot2) – Artem Sep 24 '18 at 22:15

1 Answers1

0

Please see below the approach using tidyverse package. dat is transformed to dat2 from wide format into narrow. Then geom_tile is plotted:

library(tidyverse)

# simulation
dat <- matrix(runif(81, -2, 2), ncol = 9)

## reshape data (tidy/tall form)
dat2 <- dat %>%
  tbl_df() %>%
  rownames_to_column("Var1") %>%
  gather(Var2, value, -Var1) %>%
  mutate(
    Var1 = factor(Var1, levels=1:10),
    Var2 = factor(gsub("V", "", Var2), levels=1:10))

## plot data
ggplot(dat2, aes(Var1, Var2)) +
  geom_tile(aes(fill = value)) + 
  geom_text(aes(label = round(value, 1))) +
  scale_fill_gradient(low = "green", high = "red") 

Output: graph

Artem
  • 3,304
  • 3
  • 18
  • 41