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