按单独的列排序 ggplot

Posted

技术标签:

【中文标题】按单独的列排序 ggplot【英文标题】:order ggplot by a separate column 【发布时间】:2021-06-17 01:29:34 【问题描述】:

我正在尝试在不是绘图的不同列的基础上订购此 ggplot。这可能吗?

这是我的数据子集

test<- structure(list(
  Mutations = c(
    "P9L",
    "P9S",
    "P9L",
    "P9L",
    "P9S",
    "P9L",
    "P9Q",
    "P9S",
    "P9S",
    "P9L",
    "P9S",
    "P9L",
    "P9S",
    "P9L",
    "P9Q",
    "P9S",
    "P9L",
    "P9Q",
    "P9S",
    "P9L",
    "P9S",
    "P9L",
    "S12P",
    "S12P",
    "S12P",
    "S12P",
    "S12P",
    "S12P",
    "S12P",
    "C15S",
    "C15S",
    "C15F",
    "C15F",
    "C15F",
    "C15F",
    "C15F",
    "C15S",
    "C15F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F",
    "L18F"
  ),
  x = c(
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    9L,
    12L,
    12L,
    12L,
    12L,
    12L,
    12L,
    12L,
    15L,
    15L,
    15L,
    15L,
    15L,
    15L,
    15L,
    15L,
    15L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L,
    18L
  ),
  epi_week = c(
    51L,
    53L,
    53L,
    54L,
    55L,
    55L,
    55L,
    56L,
    57L,
    57L,
    58L,
    58L,
    59L,
    59L,
    59L,
    60L,
    60L,
    60L,
    61L,
    61L,
    62L,
    62L,
    53L,
    55L,
    56L,
    57L,
    58L,
    60L,
    61L,
    52L,
    54L,
    56L,
    57L,
    58L,
    59L,
    60L,
    61L,
    62L,
    49L,
    50L,
    51L,
    52L,
    53L,
    54L,
    55L,
    56L,
    57L,
    58L,
    59L,
    60L
  ),
  n = c(
    0.018412815,
    0.015021782,
    0.015021782,
    0.010515247,
    0.024205261,
    0.032273681,
    0.00806842,
    0.023421032,
    0.080858175,
    0.048514905,
    0.036714089,
    0.073428178,
    0.045506257,
    0.053090633,
    0.007584376,
    0.040013338,
    0.026675559,
    0.00666889,
    0.134355489,
    0.023709792,
    0.037509377,
    0.012503126,
    0.015021782,
    0.01613684,
    0.015614021,
    0.016171635,
    0.009178522,
    0.013337779,
    0.031613056,
    0.027144408,
    0.010515247,
    0.007807011,
    0.016171635,
    0.018357045,
    0.037921881,
    0.013337779,
    0.007903264,
    0.012503126,
    0.16722408,
    0.353495679,
    0.699686982,
    1.221498371,
    1.727504882,
    3.238696109,
    4.260125867,
    3.70833008,
    3.913535658,
    2.065167508,
    2.935153584,
    2.734244748
  ),
  mab = c(
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L
  ),
  support = c(
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L,
    1L
  )
),
row.names = c(NA,
              50L), class = "data.frame")

这是剧情的代码

p1 <- ggplot(test, aes(x = epi_week, y = Mutations, fill = n))+  
  geom_tile() + coord_equal() +  scale_fill_gradient(trans = "log",low="white", high="darkgreen", guide_legend(title = "Percentage (%)", title.position = "top"), labels = function(x) sprintf("%.2f", x)) + theme(
    # Hide panel borders and remove grid lines
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    # Remove panel background
    panel.background = element_blank(),
    # Change axis line
    axis.line = element_line(colour = "black"),
    axis.text.y = element_text( hjust = 1, size=5,color="black")
    )

p1

p1 + geom_tile(data=test, aes(colour=factor(support, c(1,2,3)),  size=factor(support,c(1,2,3))), alpha=0) + 
  scale_colour_manual("Confidence", values=c("white", "c(1,2,3)),  size=factor(support,c(1,2,3))), alpha=0) + 
  scale_colour_manual("Confidence", values=c("white", "blue4","red")) + 
  scale_size_manual("Confidence", values=c(0,0.2,0.2))

突变是一个字母后跟一个数字,然后是另一个字母的名称(例如 R377L)。

在我的数据中,我有一列 x 包含所有数字。 如何通过降低 x 值在 ggplot(即 Mutations)中排序我的 y 轴?

我还想知道是否可以在带有来自 mab 列的数据的绘图旁边添加一些列注释?

谢谢

【问题讨论】:

在没有数据样本的情况下很难提供帮助(使用 dput)。但是,请查看 reorder 函数 好的,抱歉,我刚刚添加了一部分数据 【参考方案1】:

您可以使用reorder(Mutations,-epi_week) 根据每个 Mutation 中对应的 epi_week 平均值对 Mutations 重新排序,也可以参见 help page:

ggplot(test, aes(x = epi_week, y = reorder(Mutations,-epi_week), fill = n))+  
geom_tile() + coord_equal() +  
scale_fill_gradient(trans = "log",low="white", high="darkgreen", 
guide_legend(title = "Percentage (%)", title.position = "top"), labels = function(x) sprintf("%.2f", x)) + theme(
    # Hide panel borders and remove grid lines
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    # Remove panel background
    panel.background = element_blank(),
    # Change axis line
    axis.line = element_line(colour = "black"),
    axis.text.y = element_text( hjust = 1, size=5,color="black")
    )

如果你想在剥离字符串后按其数值重新排序,它将是:

ggplot(test, aes(x = epi_week, 
y = reorder(Mutations,-as.numeric(gsub("[^0-9]*","",Mutations))), 
fill = n))+  
geom_tile() + coord_equal() + ylab("Mutation")

【讨论】:

谢谢。但我认为这是一个误解。我的情节中的 x 是 epi_week。但这不是我要找的。我希望突变按定义突变的数字排序(该数字列在我的数据集测试的“x”列中,而不是“epi_week” 什么数字?你能说清楚一点吗?我不是唯一一个误解你的问题的人 是的。对不起我的错。突变的示例是 C15F、P9Q、S12P 等 但是非常感谢我按照您的建议解决了问题p1 &lt;- ggplot(test, aes(x = epi_week, y = reorder(Mutations,x), fill = n))+ geom_tile() 您知道如何为我的数据集 (mab) 的另一列添加额外的颜色条(在图的右侧)吗?

以上是关于按单独的列排序 ggplot的主要内容,如果未能解决你的问题,请参考以下文章

自然排序以按列名对数据框的列进行排序

首先按最大负值对熊猫数据框的列进行排序

如何在 SQL Server 中连接字符串,并按不同的列排序/排序?

如何按 mySQL 中新生成的列排序?

如何在pyspark中按字母顺序对嵌套结构的列进行排序?

按 vue.js 中的列标题排序