【问题标题】:Heatmap: order y axis based on single x axis variable热图:根据单个 x 轴变量对 y 轴进行排序
【发布时间】:2022-08-18 00:40:58
【问题描述】:

在我的热图中,我想根据 Haul 1 中的百分比值(减少)来订购 Species。这是我当前的代码:

ggplot(data, aes(Haul, Species)) + geom_tile(aes(fill = Percent)) + 
  scale_fill_gradient(low = \"white\", high = \"red\") + 
  labs(fill = \"% Sites\")+ theme_bw()

因此,物种顺序应该是 b、c、d、a,基于减少运输 1 中的百分比。我尝试使用 reorder(),但找不到基于运输 1 指定排序的方法。

ggplot(data, aes(Haul, reorder(Species, Percent))) + geom_tile(aes(fill = Percent)) + 
  scale_fill_gradient(low = \"white\", high = \"red\") + 
  labs(fill = \"% Sites\")+ theme_bw()

我的数据框是这样设置的:

Species = rep(c(\"a\",\"b\",\"c\",\"d\"),3)
Haul = rep(c(1,2,3),4)
Percent = c(25,0,25,50,25,0,75,0,50,100,0,50)
data = data.frame(Species, Haul, Percent)

谢谢您的帮助!

伊莉丝

    标签: r ggplot2 heatmap


    【解决方案1】:

    reorder 中,我们可以将Percent 乘以Haul == 1 只查看第一个Haul

    ggplot(data, aes(Haul, reorder(Species, Percent * (Haul == 1)))) + 
      geom_tile(aes(fill = Percent)) + 
      scale_fill_gradient(low = "white", high = "red") + 
      labs(fill = "% Sites", y = "Haul") +
      theme_bw()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2018-03-20
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多