Home > other >  barplot with ggplot fill by a column of dataframe
barplot with ggplot fill by a column of dataframe

Time:06-27

I was trying to create a barplot using a column of dataframe to fill the bars, but the bars result empty. the head of my df dataframe (13.000 rows) is this:

  head(metadata)
                                membership condition timepoint                 rep            barcode
   AAACAGCCACCTGCTC-1           3          WT        d3                        r1                      AAACAGCCACCTGCTC-1
   AAACATGCAGGAATCG-1           4          WT        d3                        r1                      AAACATGCAGGAATCG-1
   AAACCAACAATATAGG-1           3          KO        d3                        r1                      AAACCAACAATATAGG-1
   AAACCGAAGCGAGTAA-1           3          KO        d3                        r1                      AAACCGAAGCGAGTAA-1
   AAACCGAAGCGATAAG-1           3          KO        d5                        r1                      AAACCGAAGCGATAAG-1
   AAACCGAAGGATAAAC-1           4          KO        d5                        r1                      AAACCGAAGGATAAAC-1

What I've tried is:

    ggplot(metadata, aes(x=treatment, fill=membership))   geom_bar(position = "fill")  
        theme(axis.text.x=element_text(angle=45, hjust=1))    geom_hline(yintercept = 0.50)

but the result is: enter image description here

while I was expecting the bars filled by the distributions of clusters (membership) in the two conditions. Here I plot an example of what I want to do:enter image description here

CodePudding user response:

It could be because the membership column is numerical, make sure it to be factor/character.

  • Related