限制传奇中显示的项目数量-GGPLOT R

发布于 2025-02-12 05:50:10 字数 1946 浏览 2 评论 0原文

我有一个很大的分类数据集,我需要将其绘制为堆叠的条形图。示例数据:

ID  X   A   B   C   D   E   F   G
1   5   9   6   7   4   8   10  6
2   6   3   9   10  3   10  4   8
3   6   6   5   8   8   8   8   1
4   9   3   2   8   4   1   5   8
5   6   6   2   8   3   7   4   10
6   0   7   8   9   1   4   9   10
7   3   2   6   8   8   1   8   7
8   4   7   10  2   9   7   9   8
9   5   7   9   10  8   2   2   1
10  0   4   6   8   9   10  7   1
11  8   9   2   2   6   5   1   7
12  8   6   0   9   7   9   8   1
13  2   8   4   4   4   2   6   7
14  4   6   6   4   9   9   3   5
15  8   1   0   6   5   8   1   1
16  6   6   9   3   9   2   1   1
17  2   4   0   2   4   8   10  9
18  5   9   8   9   4   9   3   9
19  0   2   1   6   6   9   6   2
20  3   3   7   10  4   5   6   8
21  2   6   6   9   8   10  9   4
22  7   7   1   6   8   3   7   1
23  1   9   4   5   8   9   7   7
24  0   8   5   9   1   8   9   1
25  2   1   0   1   1   2   10  7
26  10  4   1   8   2   5   9   0
27  2   7   10  10  2   3   8   6
28  6   4   2   6   7   3   1   0
29  8   1   3   4   1   10  3   6
30  1   6   5   4   7   9   7   10
31  4   4   3   2   2   9   0   4
32  9   6   6   1   6   1   5   2

绘图零件没问题,使用GGGPLOT如下: -

l5 <- read.xlsx(paste(taxawmeta,taxawmeta_files[2], sep = ""), sheetIndex = 1)

l5_long <- l5 %>% gather(taxa,value,-c(X.FinalSampleID,TimePoint_Luna))

ggplot(l5_long, aes(fill=taxa, y = value, x = X.FinalSampleID, )) +
  geom_bar(position='stack', stat='identity') +
  theme_minimal() + 
  labs(x='Sample', y='Relative Abundance', title='Family Level Relative Abundance') +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position="none") 

我遇到问题的地方是实际数据集有近200个变量。这意味着传说完全失控。我知道我可以隐藏传说: -

theme(.position="none") 

...但是我想做的就是继续说十大条目,因为那是最令人感兴趣的条目。是否有任何简单的方法可以限制传说中显示的项目数量?到目前为止,我发现的任何东西似乎都非常令人费解,并且不直接适用于此问题。

I have a large taxonomic dataset that I need to plot as a stacked bar chart. Sample Data:

ID  X   A   B   C   D   E   F   G
1   5   9   6   7   4   8   10  6
2   6   3   9   10  3   10  4   8
3   6   6   5   8   8   8   8   1
4   9   3   2   8   4   1   5   8
5   6   6   2   8   3   7   4   10
6   0   7   8   9   1   4   9   10
7   3   2   6   8   8   1   8   7
8   4   7   10  2   9   7   9   8
9   5   7   9   10  8   2   2   1
10  0   4   6   8   9   10  7   1
11  8   9   2   2   6   5   1   7
12  8   6   0   9   7   9   8   1
13  2   8   4   4   4   2   6   7
14  4   6   6   4   9   9   3   5
15  8   1   0   6   5   8   1   1
16  6   6   9   3   9   2   1   1
17  2   4   0   2   4   8   10  9
18  5   9   8   9   4   9   3   9
19  0   2   1   6   6   9   6   2
20  3   3   7   10  4   5   6   8
21  2   6   6   9   8   10  9   4
22  7   7   1   6   8   3   7   1
23  1   9   4   5   8   9   7   7
24  0   8   5   9   1   8   9   1
25  2   1   0   1   1   2   10  7
26  10  4   1   8   2   5   9   0
27  2   7   10  10  2   3   8   6
28  6   4   2   6   7   3   1   0
29  8   1   3   4   1   10  3   6
30  1   6   5   4   7   9   7   10
31  4   4   3   2   2   9   0   4
32  9   6   6   1   6   1   5   2

The plotting part is no problem, using gggplot as below:-

l5 <- read.xlsx(paste(taxawmeta,taxawmeta_files[2], sep = ""), sheetIndex = 1)

l5_long <- l5 %>% gather(taxa,value,-c(X.FinalSampleID,TimePoint_Luna))

ggplot(l5_long, aes(fill=taxa, y = value, x = X.FinalSampleID, )) +
  geom_bar(position='stack', stat='identity') +
  theme_minimal() + 
  labs(x='Sample', y='Relative Abundance', title='Family Level Relative Abundance') +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position="none") 

Where I'm running into an issue is the actual dataset has almost 200 variables. Meaning the legend is completely out of control. I know I can just hide the legend with:-

theme(.position="none") 

... but what I'd like to do is keep say the top 10 entries as those are the ones of most interest. Is there any simple method to limit the number of items that are displayed in the legend? Anything I've found so far seems very convoluted and not directly applicable to this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文