I have a made a heatmap in R using ggplot.
Example
# Libraries
library(tidyverse)
# Create data frame
df <- data.frame(test = rep(c("testA-01", "testA-02", "testA-03", "testB-01", "testB-02", "testB-03", "testC-01", "testC-02", "testC-03"),3),
time = c( rep(0,9), rep(1, 9), rep(2, 9) ),
score = sample(1:10, 27, replace = TRUE) )
# Create heatmap
ggplot(data = df, mapping = aes(x = time, y = test)) +
geom_tile(mapping = aes(fill = score, width=0.9, height=0.9)) +
scale_fill_gradientn(limits = c(1,10), colours=c("grey95", "grey40", "red"), na.value = "white" ) +
scale_y_discrete(name = "Test", limits = c("testC-03", "testC-02", "testC-01", "testB-03", "testB-02", "testB-01", "testA-03",
"testA-02", "testA-01")) +
theme_classic()
This lead to the following plot:
I would like to bundle the labels on the y-axis, so that I do not repeat "Test[letter]" three times for each test. I am able to do it manually, however, I thought maybe it is possible to use ggplot. The first part of the solution is to remove the "Test[letter]" part from the limits
of scale_y_discrete()
. Next I would like to add the labels vertically and grouped per test on the y-axis (preferably with a vertical line grouping the tests), like this:
Is this possible in ggplot? And if so, how do you do this?