how can I make stacked barplot with ggplot2

前端 未结 1 408
你的背包
你的背包 2020-12-21 18:21

Hi I wanted to make a stacked barplot using ggplot2 with below data

Chr NonSyn_Snps Total_exonic_Snps
A01 9217    13725
A02 6226    9133
A03 14888   21531
A0         


        
相关标签:
1条回答
  • 2020-12-21 18:59

    The ggplot idiom works best with long data rather than wide data. You need to melt your wide data frame into long format to benefit from many of ggplot's options.

    # get data
    dat <- read.table(text = "Chr NonSyn_Snps Total_exonic_Snps
    A01 9217    13725
    A02 6226    9133
                      A03 14888   21531
                      A04 5272    7482
                      A05 4489    6608
                      A06 8298    12212
                      A07 6351    9368
                      A08 3737    5592
                      A09 12429   18119
                      A10 7165    10525", header= TRUE)
    
    
    # load libraries
    require(ggplot2)
    require(reshape2)
    
    # melt data from wide to long
    dat_m <- melt(dat)
    
    # plot
    ggplot(dat_m, aes(Chr, value, fill = variable)) + 
      geom_bar(stat = "identity") + 
      xlab("Chromosome") + 
      ylab("Number of SNPs")
    

    enter image description here

    0 讨论(0)
提交回复
热议问题