How to label barplots with the row that the data is from?

46 views Asked by At

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

1

There are 1 answers

2
bcarlsen On

There's no reason to write out 15 separate calls to barplot.

This is an appropriate case for using a for loop:

# make simulated data since you did not provide any
my_matrix <- matrix(sample(10, 135, replace = TRUE), ncol = 9, nrow = row_count)

par(mfrow = c(3,5))

for (i in 1:nrow(my_matrix)) {
  barplot(
    my_matrix[i,],
    main = 'Gene Expression',
    sub = paste("Row", i),
    <your other arguments>
  )
}

You can also use whatever *apply family function you are most comfortable with, e.g.:

invisible(
  vapply(
    1:nrow(my_matrix),
    FUN = function(i) {
      barplot(
        my_matrix[i,],
        main = 'Gene Expression',
        sub = paste("Row", i),
        <your other arguments>
      )
      TRUE
    },
    FUN.VALUE = TRUE
  )
)