Friday, 3 November 2017

A stacked barplot in R

I wanted to make a barplot showing the 96 barcodes adjacent to each other, and for each barcode, a stack showing the number of reads for plate 1, 2, 3.

Getting the data into R (painful!)
The problem was getting my data into R. The input data did not have values for every plate-barcode combination, but I wanted to assume a value of 0 for combinations that were not in the input file. In the end I had to write some code to squeeze the data into R:

Plotting the data in R (ok!)
Plotting the data was not so hard. I used the example from http://www.r-graph-gallery.com/ to make a stacked barplot:colnames <- seq(1,96)rownames <- seq(1,3)colnames(mymatrix) <- colnamesrownames(mymatrix) <- rownamesbarplot(mymatrix, col=colors()[c(23,89,12)], border="white", space=0.04, font.axis=2, xlab="barcode", ylab="thousands of input reads", legend=rownames(mymatrix))

A little bit of the plot:

Some other little tricks I learnt:To put some space around the plot I can type before the 'barplot' command:par( mar=c(8, 4.7, 2.3, 0)) # last value is space on RHS, second last value is space at top, 2nd value is space on LHS, 1st value is space below

In the barplot command itself: border="white": use white for the border of the barsspace=0.04 : leaves space before each bar. cex.names=0.5 makes the x-axis labels smallerlas=3 makes the labels perpendicular to the axis