如何在Rstudio中通过分组类别进行标记

huangapple go评论69阅读模式
英文:

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)

我从一篇论文中获得了以下图像:

如何在Rstudio中通过分组类别进行标记

我尝试了很多次,但无法成功,请帮忙。

英文:

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 &lt;- data.frame(
  Category = c(&quot;Ank&quot;, &quot;Ank&quot;, &quot;Ank&quot;, &quot;Toxin&quot;, &quot;Toxin&quot;, &quot;Toxin&quot;, &quot;TPR&quot;, &quot;TPR&quot;),
  Gene = c(
    &quot;Tubulin (PF00091.26)&quot;,
    &quot;PD40 (PF07676.13)&quot;,
    &quot;HEAT_2 (PF13646.7)&quot;,
    &quot;DUF4097 (PF13349.7)&quot;,
    &quot;fn3_5 (PF06280.13)&quot;,
    &quot;fn3 (PF00041.22)&quot;,
    &quot;Cad (PF03596.14)&quot;,
    &quot;Big_2 (PF02368.19)&quot;
  ),
  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.

如何在Rstudio中通过分组类别进行标记

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")

如何在Rstudio中通过分组类别进行标记

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")

如何在Rstudio中通过分组类别进行标记

英文:

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)
#&gt; Loading required package: viridisLite
library(ggplot2)
library(tidyverse)

df |&gt;
  pivot_longer(-c(Category, Gene)) |&gt;
  ggplot(aes(Gene, name, fill = value)) +
  geom_tile(color = &quot;black&quot;) +
  scale_fill_viridis(discrete = FALSE) +
  geom_text(aes(label = value,
                color = ifelse(value &lt; 1, &quot;light&quot;, &quot;dark&quot;))) +
  scale_color_manual(values = c(light = &quot;white&quot;, dark = &quot;black&quot;),
                     guide = &quot;none&quot;) +
  labs(x = &quot;&quot;, y = &quot;&quot;) +
  facet_wrap( ~ Category, scales = &quot;free_x&quot;) +
  theme(axis.text.x = element_text(angle = 90),
        legend.position = &quot;bottom&quot;)

如何在Rstudio中通过分组类别进行标记<!-- -->

ggh4x::guide_axis_nested()

library(ggh4x)

df |&gt;
  pivot_longer(-c(Category, Gene)) |&gt;
  mutate(Gene = paste(Gene, Category, sep = &quot;&amp;&amp;&quot;)) |&gt;
  ggplot(aes(Gene, name, fill = value)) +
  geom_tile(color = &quot;black&quot;) +
  scale_fill_viridis(discrete = FALSE) +
  geom_text(aes(label = value,
                color = ifelse(value &lt; 1, &quot;light&quot;, &quot;dark&quot;))) +
  scale_color_manual(values = c(light = &quot;white&quot;, dark = &quot;black&quot;),
                     guide = &quot;none&quot;) +
  guides(x = ggh4x::guide_axis_nested(delim = &quot;&amp;&amp;&quot;)) +
  labs(x = &quot;&quot;, y = &quot;&quot;) +
  theme(axis.text.x = element_text(angle = 90),
        legend.position = &quot;bottom&quot;)

如何在Rstudio中通过分组类别进行标记<!-- -->

huangapple
  • 本文由 发表于 2023年7月18日 04:18:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定