ggalluvial:第一轴和第二轴的相同地层顺序
ggalluvial: identical strata order of the first and second axis
我想保持冲积地块的第二个轴与第一个轴的顺序相同。也就是说,由于第一个轴是第二个轴的更高级别的分类法,其主要目的是显示生物体的概览和分组,以增强图表的可读性。为此,我尝试手动订购地层。然而,我所做的只是重新洗牌矿脉而不是地层(例如从这个 tutorial 或玩 lode.guidance
)。
有人知道如何解决这个问题吗?最后,第一轴和第二轴之间的所有矿脉应该水平流动,然后从第二轴到第三轴按原样排序。
数据的简短版本(仍然很广泛,抱歉):
taxa <- structure(list(Order = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L,
32L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L),
.Label = c("Adinetida", "Cercomonadidae_or",
"Cercozoa_unclassified",
"Chaetopeltidales",
"Chlorophyta_ph_unclassified",
"Chromulinales",
"Chrysophyceae_unclassified",
"Chytridiomycetes_unclassified",
"Conthreep", "Craspedida_or",
"Cryomonadida", "Cryptomonadales",
"Cystobasidiales",
"Cystobasidiomycetes_unclassified",
"Cystofilobasidiales",
"Dinophyceae_unclassified",
"Diplogasterida",
"Glissomonadida_or",
"Helotiales", "Imbricatea_unclassified",
"Incertae_Sedis",
"Intramacronucleata_unclassified",
"Leotiomycetes_unclassified", "LG08-10_or",
"Litostomatea", "Monhysterida",
"Ochromonadales",
"Ochrophyta_ph_unclassified",
"Parachela",
"Peronosporomycetes_or",
"Phragmoplastophyta_unclassified",
"Saccharomycetales",
"Spirotrichea", "Spongomonadida",
"Thecofilosea_unclassified",
"Tremellales", "Tremellomycetes_or",
"Tremellomycetes_unclassified",
"Trichosporonales"),
class = "factor"),
Genus = c(paste(rep("Poterioochromonas", 19)),
paste(rep("Colpoda", 9)),
paste(rep("Colpodea_unclassified", 24)),
paste(rep("Colpodida_ge", 28)),
paste(rep("Conthreep_unclassified", 4)),
"Cryptocaryon", "Cyclidium",
paste(rep("Nassophorea_unclassified", 2)),
"Platyophrya", paste(rep("Tetrahymena", 5)),
paste(rep("uncultured", 3)),
paste(rep("uncultured_ge", 4)), "Glarea",
paste(rep("Helotiales_unclassified_ge", 13)),
paste(rep("Chrysolepidomonas", 12)), "Ochromonas",
paste(rep("Debaryomycetaceae_unclassified", 3)),
"Pichiaceae_unclassified_ge",
paste(rep("Saccharomycetaceae_unclassified", 5)),
paste(rep("Yarrowia", 4))),
Freq = rep(1, 141),
Habitat = c("B", "B", "B", "B", "B", "B", "B","B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "A", "A", "A",
"B", "A", "A", "B", "B", "B", "B", "A", "A", "A", "A",
"B", "A", "A", "A", "A", "B", "A", "B", "B", "A", "B",
"B", "B", "A", "A", "A", "B", "B", "B", "A", "A", "A",
"A", "A", "A", "A", "A", "B", "A", "B", "A", "B", "B",
"A", "A", "B", "B", "A", "B", "A", "B", "B", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A",
"B", "A", "A", "B", "B", "B", "A", "B", "B", "A", "B",
"B", "B", "A", "A", "A", "A", "A", "A", "A", "A", "B",
"A", "B", "A", "B", "A", "A", "A", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"A", "A", "B", "A", "B", "A", "B", "B", "B")),
class = "data.frame", row.names = seq(1:141))
这里是三个轴按字母顺序排列的冲积图:
library("ggalluvial")
ggplot(data = taxa,
aes(axis1 = Order, axis2 = Genus, axis3 = Habitat, y = Freq)) +
stat_alluvium(aes(fill = Habitat)) +
geom_stratum(linetype = 1, lwd = 0.01) +
geom_text(stat = "stratum", infer.label = TRUE, size = 3) +
theme_void() +
theme(legend.position = "none")
正如@s_t 所建议的,这应该可以通过使 taxa$Genus
成为因子变量而不是字符变量来解决。但是 as.factor()
将因子水平按字母顺序排列,这不是您想要的。通过按照它们在 taxa
中出现的顺序指定级别,以下代码在第二个轴中重新排序层,同时保持其他轴不变。结果情节是你想要的吗? (省略了从您的问题复制的代码。)
# ensure that 'Genus' is a factor with levels in order of appearance
taxa$Genus <- factor(taxa$Genus, levels = as.character(unique(taxa$Genus)))
# plot
ggplot(data = taxa,
aes(axis1 = Order, axis2 = Genus, axis3 = Habitat, y = Freq)) +
stat_alluvium(aes(fill = Habitat)) +
geom_stratum(linetype = 1, lwd = 0.01) +
geom_text(stat = "stratum", infer.label = TRUE, size = 3) +
theme_void() +
theme(legend.position = "none")
由 reprex package (v0.3.0)
于 2020-05-16 创建
我想保持冲积地块的第二个轴与第一个轴的顺序相同。也就是说,由于第一个轴是第二个轴的更高级别的分类法,其主要目的是显示生物体的概览和分组,以增强图表的可读性。为此,我尝试手动订购地层。然而,我所做的只是重新洗牌矿脉而不是地层(例如从这个 tutorial 或玩 lode.guidance
)。
有人知道如何解决这个问题吗?最后,第一轴和第二轴之间的所有矿脉应该水平流动,然后从第二轴到第三轴按原样排序。
数据的简短版本(仍然很广泛,抱歉):
taxa <- structure(list(Order = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L,
32L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L),
.Label = c("Adinetida", "Cercomonadidae_or",
"Cercozoa_unclassified",
"Chaetopeltidales",
"Chlorophyta_ph_unclassified",
"Chromulinales",
"Chrysophyceae_unclassified",
"Chytridiomycetes_unclassified",
"Conthreep", "Craspedida_or",
"Cryomonadida", "Cryptomonadales",
"Cystobasidiales",
"Cystobasidiomycetes_unclassified",
"Cystofilobasidiales",
"Dinophyceae_unclassified",
"Diplogasterida",
"Glissomonadida_or",
"Helotiales", "Imbricatea_unclassified",
"Incertae_Sedis",
"Intramacronucleata_unclassified",
"Leotiomycetes_unclassified", "LG08-10_or",
"Litostomatea", "Monhysterida",
"Ochromonadales",
"Ochrophyta_ph_unclassified",
"Parachela",
"Peronosporomycetes_or",
"Phragmoplastophyta_unclassified",
"Saccharomycetales",
"Spirotrichea", "Spongomonadida",
"Thecofilosea_unclassified",
"Tremellales", "Tremellomycetes_or",
"Tremellomycetes_unclassified",
"Trichosporonales"),
class = "factor"),
Genus = c(paste(rep("Poterioochromonas", 19)),
paste(rep("Colpoda", 9)),
paste(rep("Colpodea_unclassified", 24)),
paste(rep("Colpodida_ge", 28)),
paste(rep("Conthreep_unclassified", 4)),
"Cryptocaryon", "Cyclidium",
paste(rep("Nassophorea_unclassified", 2)),
"Platyophrya", paste(rep("Tetrahymena", 5)),
paste(rep("uncultured", 3)),
paste(rep("uncultured_ge", 4)), "Glarea",
paste(rep("Helotiales_unclassified_ge", 13)),
paste(rep("Chrysolepidomonas", 12)), "Ochromonas",
paste(rep("Debaryomycetaceae_unclassified", 3)),
"Pichiaceae_unclassified_ge",
paste(rep("Saccharomycetaceae_unclassified", 5)),
paste(rep("Yarrowia", 4))),
Freq = rep(1, 141),
Habitat = c("B", "B", "B", "B", "B", "B", "B","B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "A", "A", "A",
"B", "A", "A", "B", "B", "B", "B", "A", "A", "A", "A",
"B", "A", "A", "A", "A", "B", "A", "B", "B", "A", "B",
"B", "B", "A", "A", "A", "B", "B", "B", "A", "A", "A",
"A", "A", "A", "A", "A", "B", "A", "B", "A", "B", "B",
"A", "A", "B", "B", "A", "B", "A", "B", "B", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A",
"B", "A", "A", "B", "B", "B", "A", "B", "B", "A", "B",
"B", "B", "A", "A", "A", "A", "A", "A", "A", "A", "B",
"A", "B", "A", "B", "A", "A", "A", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"A", "A", "B", "A", "B", "A", "B", "B", "B")),
class = "data.frame", row.names = seq(1:141))
这里是三个轴按字母顺序排列的冲积图:
library("ggalluvial")
ggplot(data = taxa,
aes(axis1 = Order, axis2 = Genus, axis3 = Habitat, y = Freq)) +
stat_alluvium(aes(fill = Habitat)) +
geom_stratum(linetype = 1, lwd = 0.01) +
geom_text(stat = "stratum", infer.label = TRUE, size = 3) +
theme_void() +
theme(legend.position = "none")
正如@s_t 所建议的,这应该可以通过使 taxa$Genus
成为因子变量而不是字符变量来解决。但是 as.factor()
将因子水平按字母顺序排列,这不是您想要的。通过按照它们在 taxa
中出现的顺序指定级别,以下代码在第二个轴中重新排序层,同时保持其他轴不变。结果情节是你想要的吗? (省略了从您的问题复制的代码。)
# ensure that 'Genus' is a factor with levels in order of appearance
taxa$Genus <- factor(taxa$Genus, levels = as.character(unique(taxa$Genus)))
# plot
ggplot(data = taxa,
aes(axis1 = Order, axis2 = Genus, axis3 = Habitat, y = Freq)) +
stat_alluvium(aes(fill = Habitat)) +
geom_stratum(linetype = 1, lwd = 0.01) +
geom_text(stat = "stratum", infer.label = TRUE, size = 3) +
theme_void() +
theme(legend.position = "none")
由 reprex package (v0.3.0)
于 2020-05-16 创建