Question: Plotting Bar charts in R
1
gravatar for michele.tebaldi.92
17 months ago by
michele.tebaldi.9230 wrote:

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 graph R • 604 views
ADD COMMENTlink modified 17 months ago by zx87549.7k • written 17 months ago by michele.tebaldi.9230

Do you have to use R?

ADD REPLYlink written 17 months ago by st.ph.n2.5k
2
gravatar for benformatics
17 months ago by
benformatics2.0k
ETH Zurich
benformatics2.0k wrote:
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...

ADD COMMENTlink modified 17 months ago • written 17 months ago by benformatics2.0k

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.

ADD REPLYlink modified 17 months ago • written 17 months ago by michele.tebaldi.9230

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)

ADD REPLYlink written 17 months ago by benformatics2.0k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 758 users visited in the last hour