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
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?
CodePudding user response:
zz %>%
as.data.frame() %>%
mutate(row = 11 - row_number()) %>%
pivot_longer(-row, names_to = "col") %>%
filter(!is.na(value)) %>%
# slice(1:12) %>%
mutate(col = parse_number(col),
col_new = col - 5 - (row col)/2,
row_new = row - (10 - col)) %>%
ggplot(aes(col_new, row_new, fill = value))
geom_tile(width = 1)
CodePudding user response:
p1_fmt <- p1 guides(fill = "none") coord_equal()
tri_file <- tempfile()
ggsave(tri_file, p1_fmt, width = 3, height = 3, device = "png")
p1_guide <- cowplot::get_legend(p1)
guide_file <- tempfile()
ggsave(guide_file, p1_guide, width = 1, height = 1.5, device = "png")
library(magick)
tri <- image_read(tri_file) %>%
image_rotate(45) %>%
image_trim()
guide <- image_read(guide_file)
image_append(c(tri, guide))