簡體   English   中英

手動更改ggplot中堆疊條形圖的順序

[英]Manually change order of stacked bar charts in ggplot

我想手動更改堆疊條形圖的順序,以使我的標簽可讀,這些標簽的觀察結果很少,以至於它們彼此重疊。 我的目標是將順序設置為“未分類”、“真菌”、“綠色植物”,以將幾乎沒有觀察到的條形彼此分開。

我在這里嘗試了建議的解決方案,但它不起作用。 也許我錯過了什么?

levels(as.factor(totaltibble$kingdom))
[1] "Fungi"         "unclassified"  "Viridiplantae"

phytibble <- psmelt(physeq_comp)
totaltibble <-phytibble %>%
  group_by(Sample, superkingdom, kingdom)%>%
  summarize(sum(Abundance))

        ggplot(totaltibble, aes(superkingdom, `sum(Abundance)`, fill=factor(kingdom, levels=c("unclassified", "Fungi", "Viridiplantae"))))+
      geom_col(aes(fill=kingdom))+
      scale_y_continuous("Anzahl der Reads", labels = comma_format(big.mark = ".", decimal.mark = ","))+
      scale_fill_manual("Reich", labels = c("Fungi", "unklassifiziert", "Viridiplantae"), values = wes_palette("Darjeeling1") )+
      scale_x_discrete("Domäne", labels = c("Backteria", "Eukaryota", "unklassifiziert", "Viren"))+
      ggtitle("Absolute Häufigkeit nach Reich und Domäne")+
       facet_grid(~Sample, labeller=(Sample=sample_labeller))+
      geom_text(aes(label=`sum(Abundance)`), vjust=1.6)+
      theme_bw()

Plot

object 我正在從以下位置創建 plot:

structure(list(Sample = c("MB5_2020_nano", "MB5_2020_nano", "MB6_2020_nano", 
"MB6_2020_nano", "MB5_2020_ill", "MB5_2020_ill", "MB6_2020_ill", 
"MB6_2020_ill", "MB5_2020_nano", "MB6_2020_nano", "MB5_2020_ill", 
"MB5_2020_nano", "MB5_2020_nano", "MB6_2020_ill", "MB6_2020_nano", 
"MB6_2020_nano", "MB6_2020_ill", "MB5_2020_nano", "MB6_2020_nano", 
"MB5_2020_ill", "MB6_2020_ill", "MB5_2020_ill", "MB5_2020_ill", 
"MB6_2020_ill"), superkingdom = c("Eukaryota", "unclassified", 
"Eukaryota", "unclassified", "unclassified", "Eukaryota", "Eukaryota", 
"unclassified", "Bacteria", "Bacteria", "Eukaryota", "Eukaryota", 
"Eukaryota", "Eukaryota", "Eukaryota", "Eukaryota", "Bacteria", 
"Viruses", "Viruses", "Bacteria", "Eukaryota", "Eukaryota", "Viruses", 
"Viruses"), totalreads = c(740180, 740180, 220406, 220406, 122691, 
122691, 41791, 41791, 740180, 220406, 122691, 740180, 740180, 
41791, 220406, 220406, 41791, 740180, 220406, 122691, 41791, 
122691, 122691, 41791), kingdom = c("Fungi", "unclassified", 
"Fungi", "unclassified", "unclassified", "Fungi", "Fungi", "unclassified", 
"unclassified", "unclassified", "unclassified", "unclassified", 
"Viridiplantae", "unclassified", "unclassified", "Viridiplantae", 
"unclassified", "unclassified", "unclassified", "unclassified", 
"Viridiplantae", "Viridiplantae", "unclassified", "unclassified"
), abundance = c(440891, 295055, 126035, 93059, 61774, 60325, 
28618, 12905, 3548, 1021, 591, 437, 224, 220, 191, 93, 47, 25, 
7, 1, 1, 0, 0, 0), percent = c(59.5653759896241, 39.8626009889486, 
57.1831075379073, 42.2216273604167, 50.3492513713312, 49.1682356489066, 
68.4788590844919, 30.8798545141298, 0.479342862546948, 0.463236028057312, 
0.481697924053109, 0.0590396930476371, 0.0302629090221298, 0.526429135459788, 
0.0866582579421613, 0.0421948585791675, 0.112464406211864, 0.00337755681050555, 
0.0031759570973567, 0.000815055709057714, 0.0023928597066354, 
0, 0, 0)), row.names = c(NA, -24L), groups = structure(list(Sample = c("MB5_2020_ill", 
"MB5_2020_ill", "MB5_2020_ill", "MB5_2020_ill", "MB5_2020_nano", 
"MB5_2020_nano", "MB5_2020_nano", "MB5_2020_nano", "MB6_2020_ill", 
"MB6_2020_ill", "MB6_2020_ill", "MB6_2020_ill", "MB6_2020_nano", 
"MB6_2020_nano", "MB6_2020_nano", "MB6_2020_nano"), superkingdom = c("Bacteria", 
"Eukaryota", "unclassified", "Viruses", "Bacteria", "Eukaryota", 
"unclassified", "Viruses", "Bacteria", "Eukaryota", "unclassified", 
"Viruses", "Bacteria", "Eukaryota", "unclassified", "Viruses"
), totalreads = c(122691, 122691, 122691, 122691, 740180, 740180, 
740180, 740180, 41791, 41791, 41791, 41791, 220406, 220406, 220406, 
220406), .rows = structure(list(20L, c(6L, 11L, 22L), 5L, 23L, 
    9L, c(1L, 12L, 13L), 2L, 18L, 17L, c(7L, 14L, 21L), 8L, 24L, 
    10L, c(3L, 15L, 16L), 4L, 19L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -16L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

問題似乎是您有兩個不同的地方kingdom fill到 Kingdom :

  1. ggplot(... aes(fill=factor(kingdom...
  2. 在對 geom 的調用中: geom_col(aes(fill=kingdom))

ggplot中決定映射的層次結構在一般映射之前使用 geom 調用中的映射。 這里發生的是geom_col(aes(fill=kingdom))正在覆蓋您在頂部調用中所說的內容。 它將 map 非重構kingdom列,而不是您重構的列。

簡單的解決方法是使用geom_col()代替geom_col(aes(fill=kingdom))

如果您願意,可以使用替代修復:

  • 在繪圖前更改kingdomtotaltibble$kingdom <- factor(totaltibble$kingdom, levels=c("unclassified", "Fungi", "Viridiplantae"))

  • 復制ggplot()中使用的aes(fill=...語句以在 geom_col geom_col()語句中使用。

  • 刪除ggplot()中的fill= statemnt 並將其移至geom_col()

在第一行ggplot()您引用並更改它,但您也在第二行中說明它: geom_col(aes(fill=kingdom)) 由於您在geom_col()重述它,因此ggplot將使用 geom 調用中所述的通用映射語句。 只需在geom_col()中刪除它 - 它在那里不需要並且會導致問題。 所以,只有geom_col()沒有aes() 或者,在 plot 之前更改kingdom ,或者在aes()中為geom_col()使用相同的factor(...代碼)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM