更改条形图顶部的预定义标签 - ggplot2

问题描述 投票:0回答:0

我想为我的 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
  )
r ggplot2 label bar-chart
© www.soinside.com 2019 - 2024. All rights reserved.