我有多个数据库,来自13个地点的每一个,有90个不同物种的平均丰度数据,按比例缩小如下:

 site1<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))
 site2<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))
 site3<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))
 site4<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))  

我想要实现的是一个条形图的网格,每个地点最丰富的10个物种按降序排列.我使用用途:

 site1plot<-ggplot(head(site1,10), aes(x=reorder(species, - abundance), y=abundance, fill = species)) + geom_bar(stat="identity")
 site2plot<-ggplot(head(site2,10), aes(x=reorder(species, - abundance), y=abundance, fill = species)) + geom_bar(stat="identity")
 site3plot<-ggplot(head(site3,10), aes(x=reorder(species, - abundance), y=abundance, fill = species)) + geom_bar(stat="identity")
 site4plot<-ggplot(head(site4,10), aes(x=reorder(species, - abundance), y=abundance, fill = species)) + geom_bar(stat="identity")
 ggarrange(site1plot,site2plot,site3plot,site4plot, common.legend = TRUE, legend = "bottom")

我的问题是,因为不同的网站都有不同的前10个物种,图例不适合所有的,即使我可以使它包括所有的物种,我需要的图表都有不同的 colored颜色 ,每个物种取决于顺序.如果我不做共同的图例,图表变得超级混乱,因为我有13个站点,并有他们网格与图例下面的每个是有点多.希望有个简单的解决办法?任何帮助非常感谢!

我的问题是,前10个物种在不同的地点不同,

推荐答案

另一个可能的 Select 是使用方面来组合和绘制所有数据,例如,

library(tidyverse)
library(tidytext)

site1<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))
site2<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))
site3<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))
site4<-data.frame(species = LETTERS[1:26], abundance = sample(x = 1:10, rep = TRUE, size = 26))  

bind_rows(site1, site2, site3, site4, .id = "site") %>%
  slice_max(n = 10, order_by = abundance,
            by = site, with_ties = FALSE) %>%
  ggplot(aes(x = reorder_within(x = species, by = -abundance,
                                within = site, sep = ""),
             y = abundance,
             fill = species)) +
  geom_col() +
  facet_wrap(~site, scales = "free_x",
             labeller = as_labeller(\(x) paste("Site:", x))) +
  guides(fill = guide_legend(ncol = 4)) +
  theme(legend.position = "bottom",
        axis.title.x = element_blank())

创建于2024—04—04,reprex v2.1.0

R相关问答推荐

如何在ggplot 2 geom_segment图表中将UTC转换为EET?

使用ggplot将平滑线添加到条形图

R箱形图gplot 2 4组但6个参数

使用strsplit()将向量操作为数据框

在连续尺度上转置标签[瀑布图,R]

使用R闪光显示所有数据点作为默认设置

将. xlsx内容显示为HTML表

如何通过匹配R中所有可能的组合来从宽到长旋转多个列?

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

Geom_Hline将不会出现,而它以前出现了

查找所有站点的最小值

我如何go 掉盒子图底部的数字?

将统计检验添加到GGPUBR中的盒图,在R

层次树图的数据树

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

如何从嵌套数据中自动创建命名对象?在R中

抽样变换-REXP与RWEIBUR

每行不同列上的行求和

在REST API中使用参数R

真实世界坐标的逆st_变换