【问题标题】:Change the order of factors in y-axis of a ggplot2 heatmap based on how common they are根据它们的常见程度更改 ggplot2 热图 y 轴上的因子顺序
【发布时间】:2021-06-05 15:39:20
【问题描述】:

我有一个包含数千行的 data.frame,看起来像这样

rep = c("rep1", "rep1", "rep1","rep2", "rep2", "rep2","rep3", "rep3", "rep3")
species = c("a","b","d","b","e","f","b","f","h")
value=c(sample.int(100, 9))

df <- data.frame(rep,species,value)
df2 <- df %>% 
  mutate(species=factor(species)) %>% 
  arrange(species,rep)

ggplot(df2,aes(rep, species, fill=value))+
  geom_tile()

我想改变 y 轴的顺序,使所有重复中存在的物种首先出现,然后出现在两个重复中的物种在最后,最后出现在一个重复中的物种。

例如,我想先有物种 b,然后是 f,然后是其余的。 我的因素太多了,不可能一一改变。

如果有任何建议或指导,我将不胜感激。 提前谢谢你

更新解决方案: 我通过使用 fct_infreq() https://cran.r-project.org/web/packages/forcats/vignettes/forcats.html 找到了另一个解决方案。请检查一下

ggplot(df2,aes(rep, fct_infreq(species), fill=value))+
  geom_tile()

【问题讨论】:

    标签: r ggplot2 dplyr tidyverse


    【解决方案1】:

    这可以通过首先添加一个包含每个物种的重复次数的列来实现,例如dplyr::add_count 并根据计数对species 重新排序:

    library(ggplot2)
    library(dplyr)
    
    set.seed(42)
    
    rep = c("rep1", "rep1", "rep1","rep2", "rep2", "rep2","rep3", "rep3", "rep3")
    species = c("a","b","d","b","e","f","b","f","h")
    value=c(sample.int(100, 9))
    
    df <- data.frame(rep,species,value)
    df2 <- df %>% 
      add_count(species) %>% 
      mutate(species=reorder(species, n)) %>% 
      arrange(species,rep)
    
    ggplot(df2,aes(rep, species, fill=value))+
      geom_tile()
    

    【讨论】:

      【解决方案2】:

      不是最干净的

      library(tidyverse)
      
      rep = c("rep1", "rep1", "rep1","rep2", "rep2", "rep2","rep3", "rep3", "rep3")
      species = c("a","b","d","b","e","f","b","f","h")
      value=c(sample.int(100, 9))
      
      df <- data.frame(rep,species,value)
      df2 <- df %>% 
        mutate(species=factor(species)) %>% 
        arrange(species,rep)
      
      df3 <- df2 |>
        add_count(species,name = "order") 
      
      ggplot(df3,aes(x = rep,y = fct_reorder(species,order,.desc   = T), fill=value))+
        geom_tile()
      

      reprex package (v2.0.0) 于 2021-06-05 创建

      会话信息
      sessionInfo()
      #> R version 4.1.0 (2021-05-18)
      #> Platform: x86_64-w64-mingw32/x64 (64-bit)
      #> Running under: Windows 10 x64 (build 21390)
      #> 
      #> Matrix products: default
      #> 
      #> locale:
      #> [1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252   
      #> [3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C                      
      #> [5] LC_TIME=Portuguese_Brazil.1252    
      #> 
      #> attached base packages:
      #> [1] stats     graphics  grDevices utils     datasets  methods   base     
      #> 
      #> other attached packages:
      #> [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.6     purrr_0.3.4    
      #> [5] readr_1.4.0     tidyr_1.1.3     tibble_3.1.2    ggplot2_3.3.3  
      #> [9] tidyverse_1.3.1
      #> 
      #> loaded via a namespace (and not attached):
      #>  [1] tidyselect_1.1.1  xfun_0.23         haven_2.4.1       colorspace_2.0-1 
      #>  [5] vctrs_0.3.8       generics_0.1.0    htmltools_0.5.1.1 yaml_2.2.1       
      #>  [9] utf8_1.2.1        rlang_0.4.11      pillar_1.6.1      glue_1.4.2       
      #> [13] withr_2.4.2       DBI_1.1.1         dbplyr_2.1.1      modelr_0.1.8     
      #> [17] readxl_1.3.1      lifecycle_1.0.0   munsell_0.5.0     gtable_0.3.0     
      #> [21] cellranger_1.1.0  rvest_1.0.0       evaluate_0.14     labeling_0.4.2   
      #> [25] knitr_1.33        ps_1.6.0          curl_4.3.1        fansi_0.5.0      
      #> [29] highr_0.9         broom_0.7.6       Rcpp_1.0.6        backports_1.2.1  
      #> [33] scales_1.1.1      jsonlite_1.7.2    mime_0.10         farver_2.1.0     
      #> [37] fs_1.5.0          hms_1.1.0         digest_0.6.27     stringi_1.6.2    
      #> [41] grid_4.1.0        cli_2.5.0         tools_4.1.0       magrittr_2.0.1   
      #> [45] crayon_1.4.1      pkgconfig_2.0.3   ellipsis_0.3.2    xml2_1.3.2       
      #> [49] reprex_2.0.0      lubridate_1.7.10  assertthat_0.2.1  rmarkdown_2.8    
      #> [53] httr_1.4.2        rstudioapi_0.13   R6_2.5.0          compiler_4.1.0
      

      【讨论】:

        【解决方案3】:

        一种方法是按如下方式计算因子的水平:

        df2 <- df %>%
          mutate(species = factor(species,
                                  levels = df %>%
                                             count(~ species) %>%
                                             arrange(freq) %>%
                                             .$species))
        
        ggplot(df2, aes(rep, species, fill = value)) +
          geom_tile()
        

        【讨论】:

        • 谢谢你。它看起来不错,但是当我复制粘贴您的代码时,我收到此错误错误:mutate() 输入问题species。 x 在group_by() 中添加计算列时出现问题。 x mutate() 输入 ..1 有问题。 x 输入 ..1 必须是向量,而不是 formula 对象。 ℹ 输入..1~species。 ℹ 输入speciesfactor(...)
        【解决方案4】:
        rep = c("rep1", "rep1", "rep1","rep2", "rep2", "rep2","rep3", "rep3", "rep3")
        species = c("a","b","d","b","e","f","b","f","h")
        value=c(sample.int(100, 9))
        
        df <- data.frame(rep,species,value)
        library(tidyverse)
        df %>% add_count(species) %>% arrange(-n) %>% filter(!duplicated(species)) %>% pull(species) -> vec
        
        df2 <- df %>% 
          mutate(species=factor(species, levels = vec,ordered = T)) %>% 
          arrange(species,rep)
        
        ggplot(df2,aes(rep, species, fill=value))+
          geom_tile()
        

        reprex package (v2.0.0) 于 2021-06-05 创建

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-01
          • 1970-01-01
          • 2018-09-22
          • 2018-11-20
          • 2022-07-12
          • 1970-01-01
          • 2019-05-19
          • 2015-03-30
          相关资源
          最近更新 更多