在ggplot 中,我们可以使用geom_bar 来画柱状图。

    但默认下,柱状图并不需要定义y,我们只需要制定相应的x 或进一步的分组(fill 等),就会对数据进行计数。

    但在某些情况下,我们的数据框可能非常大,这时候就可以自己进行计数,然后告诉ggplot 即可。

    有两种指定y 的方式:

    1. geom_col
    2. geom_bar(stat = 'identity')

    它们的结果都是一样的。

    那么该如何分组计数呢?

    也非常简单,tidyverse 套件提供了group_by 分组以及summarise 函数,使用n() 计算。

    或者直接基础的table 搞定:

    1. ## count variant in each sample
    2. tmp1 <- table(mutation_number_order$name, mutation_number_order$Variant_Classification)
    3. tmp1 <- as.data.frame(tmp1)
    4. colnames(tmp1)[1:2] <- c("name", "Variant_Classification")
    5. > head(tmp1)
    6. name Variant_Classification Freq
    7. 1 S110011502DT Frame_Shift_Del 38
    8. 2 S110011501DT Frame_Shift_Del 41
    9. 3 S110020203DT Frame_Shift_Del 36
    10. 4 S110030206DT Frame_Shift_Del 43
    11. 5 S110030801DT Frame_Shift_Del 50
    12. 6 S110020201DT Frame_Shift_Del 49

    合并到原表格中,直接画就完事了:

    1. ## count variant in each sample
    2. tmp1 <- table(mutation_number_order$name, mutation_number_order$Variant_Classification)
    3. tmp1 <- as.data.frame(tmp1)
    4. colnames(tmp1)[1:2] <- c("name", "Variant_Classification")
    5. head(tmp1)
    6. tmp3 <- merge(mutation_number_order, tmp1, by = c("name", "Variant_Classification"))
    7. mutation_number_final <- unique(tmp3)
    8. colnames(mutation_number_final) <- c("Tumor_Sample_Barcode",
    9. "Variant_Classification",
    10. "Clinical_Type",
    11. "Total_Counts",
    12. "Counts")
    13. max_counts <- max(mutation_number_final$Total_Counts)
    14. # counts plot
    15. p1 <- ggplot(data = mutation_number_final) +
    16. geom_col(mapping = aes(x = Tumor_Sample_Barcode, y = Counts, fill = Variant_Classification), position = "stack") +
    17. barplot_theme + labs(x = NULL, size = 14) + scale_y_continuous(expand=c(0,0)) +
    18. coord_cartesian(ylim = c(0, max_counts + 100))
    19. (p1 <- p1 + labs(y = "Mutation Counts") + scale_x_discrete(expand = expansion(mult = c(0.03,0.05))))

    28. 自己算柱状图counts 节约数据框大小 - 图1

    y 也就是table 最后算出来的各组的数值,fill 分组的变量,再stack 也就堆积到一起啦。