【问题标题】:Order y-axis of ggplot by another factor (not alphabetically) R按另一个因素(不按字母顺序)对 ggplot 的 y 轴进行排序 R
【发布时间】:2013-06-07 23:15:08
【问题描述】:

我已经阅读了很多关于对 ggplot2 中制作的热图的 y 轴进行排序的问答,因此再写一篇感觉很糟糕,但我似乎无法实现我想要的。 (这可能是因为我是 R 新手,才刚刚开始掌握术语和工作原理。)提前感谢您的帮助!

我正在尝试为基因富集分析生成热图。我的数据被导入为 .csv 文件,格式为:Gene Category Description Variable1 Variable2 Variable3。所以每一行列出一个基因,基因所属的类别(每个类别中有多个基因),基因类别的描述,与每个样本相关的数值(3列,每个样本的值)。

我想做的是按类别对 y 轴进行排序,同时按基因绘制值。 (并且标记它的方法会很棒!)下面是我到目前为止的代码......它似乎按字母顺序排列 y 轴。

library(ggplot2)
library(reshape2)
GO_sum <- read.csv("~/R/FuncEnr/GO_sum.csv", header=T)
GO_sum.m <- melt(GO_sum, id = c("Gene", "Category", "Description"), na.rm = FALSE)


(GOplot <- ggplot(GO_sum.m, aes(variable, Gene)) + 
    geom_tile(aes(fill = value), colour = "white") + 
    scale_fill_gradient2(low = "darkred", high = "darkblue", guide="colorbar"))

谢谢!

这是一些示例数据(复制和粘贴,另存为 .csv):

Gene    Category    Description s1  s2  s3
G0001   GO:0000036  acyl carrier activity   -1.357472549    -1.357472549    -0.703587499
G0002   GO:0000103  sulfate assimilation    0   -0.761925294    -1.772268589
G0003   GO:0000104  succiNAte dehydrogeNAse activity    -1.192800096    -1.192800096    -1.192800096
G0014   GO:0000160  two-component sigNAl transduction system (phosphorelay) 0   -1.772268589    -1.192800096
G0005   GO:0000287  magnesium ion binding   -1.772268589    -1.772268589    -1.192800096
G0006   GO:0000287  magnesium ion binding   -1.192800096    -1.192800096    -1.164082367
G0007   GO:0000287  magnesium ion binding   -1.132072566    -1.772268589    -1.772268589
G0008   GO:0000287  magnesium ion binding   -1.452170577    0   -1.192800096
G0009   GO:0000287  magnesium ion binding   0   -1.772268589    -1.192800096
G0083   GO:0003676  nucleic acid binding    -1.192800096    -1.192800096    -1.772268589
G0044   GO:0003676  nucleic acid binding    -0.587905946    -0.363837338    -0.843984355
G0045   GO:0003676  nucleic acid binding    0.212339083 0.212339083 0.276358685
G0046   GO:0003676  nucleic acid binding    -0.374137972    -0.761925294    -0.761925294
G0147   GO:0003677  DNA binding 0   0   0
G0048   GO:0003677  DNA binding -1.192800096    0   -1.192800096
G0049   GO:0003677  DNA binding 0.530699113 -0.340270054    -0.485584696
G0050   GO:0003677  DNA binding -1.192800096    -0.374137972    -0.374137972

【问题讨论】:

  • 您必须提供一些数据或将我们链接到您的文件。此代码不可重现。
  • 因子变量的 Alpha 排序是常态,但通过示例数据集,我们可以向您展示如何“重新排序”。
  • 请给我们dput(GO_sum)

标签: r ggplot2 r-factor


【解决方案1】:

我建议您使用facet_grid() 将您的图按显示基因类别的列Description 划分。使用参数scales="free_y"space="free_y",您可以确保每个构面的图块大小相同。只有您应该为 Description 使用较短的名称,因为这样长的名称不适合。

ggplot(GO_sum.m, aes(variable, Gene)) + 
   geom_tile(aes(fill = value), colour = "white") + 
   scale_fill_gradient2(low = "darkred", high = "darkblue", guide="colorbar")+
   facet_grid(Description~.,scales="free_y",space="free_y")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 2011-08-29
    • 2015-01-18
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多