Skip to content

Instantly share code, notes, and snippets.

@xie186
Last active March 5, 2019 03:48
Show Gist options
  • Save xie186/738261df7fd75626ce59ec315c5a8ca4 to your computer and use it in GitHub Desktop.
Save xie186/738261df7fd75626ce59ec315c5a8ca4 to your computer and use it in GitHub Desktop.
Barplot with coloring for different categories (classes)

How to donwload:

Click Download Zip button (topright).

How to use

Usage: Rscript barplot_with_class.R

Example: Rscript barplot_with_class.R demo.tab test.pdf

Input:

  • Col 1: Categories of GO term
  • Col 2: Description of the GO term
  • Col 3: Percentage: here you can also use -log10(p-value) or other types of value you want.

Output:

  • File name with a suffix of .pdf

Here is the figure generated using demo.tab: image

args = commandArgs(trailingOnly=TRUE)
# test if there is at least one argument: if not, return an error
if (length(args)!=2) {
stop("Two argument must be supplied (input file).n", call.=FALSE)
}
cc<-read.table(args[1], check.names=F, sep="\t",header=T)
### rownames(cc) <- paste(cc[,1], sprintf("%02d", cc[,2]), sep="::")
rownames(cc) = cc[,2]
cc = as.matrix(cc)
## Get category data
cat_all = cc[,c(1)]
cat_all
cat_list = unique(cat_all)
cat_list
cat <- factor(cat_all, levels = cat_list)
#head(cat)
cat_tab = table(cat)
pdf(args[2], height=2.8, width = 4)
par(mar=c(5.4,3,2.5,1), xpd=TRUE)
## Three colors are enough here.
col_list = c(rgb(152, 115, 0, max=255), rgb(90, 154, 215, max=255),
rgb(237, 125, 49, max=255), rgb(63, 83, 35, max=255),
rgb(255,192,0, max=255), rgb(113, 48,157, max=255),
rgb(111,176,74, max=255), rgb(63,101,141, max=255),
rgb(158, 72, 13, max=255), rgb(99,99,99, max=255), rgb(0, 0, 0, max=255))
### Get an vector of colors (same categories have the sample color for the bar).
col_all = c()
for(i in 1:length(cat_list)){
num = cat_tab[i]
cat(num, "\n")
col_all = c(col_all, rep(col_list[i], num))
}
tab <- cbind(as.numeric(cc[,c(3)]))
rownames(tab) = rownames(cc)
colnames(tab) = c("Percentage")
#tab = t(as.matrix(cc[,c(4)])))
head(tab)
ttab <- t(tab)
x_coor <- barplot(ttab, col = col_all, las=2, ylim=c(0,100), ylab="Percentage (%)",
border = NA, xlab = "", axes =F, beside = TRUE,
space=c(0.25, 0.25), xlim=c(0, nrow(tab)), width=0.8)
## Add text at top of bars
text(x = x_coor + 0.5, y = ttab[1,] + 7, label = ttab[1,], pos = 3, cex = 0.8, col = col_all, srt = 45)
sum = 0
## draw the rectange under the bar for each category
ratio = 1
for(index in 1:length(cat_tab)){
#cat(cat_tab[index], "\n")
rect(sum*ratio, -10, (sum + cat_tab[index])*ratio, -1, col=col_list[index], border = NA)
sum = sum + cat_tab[index]
}
### Write the category info inthe rectangle
sum = 0
for(index in 1:length(cat_tab)){
label = names(cat_tab)[index]
cat("Check ", index, ": ", label, "\n")
mtext(label, side=1, at= sum+as.numeric(cat_tab[index])/2, cex = 1,
line = -0.1, col=rgb(255,255,255, max=255))#, at= sum*1.2 + cat_tab[index]*1.2/0.5, outer=TRUE)
sum = sum + cat_tab[index]
}
cat(sum, "\n")
axis(side=2, at = 20*(0:5))
#legend(0, 130, legend=c(" "," "," "," "," "), fill= col_ligation, border = NA, bty = "n")
dev.off()
Category Description Percentage
MF IM08 77.6533866815935
MF IM14 79.4809007169441
MF IM11 82.4982000748021
CC LC01 86.5376279999999
CC LC09 84.5496594635102
CC LC08 88.5778479720064
BP LB08 82.2084337116626
BP LB14 88.1933421109348
BP LB06 81.1706626107194
BP LB02 84.7519189822505
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment