I have a dataset similar to the below example
df <- structure(list(Species = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,3L,
1L, 2L, 3L), .Label = c("setosa", "versicolor", "virginica"), class =
"factor"), flower_att = c("Sepal.Length", "Sepal.Length", "Sepal.Length",
"Sepal.Width", "Sepal.Width", "Sepal.Width", "Petal.Length", "Petal.Length",
"Petal.Length", "Petal.Width", "Petal.Width", "Petal.Width"), measurement =
c(5.1, 7, 6.3, 3.5, 3.2, 3.3, 1.4, 4.7, 6, 0.2, 1.4, 2.5), month =
c("January", "February", "January", "February", "January", "February",
"January", "February", "January", "February", "January", "February")),
row.names = c(NA,-12L), class = "data.frame")
I want to display both sepal length and width for each species and month side by side. I was hoping to do this using a diagonal split cell in the heatmap with 2 different colour legends i.e. red for length and blue for width. If possible I would like the value to be displayed within the cell segment. My search so far has found this closest example but I am looking for a workable ggplot version.
My own attempt currently looks like the below. I cannot work out how to break up the cells.
ggplot(df, aes(x=month, y=Species)) + geom_tile(aes(fill=measurement),
color="black") + theme(axis.text.x = element_text(angle=45, hjust = .5)) +
geom_text(aes(label = round(measurement, .1))) + scale_fill_gradient(low =
"white", high = "red")
Update
After some serious digging through the internet I have found a potential option using geom_segment
and geom_text_repel
, see below. Could anyone tell me if this a viable option to pursue? If so how can I get it to meet the requirements above?
I am open to switching scale_fill_gradient
to scale_fill_manual
or other alternative, my main objective is to have the all data displayed side by side
ggplot(df, aes(x=month, y=Species)) +
geom_tile(aes(fill=measurement), color="black") +
theme(axis.text.x = element_text(angle=45, hjust = .5)) +
geom_text_repel(aes(label = round(measurement, .1))) +
scale_fill_gradient(low = "white", high = "red")
gb <- ggplot_build(p)
p + geom_segment(data=gb$data[[1]],
aes(x=xmin, xend=xmax, y=ymin, yend=ymax), color="black")