This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Plotting Bar charts in R

Hi everyone, I'm new to R plotting functionalities. I'm trying to plot an horizontal stacked bar plot divided in three groups based on my table. The table is this:

head(chart_table)

                           Uniquely.mapped.Reads Non.uniquely.Mapped.Reads Nuclei      Sample
GC065783_ACTCGCTA-CTCTCTAT               5441281                    476072     50      pooled
GC065783_ACTCGCTA-TATCCTCT               7745233                    415674      1 single-cell
GC065783_ACTCGCTA-GTAAGGAG               5224144                    410210     50      pooled
GC065783_ACTCGCTA-ACTGCATA               6170632                    389391      1 single-cell
GC065783_ACTCGCTA-AAGGAGTA               7321056                    494480     25      pooled
GC065783_ACTCGCTA-CTAAGCCT               7799346                    485494      1 single-cell

and i'm trying to plot to plot it this way (i plotted it manually):

Screenshot-2019-05-07-at-19-29-53

Is there a simple way? Can someone help me?

plot r graph

Do you have to use R?

1 answer

library(ggplot2)
library(reshape2)

data <- chart_table
data$id <- row.names(data)
data$Nuclei <- NULL
data <- melt(data)
data$reads <- data$value/1e6
data$variable <- factor(data$variable,levels=c('Non.uniquely.Mapped.Reads','Uniquely.mapped.Reads'))

ggplot(data, aes(fill=variable, y=reads, x=id)) + 
  geom_bar( stat="identity") +
  coord_flip() +
  facet_grid(Sample~.,scales="free_y") +
  ylab('Number of reads (millions)') +
  xlab('')

A bit crude but it works... not sure what you wanted to do with the Nuclei information...

The Nuclei information is useless for visualization, i needed it before to classify samples into sampleType :) i tried your code, but there's a problem for data$variable, because it outputs only NAs. Can you explain me what are you trying to do there?

Many thanks! edit: I solved! Data$variable is already a factor, so that row of the code is useless! Thank you very much again.

ya i was just making it so that the non.uniquely.mapped reads were at the top of the stacked barchart (like in the example)

Log in to answer this question.