将数字字符串可视化为矩阵热图
visualize numerical strings as a matrixed heatmap
我正在尝试将数字字符串矩阵可视化为热图
以这个例子为例,36 个元素长度 "History" 数字字符串,假设我有 6 行(我实际上有 500 行)。我想可视化 6x36 "pixels or cells" 矩阵的热图。此外,通过 "Survive" 变量上的 True/False 直观地对它们进行排序或拆分会很棒。
testdata=
History Survive
1 111111111111111211111111111111111111 FALSE
2 111111111111111110000000000000000000 TRUE
3 000111222111111111111111111111110000 FALSE
4 111111111111111111111111100000000000 TRUE
5 011231111111111111111111111111111111 FALSE
6 111111234111111111111111110000000000 TRUE
这是一个想法。我们可以拆分 Histroy
列,然后创建 rowid
和 ID
列以将数据绘制为热图。
library(tidyverse)
testdata2 <- testdata %>% mutate(History = str_split(History, pattern = ""))
testdata3 <- testdata2%>%
rowid_to_column() %>%
unnest() %>%
group_by(rowid) %>%
mutate(ID =row_number())
p <- ggplot(testdata3, aes(x = ID, y = rowid, fill = History)) +
geom_tile(color = "black") +
scale_fill_brewer() +
scale_y_reverse() +
labs(x = "", y = "") +
theme_minimal()
print(p)
如果我们想在 Survival
列中按 TRUE
和 FALSE
将数据绘制为分面,我们需要单独创建 rowid
作为 TRUE
和 FALSE
对于 Survival
.
testdata4 <- testdata2%>%
group_by(Survive) %>%
mutate(rowid = row_number()) %>%
unnest() %>%
group_by(Survive, rowid) %>%
mutate(ID = row_number())
p2 <- ggplot(testdata4, aes(x = ID, y = rowid, fill = History)) +
geom_tile(color = "black") +
scale_fill_brewer() +
scale_y_reverse() +
labs(x = "", y = "") +
theme_minimal() +
facet_grid(~ Survive)
print(p2)
数据
testdata <- read.table(text =
" History Survive
1 111111111111111211111111111111111111 FALSE
2 111111111111111110000000000000000000 TRUE
3 000111222111111111111111111111110000 FALSE
4 111111111111111111111111100000000000 TRUE
5 011231111111111111111111111111111111 FALSE
6 111111234111111111111111110000000000 TRUE",
header = TRUE, stringsAsFactors = FALSE,
colClasses = c("numeric", "character", "logical"))
我正在尝试将数字字符串矩阵可视化为热图 以这个例子为例,36 个元素长度 "History" 数字字符串,假设我有 6 行(我实际上有 500 行)。我想可视化 6x36 "pixels or cells" 矩阵的热图。此外,通过 "Survive" 变量上的 True/False 直观地对它们进行排序或拆分会很棒。
testdata=
History Survive
1 111111111111111211111111111111111111 FALSE
2 111111111111111110000000000000000000 TRUE
3 000111222111111111111111111111110000 FALSE
4 111111111111111111111111100000000000 TRUE
5 011231111111111111111111111111111111 FALSE
6 111111234111111111111111110000000000 TRUE
这是一个想法。我们可以拆分 Histroy
列,然后创建 rowid
和 ID
列以将数据绘制为热图。
library(tidyverse)
testdata2 <- testdata %>% mutate(History = str_split(History, pattern = ""))
testdata3 <- testdata2%>%
rowid_to_column() %>%
unnest() %>%
group_by(rowid) %>%
mutate(ID =row_number())
p <- ggplot(testdata3, aes(x = ID, y = rowid, fill = History)) +
geom_tile(color = "black") +
scale_fill_brewer() +
scale_y_reverse() +
labs(x = "", y = "") +
theme_minimal()
print(p)
如果我们想在 Survival
列中按 TRUE
和 FALSE
将数据绘制为分面,我们需要单独创建 rowid
作为 TRUE
和 FALSE
对于 Survival
.
testdata4 <- testdata2%>%
group_by(Survive) %>%
mutate(rowid = row_number()) %>%
unnest() %>%
group_by(Survive, rowid) %>%
mutate(ID = row_number())
p2 <- ggplot(testdata4, aes(x = ID, y = rowid, fill = History)) +
geom_tile(color = "black") +
scale_fill_brewer() +
scale_y_reverse() +
labs(x = "", y = "") +
theme_minimal() +
facet_grid(~ Survive)
print(p2)
数据
testdata <- read.table(text =
" History Survive
1 111111111111111211111111111111111111 FALSE
2 111111111111111110000000000000000000 TRUE
3 000111222111111111111111111111110000 FALSE
4 111111111111111111111111100000000000 TRUE
5 011231111111111111111111111111111111 FALSE
6 111111234111111111111111110000000000 TRUE",
header = TRUE, stringsAsFactors = FALSE,
colClasses = c("numeric", "character", "logical"))