【问题标题】:How to assign colours to specific genes in gggenes arrowplot?如何为gggenes箭头图中的特定基因分配颜色?
【发布时间】:2022-06-23 16:48:28
【问题描述】:

我是 R 新手,我正在尝试制作箭头图。但是,基本的 gggenes set3 颜色主题只有 12 种颜色,我需要更多。

我想为一组基因分配颜色(例如,糖基转移酶全为红色,甲基转移酶全为蓝色)

我在我的 df 中添加了一个名为“颜色”的额外列,并为每个基因分配了一个十六进制代码 (#c1ffc1) - 只是为了测试所有基因在通过并分配糖基转移酶等之前是否可以改变颜色 - 我设法让它改变颜色一次,现在它不起作用?

这是三个基因的代码示例

#add colour column to assign to genes
> colour <- c("#c1ffc1")
> df1$colour <- colour
> #change colour
> library(ggplot2)
> library(gggenes)
> ggplot(df1, aes(xmin = start, xmax = end, y = molecule, fill = colour)) +
+   geom_gene_arrow() +
+   geom_gene_label(aes(label = gene)) + 
+   facet_wrap(~ molecule, scales = "free", ncol = 1) + 
+   theme(legend.position="top") + xlim(0,37841) + scale_fill_discrete(name = "gene", labels = c("VanH", "VanA", "VanX"))
 molecule start   end  strand   gene  orientation  colour
 KJ364518.1  2314  3345 reverse vanH 1  #f15854
 KJ364518.1  3347  4387 reverse vanA 1  #f15854
 KJ364518.1  4384  4992 reverse vanX 1  #f15854
 KJ364518.1  6334  7125 reverse ajrR 1  #faa43a
 KJ364518.1  7246  8097 reverse pdh  1  #5da5da
 KJ364518.1  8410 10272 reverse tri  1  #b276b2

提前非常感谢, 露西

【问题讨论】:

  • 没有样本数据很难知道,但是......我推断gene 是一个分类变量,并且尝试区分超过(比如说)7 种不同的颜色甚至可能是有问题的不考虑色盲等问题。但是......通常你可以使用aes(color=I(colour))(或添加到现有的aes(..))来强制ggplot在数据中使用你预定义的颜色。
  • @r2evans 您好,非常感谢您的回复!我已经编辑了帖子以添加 df 的图像,希望这更有意义?
  • 谢谢!为此......请不要(仅)发布代码/数据/错误的图像:它会破坏屏幕阅读器并且无法复制或搜索(参考:meta.stackoverflow.com/a/285557xkcd.com/2116)。请直接包含代码、控制台输出或数据(例如,data.frame(...) 或来自dput(head(x)) 的输出)。 (我不会花时间转录数据。)
  • @r2evans 真诚的道歉,我对此很陌生...我的数据是一个表格有没有办法输入,所以你可以看到?
  • 如果您将dput(head(df1)) 的输出复制粘贴到您的问题中,我们可以使用它来重现您的部分数据。

标签: r ggplot2 colors hex


【解决方案1】:

您的代码似乎工作正常,唯一的问题是您为映射提供了文字颜色,而不是 fill = your_classification_here。要显示文字颜色,您可以使用scale_fill_identity()

library(ggplot2)
library(gggenes)

txt <- " molecule start   end  strand   gene  orientation  colour
 KJ364518.1  2314  3345 reverse vanH 1  #f15854
 KJ364518.1  3347  4387 reverse vanA 1  #f15854
 KJ364518.1  4384  4992 reverse vanX 1  #f15854
 KJ364518.1  6334  7125 reverse ajrR 1  #faa43a
 KJ364518.1  7246  8097 reverse pdh  1  #5da5da
 KJ364518.1  8410 10272 reverse tri  1  #b276b2"

df1 <- data.table::fread(text = txt)

ggplot(df1, aes(xmin = start, xmax = end, y = molecule, fill = colour)) +
  geom_gene_arrow() +
  geom_gene_label(aes(label = gene)) +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_identity()

reprex package (v2.0.1) 于 2022-05-11 创建

【讨论】:

  • 非常感谢! scale_fill_identity 效果很好!
  • 不好意思又问了,买不知道有没有办法让单基因箭头指向另一个方向?
  • 可能会交换 xminxmax
  • 非常感谢!这会改变所有箭头的方向。你知道有没有办法像这样分离单个基因/箭头?
【解决方案2】:

当您创建数据框时,名为方向的列之一包含信息方向,表示为 1 或 -1。如果添加命令

forward = orientation

在您的 ggplot 美学中,它将应用每个基因的方向。

【讨论】:

    猜你喜欢
    • 2019-09-17
    • 2020-12-03
    • 2017-02-22
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    相关资源
    最近更新 更多