我想为我的 ggplot 图添加标签。该图是使用“freq”列创建的,其中包含先前计算的百分比值。在图表的顶部,我想添加每组“n”变量而不是预定义变量的总和。我在尝试这样做时我的代码有错误
vgene_group_IGH <-
Acute_Bcells_dataset_IGH %>%
group_by(codigo, vgene) %>%
summarise(n = n()) %>%
mutate(freq = n/sum(n)*100) %>%
arrange(desc(n)) %>%
ungroup() %>%
mutate(vgene = reorder(vgene, freq))
#ploting
ggplot(vgene_group_IGH, aes(x = reorder(as.factor(codigo), -freq, FUN = sum), y = freq, fill = vgene)) +
geom_bar(stat = "identity") +
xlab("codigos") +
ylab("V-gene expression(%)") +
scale_fill_manual(values = setNames(colors, unique_vgene)) +
geom_text(
aes(label = after_stat(n), group = codigo),
stat = 'summary', fun = sum, vjust = -1
)