【问题标题】:Manually change order of stacked bar charts in ggplot手动更改ggplot中堆叠条形图的顺序
【发布时间】:2021-04-16 12:40:34
【问题描述】:

我想手动更改堆叠条形图的顺序,以使我的标签具有可读性,这些标签的观察结果非常少,以至于它们彼此重叠。 我的目标是将顺序设置为“未分类”、“真菌”、“绿色植物”,以便将几乎没有观察到的条形彼此分开。

I tried the proposed solution here but it would not work.也许我错过了什么?

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()

the 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"))

【问题讨论】:

  • 究竟是什么不工作?愚蠢的建议,您在传递给 ggplot 之前尝试过ungroup 吗?有时分组数据会导致问题(尽管我真的不认为这是这里的问题)

标签: r ggplot2 bar-chart stacked-chart geom-col


【解决方案1】:

问题似乎是您有两个不同的位置将fill 映射到kingdom

  1. ggplot(... aes(fill=factor(kingdom...内的顶部调用中
  2. 在对 geom 的调用中:geom_col(aes(fill=kingdom))

ggplot 中决定映射的层次结构在一般映射之前使用 geom 调用中的映射。这里发生的事情是geom_col(aes(fill=kingdom)) 正在覆盖您在顶级通话中所说的内容。它将映射未重构的 kingdom 列,而不是重构的列。

简单的解决方法是使用geom_col() 代替geom_col(aes(fill=kingdom))

如果您愿意,可以选择替代修复:

  • 在绘图前更改kingdomtotaltibble$kingdom &lt;- factor(totaltibble$kingdom, levels=c("unclassified", "Fungi", "Viridiplantae"))

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

  • 删除ggplot() 中的fill= statemnt 并将其移至geom_col()

在第一行 ggplot() 您引用并更改它,但您也在第二行中说明它:geom_col(aes(fill=kingdom))。由于您在geom_col()重述它,ggplot 将使用 geom 调用中所述的通用映射语句。只需在 geom_col() 中删除它 - 那里不需要它并且会导致问题。所以,只有geom_col() 没有aes()。或者,在绘图之前更改kingdom,或者在aes() 中使用相同的factor(... code 来代替geom_col()

【讨论】:

  • 感谢所有提示。我仍然不知道出了什么问题。我尝试了所有建议的解决方案,不幸的是没有一个解决它。条形段的顺序仍然相同
  • @mr.raccoon,你能分享你的数据或你的部分数据,以便重现吗?另外,我建议在您的问题中分享您的情节图像,因为通过查看您的输出可能会有所了解。要共享数据,请在您的控制台中通过dput(totaltibble) 共享,然后将该输出复制并粘贴到格式化为代码的问题中。如果它太大,您可以执行dput(head(totaltibble, 25))dput(totaltibble[sample(1:nrow(totaltibble), 25),]) 之类的操作。
  • 感谢您再次调查。我改变了我原来的帖子,包括一张阴谋的图片和我通过dput创建它的对象@
猜你喜欢
  • 2020-01-30
  • 2020-10-16
  • 1970-01-01
  • 2017-07-21
  • 2018-02-15
  • 1970-01-01
  • 2019-05-19
  • 2017-09-17
  • 2022-08-19
相关资源
最近更新 更多