I wish to create an triangular matrix, rotate it 45 degrees clockwise, and attach color bars on the sides of the rotated triangular matrix. Here is my code:
# The data
library(tidyverse)
x <- 1:10
y <- 1:10
data <- expand.grid(X=x, Y=y)
data$X <- as.numeric(data$X)
data$Y <- as.numeric(data$Y)
data$Z <- 1:(10*10)
# Create upper triangular matrix
zz <- t(matrix(data$Z, 10, 10))
zz[lower.tri(zz)] <- NA
data$zz <- c(t(zz))
# Use "-Y" so that the first row of data is plotted in the first row
p1 <- ggplot(data, aes(X, -Y, fill= zz)) +
geom_tile() +
# scale_fill_gradient(low="blue", high="red") +
scale_fill_gradient(low = "#132B43", high = "#56B1F7", space = "Lab", na.value="white") +
theme_bw() +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
I wish to rotate this matrix 45 degrees clockwide. I did so following the suggested code here:
rotate <- function(df, degree) {
dfr <- df
degree <- pi * degree / 180
l <- sqrt(df$X^2 + df$Y^2)
teta <- atan(df$Y / df$X)
dfr$X <- round(l * cos(teta - degree))
dfr$Y <- round(l * sin(teta - degree))
return(dfr)
}
data_rot <- rotate(data, 45)
p2 <- ggplot(data_rot, aes(X, -Y, fill= zz)) +
geom_tile() +
# scale_fill_gradient(low="white", high="blue") +
scale_fill_gradient(low = "#132B43", high = "#56B1F7", space = "Lab", na.value="white") +
theme_bw() +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
However, p2
is not what I want:
Since I wish to rotate p1
45 degrees clockwise, the diagonal should be verticle and there should not be missing value inside the triangular matrix. How to do the rotation?
Additionally, I want to add color bars by sides of the heatmap, as shown by the pink circled color bars in the figure below:
My data has 10 columns and 10 rows, I wish to plot color bars like above for my own heatmap using 5 different colors, with each color spanning two adjacent rows/columns. How to add?