在旋转的三角矩阵旁边添加颜色条
Add color bars beside rotated triangular matrix
我想创建一个三角矩阵,将其顺时针旋转45度,并在旋转后的三角矩阵的边上添加颜色条。这是我的代码:
# 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())
这是p1
,看起来不错:
我想将这个矩阵顺时针旋转 45 度。我按照建议的代码 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())
然而,p2
并不是我想要的:
这是我想要的:
因为我想顺时针旋转p1
45度,所以对角线应该是垂直的,三角矩阵里面不能有缺失值。 如何旋转?
另外,我想在热力图的两侧添加彩条,如下图粉红色圈出的彩条所示:
我的数据有 10 列和 10 行,我希望使用 5 种不同的颜色为我自己的热图绘制上面的颜色条,每种颜色跨越两个相邻的 rows/columns。 如何添加?
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)
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))
我想创建一个三角矩阵,将其顺时针旋转45度,并在旋转后的三角矩阵的边上添加颜色条。这是我的代码:
# 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())
这是p1
,看起来不错:
我想将这个矩阵顺时针旋转 45 度。我按照建议的代码 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())
然而,p2
并不是我想要的:
这是我想要的:
因为我想顺时针旋转p1
45度,所以对角线应该是垂直的,三角矩阵里面不能有缺失值。 如何旋转?
另外,我想在热力图的两侧添加彩条,如下图粉红色圈出的彩条所示:
我的数据有 10 列和 10 行,我希望使用 5 种不同的颜色为我自己的热图绘制上面的颜色条,每种颜色跨越两个相邻的 rows/columns。 如何添加?
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)
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))