Question: Generate a bar plot in R
gravatar for libya.tahani
2.2 years ago by
libya.tahani20 wrote:

Hi, How could I generate a bar plot in R like that I found in GEO2R profile graph to show the expression values of the gene across Samples ( comparing between two cell lines ) ?

My code for example ( I plane to use this data which I loaded it from GEO datasets) :-


# load series and platform data from GEO

gset <- getGEO("GSE7700", GSEMatrix =TRUE, AnnotGPL=TRUE)
if (length(gset) > 1) idx <- grep("GPL570", attr(gset, "names")) else idx <- 1
gset <- gset[[idx]]

# make proper column names to match toptable 
fvarLabels(gset) <- make.names(fvarLabels(gset))

# group names for all samples
gsms <- "1111000000"
sml <- c()
for (i in 1:nchar(gsms)) { sml[i] <- substr(gsms,i,i) }

# log2 transform
ex <- exprs(gset)
qx <- as.numeric(quantile(ex, c(0., 0.25, 0.5, 0.75, 0.99, 1.0), na.rm=T))
LogC <- (qx[5] > 100) ||
          (qx[6]-qx[1] > 50 && qx[2] > 0) ||
          (qx[2] > 0 && qx[2] < 1 && qx[4] > 1 && qx[4] < 2)
if (LogC) { ex[which(ex <= 0)] <- NaN
  exprs(gset) <- log2(ex) }

# set up the data and proceed with analysis
sml <- paste("G", sml, sep="")    # set group names
fl <- as.factor(sml)
gset$description <- fl
design <- model.matrix(~ description + 0, gset)
colnames(design) <- levels(fl)
fit <- lmFit(gset, design)
cont.matrix <- makeContrasts(G1-G0, levels=design)
fit2 <-, cont.matrix)
fit2 <- eBayes(fit2, 0.01)
tT <- topTable(fit2, adjust="fdr","B", number=250)

tT <- subset(tT, select=c("ID","adj.P.Val","P.Value","t","B","logFC","Gene.symbol","Gene.title"))
write.table(tT, file=stdout(), row.names=F, sep="\t")

I want to generate a bar plot for each gene individual like the image that I attached here. Profile Graph

R gene • 1.6k views
ADD COMMENTlink modified 2.2 years ago by Alex Reynolds31k • written 2.2 years ago by libya.tahani20

Please use the formatting bar (especially the code option) to present your post better. I've done it for you this time.

ADD REPLYlink written 2.2 years ago by Ram32k
gravatar for Alex Reynolds
2.2 years ago by
Alex Reynolds31k
Seattle, WA USA
Alex Reynolds31k wrote:

Starting with a data frame called d.

Make a column called sample that assigns each row/sample to a sample name GSM1234, etc.

Make a column called treatment, which is a categorical factor that assigns each row's sample to normal or cancer.

Make a column called expression, which contains the expression value (log-transformed, etc.) for the row/sample (or for its gene, if that's the correct interpretation of what you're plotting).

#!/usr/bin/env Rscript


# set up `d` per your code
# add `sample`, `treatment`, and `expression` columns per this answer

out_fn <- 'figure.pdf'

pdf(out_fn, onefile=F, width=7, height=5)
p <- ggplot(d, aes(group=sample)) + 
  geom_bar(aes(x=sample, y=expression, fill=treatment), width=0.75, position="dodge2", stat="identity") + 
  facet_grid(~treatment) +
  scale_colour_brewer(palette="Set1") + 
  scale_fill_brewer(palette="Set1") +
  ggtitle('Gene expression vs Sample') +
  xlab('Samples') +
  ylab('Expression') +
  theme(plot.title = element_text(size = 12, hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
ADD COMMENTlink modified 10 months ago • written 2.2 years ago by Alex Reynolds31k

If you're using Rscript, you're better off using ggsave(p, device='pdf') than pdf(); print(p);; IMO.

ADD REPLYlink modified 2.2 years ago • written 2.2 years ago by Ram32k

I'm editing the answer to change gene to sample (or cell line), which on a second read seems more appropriate for answering the question.

ADD REPLYlink written 10 months ago by Alex Reynolds31k
Please log in to add an answer.


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