Question: Generate a bar plot in R
0
gravatar for libya.tahani
12 weeks ago by
libya.tahani10
libya.tahani10 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) :-

library(Biobase)
library(GEOquery)
library(limma)

# 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 <- contrasts.fit(fit, cont.matrix)
fit2 <- eBayes(fit2, 0.01)
tT <- topTable(fit2, adjust="fdr", sort.by="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 • 267 views
ADD COMMENTlink modified 12 weeks ago by Alex Reynolds27k • written 12 weeks ago by libya.tahani10

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

ADD REPLYlink written 12 weeks ago by RamRS20k
3
gravatar for Alex Reynolds
12 weeks ago by
Alex Reynolds27k
Seattle, WA USA
Alex Reynolds27k wrote:

Starting with a data frame called d.

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

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

Make a column called expression, which contains the expression value (log-transformed, etc.) for the row/gene.

#!/usr/bin/env Rscript

library(ggplot2)

# set up `d` per your code
# add `gene`, `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=gene)) + 
  geom_bar(aes(x=gene, 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('Expression vs Genes') +
  xlab('Genes') +
  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))
print(p)
dev.off()
ADD COMMENTlink modified 12 weeks ago • written 12 weeks ago by Alex Reynolds27k

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

ADD REPLYlink modified 12 weeks ago • written 12 weeks ago by RamRS20k
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: 743 users visited in the last hour