英文:
How to label by grouping categories in Rstudio
问题
我正在研究海洋生物与细菌共生关系。我使用R生成clustermap/heatmap。我想根据给定的分类将基因分组,并相应地标记xticks。虽然我知道如何使用R生成热图,但我不确定如何将基因标记为分类。
假设这是我的示例数据集:
df <- data.frame(
Category = c("Ank", "Ank", "Ank", "Toxin", "Toxin", "Toxin", "TPR", "TPR"),
Gene = c(
"Tubulin (PF00091.26)",
"PD40 (PF07676.13)",
"HEAT_2 (PF13646.7)",
"DUF4097 (PF13349.7)",
"fn3_5 (PF06280.13)",
"fn3 (PF00041.22)",
"Cad (PF03596.14)",
"Big_2 (PF02368.19)"
),
Bacillus_sp1 = c(1, 3, 3, 2, 0, 0, 0, 0),
Bacillus_sp2 = c(1, 3, 3, 2, 3, 1, 1, 1),
Bacillus_sp3 = c(1, 3, 3, 2, 0, 0, 0, 0),
Bacillus_sp4 = c(1, 1, 2, 2, 2, 0, 1, 0)
)
# 打印数据框
print(df)
我从一篇论文中获得了以下图像:
我尝试了很多次,但无法成功,请帮忙。
英文:
I am working on bacterial symbiotic with marine organism. clustermap/heatmap using R. I would like to group the genes into categories as given in and label the xticks accordingly. Although I know how to generate the heatmap using R, I am unsure about how to label as categories.
suppose this is my example data set
df <- data.frame(
Category = c("Ank", "Ank", "Ank", "Toxin", "Toxin", "Toxin", "TPR", "TPR"),
Gene = c(
"Tubulin (PF00091.26)",
"PD40 (PF07676.13)",
"HEAT_2 (PF13646.7)",
"DUF4097 (PF13349.7)",
"fn3_5 (PF06280.13)",
"fn3 (PF00041.22)",
"Cad (PF03596.14)",
"Big_2 (PF02368.19)"
),
Bacillus_sp1 = c(1, 3, 3, 2, 0, 0, 0, 0),
Bacillus_sp2 = c(1, 3, 3, 2, 3, 1, 1, 1),
Bacillus_sp3 = c(1, 3, 3, 2, 0, 0, 0, 0),
Bacillus_sp4 = c(1, 1, 2, 2, 2, 0, 1, 0)
)
# Print the dataframe
print(df)
Image which I got from one paper.
I tried a lot but I couldn't, kindly help
答案1
得分: 1
我认为为这篇论文创建热图的人对热图进行了一些手动编辑以添加类别。我们可以通过使用ggplot2::facet_wrap()
或使用ggh4x::guide_axis_nested()
来实现类似的结果。
facet_wrap()
library(viridis)
#> Loading required package: viridisLite
library(ggplot2)
library(tidyverse)
df %>%
pivot_longer(-c(Category, Gene)) %>%
ggplot(aes(Gene, name, fill = value)) +
geom_tile(color = "black") +
scale_fill_viridis(discrete = FALSE) +
geom_text(aes(label = value,
color = ifelse(value < 1, "light", "dark"))) +
scale_color_manual(values = c(light = "white", dark = "black"),
guide = "none") +
labs(x = "", y = "") +
facet_wrap( ~ Category, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90),
legend.position = "bottom")
ggh4x::guide_axis_nested()
library(ggh4x)
df %>%
pivot_longer(-c(Category, Gene)) %>%
mutate(Gene = paste(Gene, Category, sep = "&&")) %>%
ggplot(aes(Gene, name, fill = value)) +
geom_tile(color = "black") +
scale_fill_viridis(discrete = FALSE) +
geom_text(aes(label = value,
color = ifelse(value < 1, "light", "dark"))) +
scale_color_manual(values = c(light = "white", dark = "black"),
guide = "none") +
guides(x = ggh4x::guide_axis_nested(delim = "&&")) +
labs(x = "", y = "") +
theme(axis.text.x = element_text(angle = 90),
legend.position = "bottom")
英文:
I think the person that created the heatmap for the paper did
some manual editing of the heatmap to add the categories.
We can achieve similar results either by using ggplot2::facet_wrap()
or by using ggh4x::guide_axis_nested()
.
facet_wrap()
library(viridis)
#> Loading required package: viridisLite
library(ggplot2)
library(tidyverse)
df |>
pivot_longer(-c(Category, Gene)) |>
ggplot(aes(Gene, name, fill = value)) +
geom_tile(color = "black") +
scale_fill_viridis(discrete = FALSE) +
geom_text(aes(label = value,
color = ifelse(value < 1, "light", "dark"))) +
scale_color_manual(values = c(light = "white", dark = "black"),
guide = "none") +
labs(x = "", y = "") +
facet_wrap( ~ Category, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90),
legend.position = "bottom")
<!-- -->
ggh4x::guide_axis_nested()
library(ggh4x)
df |>
pivot_longer(-c(Category, Gene)) |>
mutate(Gene = paste(Gene, Category, sep = "&&")) |>
ggplot(aes(Gene, name, fill = value)) +
geom_tile(color = "black") +
scale_fill_viridis(discrete = FALSE) +
geom_text(aes(label = value,
color = ifelse(value < 1, "light", "dark"))) +
scale_color_manual(values = c(light = "white", dark = "black"),
guide = "none") +
guides(x = ggh4x::guide_axis_nested(delim = "&&")) +
labs(x = "", y = "") +
theme(axis.text.x = element_text(angle = 90),
legend.position = "bottom")
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论