位置百分比条形图

时间:2021-07-31 23:44:44

标签: r ggplot2 bar-chart percentage

我正在制作以下情节:

enter image description here

在根据需要调整右下方条形图时,我遇到了问题。我已设法显示每个条形的百分比,但数字未居中。我想将每个条形的百分比放置在其各自条形的末尾,或者在绘图边缘的另一个下排成一行。

我已经检查了几个 SO 问题,但是,尽管它们帮助我改进,但我无法获得预期的最终结果: geom_text how to position the text on bar as I want? Position geom_text on dodged barplot Showing data values on stacked bar chart in ggplot2

这是我尝试过的最后一个代码和我的数据示例。

  C_ordered3 <- DF_Cplot %>%
  mutate(Gene = fct_reorder(Gene, desc(n), .fun = "length")) %>%
  ggplot(aes(x = Gene, y = gene_freq_percent, fill = factor(Variant_type), label = scales::percent(gene_freq_percent, accuracy = 0.1))) +
  geom_bar(stat = "identity") +
  geom_text(aes(vjust = 0.2, hjust = -11), size = 3) +
  coord_flip(expand = 0) +
  theme_classic() +
  labs(x = '', y = '') +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%')) +
  theme(legend.position = "none",        
        axis.line = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.x = element_blank(),
        plot.margin = unit(c(0, 0, 0, 0), "cm")) +
  scale_fill_manual(values = c("#3C5488B2","#91D1C2B2","#E64B35B2","gold", "#009E73"))


> dput(DF_Cplot_SOverflow)
structure(list(Patient_ID = c("A", "B", "C", "C", "C", "C", "C", 
"D", "E", "F", "F", "G", "H", "H", "H", "I", "I", "J", "J", "K", 
"L", "L", "M", "M", "N", "O", "O", "O", "P", "P", "P", "Q", "Q"
), Gene = c("DNMT3A", "U2AF1", "SRSF2", "SRSF2", "SRRM2", "SETBP1", 
"SNRNP200", "U2AF1", "SF3B1", "JAK2", "JAK2", "U2AF1", "TET2", 
"SF3B1", "ASXL1", "SF3B1", "SRSF2", "SRSF2", "PRPF40B", "U2AF1", 
"SRSF2", "ZRSR2", "ZRSR2", "PRPF8", "SETBP1", "TET2", "TET2", 
"NRAS", "TET2", "TET2", "ZRSR2", "TET2", "TET2"), Variant_type = c("Missense", 
"Missense", "Missense", "Missense", "Missense", "Missense", "Missense", 
"Missense", "Missense", "Missense", "Missense", "Missense", "Nonsense", 
"Missense", "Frameshift", "Missense", "Missense", "Missense", 
"Missense", "Missense", "Missense", "Missense", "Frameshift", 
"Missense", "Missense", "Missense", "Nonsense", "Missense", "Frameshift", 
"Nonsense", "Nonsense", "Frameshift", "Nonsense"), n = c(98L, 
42L, 120L, 120L, 53L, 37L, 13L, 42L, 214L, 21L, 21L, 42L, 346L, 
214L, 152L, 214L, 120L, 120L, 7L, 42L, 120L, 51L, 51L, 14L, 37L, 
346L, 346L, 28L, 346L, 346L, 51L, 346L, 346L), n2 = c(1L, 1L, 
5L, 5L, 5L, 5L, 5L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 1L, 2L, 2L, 2L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L), 
    total_gene_count = c(1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 
    1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 
    1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 
    1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L, 1875L
    ), gene_freq_percent = c(0.052266667, 0.0224, 0.064, 0.064, 
    0.028266667, 0.019733333, 0.006933333, 0.0224, 0.114133333, 
    0.0112, 0.0112, 0.0224, 0.184533333, 0.114133333, 0.081066667, 
    0.114133333, 0.064, 0.064, 0.003733333, 0.0224, 0.064, 0.0272, 
    0.0272, 0.007466667, 0.019733333, 0.184533333, 0.184533333, 
    0.014933333, 0.184533333, 0.184533333, 0.0272, 0.184533333, 
    0.184533333)), class = "data.frame", row.names = c(NA, -33L
))

结果:

enter image description here

任何帮助将不胜感激!谢谢!!

0 个答案:

没有答案
相关问题