I have a csv file where there are 9 columns and 15 rows. I was told to create a barplot of each of the rows using the par function. Here's my code
par(mfrow=c(3,5))
> barplot(c(as.matrix(matnew[1,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[2,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[3,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[4,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[5,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[6,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[7,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[8,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[9,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[10,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[11,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[12,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[13,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[14,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew), barplot(c(as.matrix(matnew[15,])), main='Gene Expression', xlab='Gene Name', ylab='Length',col=c(1,1,1,2,2,2,3,3,3), names.arg=names(matnew))))))))))))))))
and this gave me 15 barplots with data from each row. I wanted to know if there was a way where I could label each barplot with which row it is meaning barplot 1 is row 1, barplot 2 is row 2 (kinda like a subehading).
There's no reason to write out 15 separate calls to
barplot
.This is an appropriate case for using a
for
loop:You can also use whatever
*apply
family function you are most comfortable with, e.g.: