-1

I am shifting from excel to R for creating better plots and am working on creating stacked bar plot in R showing the three columns Percent.Exonic, Percent.Intronic and Percent.Intergenic in one stack for each of the samples. The values of these stack plot add upto 100. There are multiple columns after these three columns in my dataset.

I have large dataset which looks like this ( I read it from a text file to a table) -

Sample_ID   Gene_ID  Percent.Mapped.Reads   Percent.Exonic  Percent.Intronic    Percent.Intergenic Col7 Col8 Col9 Col10
AAl 79.46  ABC  85  10  5 . . .
AA2 83.39  X82 96   2   2 . . . 
AAL3     AB9292  91.89  90  5   5 . . . 
AAL5    uw20  89.34 90  0   10
PBS1    wiw0  82.4  88  2   12
PBS3    vh27  81.88 100 0   0
Imr90_Rep2_54   eg282 92.29 100 0   0
Imr90_Rep2_72   yrt363 90.62    100 0   0

[![enter image description here][1]][1]can someone please share on how to get this plot with all samples in it?

I have this code so far, but I am getting confused as few previous posts on this topic use as.matrix as well? -

alignment <- read.table("Alignment.txt")
barplot(alignment,
    names.arg = alignment,
    cex.names = 0.7, 
    col = sequential, 
    xlab = "Samples",
    ylab = "Percent mapped",
    xlim = c(0,20), 
    width = 1))

Thanks!

AnkP
  • 631
  • 2
  • 9
  • 18
  • 8
    `?barplot` is all you need. You'd better search "stacked barchart / barplot" on this site. Very likely there are 200+ duplicate targets. – Zheyuan Li Oct 20 '16 at 20:47
  • Yes I am doing that and testing, would I need to use as.matrix on my data? I saw it on few posts and I am getting little confused. – AnkP Oct 20 '16 at 20:52

1 Answers1

0

I got it to work based on one of the previous posts -

    Data <- subset(Input, select=c(1,7,8,9)) # Row required columns 
    data_m <- melt(Data)

   ggplot(dat_m, aes(data_m$Sample_ID, value, fill = variable)) + 
      geom_bar(stat = "identity") + 
      xlab("Chr") + 
      ylab("Number of SNPs")+ theme(axis.text.x = element_text(angle = 90, hjust = 0)

     })

Here is the original post - how can I make stacked barplot with ggplot2

Community
  • 1
  • 1
AnkP
  • 631
  • 2
  • 9
  • 18