I am plotting a heatmap and would like to label the yaxis by group in colors:
library(ggplot2)
library(tidyr)
library(tibble)
library(hrbrthemes)
library(dplyr)
# Volcano dataset
#volcano
# Heatmap
df <- volcano %>%
# Data wrangling
as_tibble() %>%
rowid_to_column(var="X") %>%
gather(key="Y", value="Z", -1) %>%
# Change Y to numeric
mutate(Y=as.numeric(gsub("V","",Y))) %>%
mutate(group=rep(1:3,each=1769))
ggplot(df,aes(X, Y, fill= Z)) +
geom_tile() +
theme(legend.position="none") +
scale_y_discrete(expand=c(0, 0)) +
scale_x_continuous(expand = c(0, 0))
Is there a way to do so?
Credit of this example and code goes to [https://www.r-graph-gallery.com/79-levelplot-with-ggplot2.html][2]

