|
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() |