我做了几个图,我绘制了每种植物的10个细菌种类,我根据细菌所属的家族给条着色.虽然我可以做得很好,但我希望在不同的家庭和地块上保持相同的 colored颜色 ,因为稍后我想将它们组合成一个多面的图形,展示属于同一家庭的细菌属 colored颜色 相同是有意义的.

以下是我的数据和代码示例:

dput(top10_cintybus)
  structure(list(Order = c("Enterobacterales", "Enterobacterales", 
"Enterobacterales", "Sphingomonadales", "Enterobacterales", "Bacillales", 
"Hyphomicrobiales", "Bacillales", "Xanthomonadales", "Hyphomicrobiales"
), Family = c("Enterobacteriaceae", "Erwiniaceae", "Yersiniaceae", 
"Sphingomonadaceae", "Morganellaceae", "Bacillaceae", "Methylobacteriaceae", 
"Bacillaceae", "Xanthomonadaceae", "Rhizobiaceae"), MKC132 = c(0L, 
0L, 27L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC146 = c(33L, 8L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC227 = c(11L, 0L, 0L, 0L, 6L, 
2L, 0L, 3L, 0L, 0L), MKC231 = c(37L, 0L, 0L, 20L, 0L, 0L, 3L, 
0L, 2L, 2L), MKC242 = c(0L, 7L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L
), MKC276 = c(9L, 0L, 7L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC351 = c(6L, 
19L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Total = c(96, 34, 34, 20, 
6, 4, 3, 3, 2, 2), Genus = structure(c(10L, 8L, 9L, 7L, 6L, 5L, 
4L, 3L, 2L, 1L), levels = c("Allorhizobium", "Xanthomonas", "Bacillus", 
"Methylobacterium", "Heyndrickxia", "Arsenophonus", "Sphingomonas", 
"Pantoea", "Serratia", "???.1"), class = "factor")), row.names = c(NA, 
10L), class = "data.frame")

dput(top10_alappa)
 structure(list(Order = c("Enterobacterales", "Xanthomonadales", 
"Enterobacterales", "Enterobacterales", "Enterobacterales", "Enterobacterales", 
"Enterobacterales", "Hyphomicrobiales", "Enterobacterales", "Burkholderiales"
), Family = c("Erwiniaceae", "Xanthomonadaceae", "Enterobacteriaceae", 
"Erwiniaceae", "Enterobacteriaceae", "Enterobacteriaceae", "Enterobacteriaceae", 
"Rhizobiaceae", "Morganellaceae", "Oxalobacteraceae"), MKC154 = c(11L, 
0L, 36L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC167A = c(14105L, 0L, 
5810L, 13055L, 1223L, 2316L, 1276L, 0L, 550L, 13L), MKC167B = c(18L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC214 = c(83842L, 5L, 22936L, 
175L, 6828L, 94L, 0L, 7L, 0L, 0L), MKC226 = c(0L, 0L, 11L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L), MKC233 = c(55L, 0L, 13L, 14L, 0L, 0L, 
0L, 0L, 0L, 0L), MKC314 = c(0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 
0L), MKC364 = c(32L, 8L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC366 = c(38L, 
46599L, 0L, 10L, 62L, 0L, 0L, 549L, 0L, 512L), Total = c(98101, 
46612, 28806, 13257, 8113, 2410, 1276, 556, 550, 525), Genus = structure(10:1, levels = c("Janthinobacterium", 
"Morganella", "Allorhizobium", "Citrobacter", "Siccibacter", 
"Enterobacter", "Erwinia", "???.1", "Stenotrophomonas", "Pantoea"
), class = "factor")), row.names = c(NA, 10L), class = "data.frame")

我的代码为其中一个框架绘制图形作为示例.我可以按家族为条着色,但如果可能的话,我希望有一种方法使跨数据集的家族保持相同.注意,"家庭"是一个因子列.

species="Cintybus"
ggplot(data=top10_cintybus,aes(x=Genus, y=Total, fill=Family)) + 
  geom_bar(stat = "identity", colour="black") +
  coord_flip() +
  theme_bw() +
  scale_y_continuous(trans="log10", limits=c(1,1000000), labels=scales::comma,
                     expand=expansion(mult=c(0,.05))) +
  #theme(text=element_text(size=18)) +
  theme(axis.title.x = element_text(size=18)) +
  theme(axis.text.x = element_text(colour="black", face="bold", size=15)) +
  theme(axis.title.y = element_text(size=18, vjust=2.5)) +
  theme(axis.text.y = element_text(face="bold.italic", colour="black", size=15)) +
  geom_text(aes(label=format(Total, big.mark=",")), hjust=1.5, 
            color="white", fontface="plain", size=5) +
  xlab("Genus") + ylab("Abundance") +
  ggtitle(species) + theme(plot.title = element_text(size=18))

enter image description here

如果我为另一个数据帧运行上面的代码,这是一个图像.

enter image description here

例如,"Pantoea"属于欧文科,但该科在两个图的传说中有不同的 colored颜色 .我不想写一个长长的列表,我手动 for each 家族添加 colored颜色 ,因为在其他一些数据集中,我有100个家族,前10个将在数据集中混合.

推荐答案

首先要注意的是:因为你总共有几百个家庭,所以"十大家庭"的结合可能也是一个相当高的数字.每一个都需要一个独特的 colored颜色 ,很难区分它们.(即使是分类变量的10种 colored颜色 ,就像示例数据一样,也会推动它).

那就是说...

既然您提到无论如何您最终都会制作一个多面图,最简单的解决方案可能是使用facet_wrap(),这将生成一致的组合图例.在tidytext::reorder_within()scale_x_reordered()的帮助下,你的y轴可以很好地排序:

library(dplyr)
library(ggplot2)
library(tidytext)

bind_rows(Alappa = top10_alappa, Cintybus = top10_cintybus, .id = "Species") %>% 
  mutate(Genus = reorder_within(Genus, by = Total, within = Species)) %>% 
  ggplot(aes(x=Genus, y=Total, fill=Family)) + 
  geom_col(colour="black") +   # note geom_col() is equivalent to geom_bar(stat = "identity")
  geom_text(aes(label=format(Total, big.mark=",")), hjust=1.5, 
            color="white", fontface="plain", size=5) +
  coord_flip() +
  scale_x_reordered() +
  scale_y_continuous(trans="log10", limits=c(1,1000000), labels=scales::comma,
                     expand=expansion(mult=c(0,.05))) +
  facet_wrap(vars(Species), scales = "free", ncol = 1) +
  theme_bw() +
  theme(   # note you can put all your arguments in a single theme() call
    axis.title.x = element_text(size=18),
    axis.text.x = element_text(colour="black", face="bold", size=15),
    axis.title.y = element_text(size=18, vjust=2.5), 
    axis.text.y = element_text(face="bold.italic", colour="black", size=15)
  ) +
  xlab("Genus") + ylab("Abundance")

另一种方法是使用scale_fill_manual()来 for each 可能的值分配 colored颜色 .由于您可能有很多值,您可以自动执行以下操作:

library(ggplot2)
library(scales)

families <- list(top10_alappa, top10_cintybus) |>
  lapply(\(dat) dat$Family) |> 
  unlist() |>
  unique() |>
  sort()

family_pal <- viridis_pal(option = "H")(length(families)) 
names(family_pal) <- families

然后在原始代码的基础上 for each 绘图添加scale_fill_manual(values = family_pal),结果如下:

R相关问答推荐

如果窗口在CLARME或集团之外,则有条件领先/滞后滚动总和返回NA

在R中列表的结尾添加数字载体

为什么当我try 在收件箱中使用合并功能时会出现回收错误?

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

警告:lmdif:info = 0. nls. lm()函数的输入参数不正确

S用事件解决物质平衡问题

如何在geom_col中反转条

汇总数据表中两个特定列条目的值

从所有项的 struct 相同的两级列表中,将该第二级中的所有同名项绑定在一起

仅 Select 超过9行的CSV文件

R中有约束的优化问题:如何用复数和对数效益函数解决问题?

按时间顺序对不同事件进行分组

将向量元素重新排序为R中的第二个

如何创建累加到现有列累计和的新列?

如何阻止围堵地理密度图?

是否有可能从边界中找到一个点值?

按组和连续id计算日期差

构建一个6/49彩票模拟系统

需要一个函数来在第一行创建一个新变量,然后用新变量替换一个不同的变量(对于多行)

如何从矩阵绘制环弦图