首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图例标签添加到ggplot的条形图中。

将图例标签添加到ggplot的条形图中。
EN

Stack Overflow用户
提问于 2021-06-27 18:01:21
回答 1查看 361关注 0票数 3

我想将集群名称添加到ggplot中的堆叠桶图中。

正如你所看到的,有些星系团的分数很小,而且每个条形中有很多星系团。这使得我们很难看到哪些星系团是哪个,特别是在相似的颜色之间。理想情况下,我希望有选择地给一些大分数(或集群)贴上它的名称,比如当百分比大于5%时,显示它的集群名称。

例如,在示例‘朴素CD8 T’中,我希望将集群4和5标记为:

代码语言:javascript
复制
cluster.count %>% 
  ggplot(aes(x=Cell_subtype,y=count1, fill= seurat_clusters)) +
  geom_bar(stat="identity", position = 'fill')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-27 19:10:32

这一目标可以这样实现:

  1. ,而不是使用position="fill",我可以通过position="fill"标签手动计算百分比,然后可以通过geom_text轻松地添加,在geom_text中,您可以使用ifelse只显示所需的最小频率或比例的标签。

利用一些随机的示例数据,尝试如下:

代码语言:javascript
复制
library(ggplot2)
library(dplyr)

set.seed(42)
cluster.count <- data.frame(
  Cell_subtype = sample(LETTERS[1:4], 60, replace = TRUE),
  seurat_clusters = sample(0:16, 60, replace = TRUE)
)
cluster.count <- count(cluster.count, Cell_subtype, seurat_clusters, name = "count1")
cluster.count <- mutate(cluster.count, seurat_clusters = factor(seurat_clusters))

cluster.count %>% 
  group_by(Cell_subtype) %>% 
  mutate(pct = count1 / sum(count1)) %>% 
  ggplot(aes(x=Cell_subtype,y=pct, fill= seurat_clusters)) +
  geom_col() +
  geom_text(aes(label = ifelse(pct > .1, as.character(seurat_clusters), "")), position = position_stack(vjust = .5))

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68153954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档