2

I have molecular sequencing data of relative abundance (in %) of the various phyla in 9 different samples and I am trying to plot it as colour-coded barchart (where each phyla corresponds to a different colour). Simple enough on excel, but for a complete newbie on R, I am struggling quite a bit. My data is in an excel format (formated as tabs), where the first line is the labels (e.g.sample name)- when plotting it, the bar labels are misplaced, do not match, and R plots the first line of my excel file (the names) as a separate value (pictures attached). What I have so far is:

attach(data) 
data.1<-as.matrix(data)
par(mfrow=c(1,1))
barplot(data.1, col=c("aquamarine3","azure2","blue2","brown3","cadetblue3","deepskyblue3","firebrick3","gold3","darkorange3","darkorchid3","darkseagreen","darkslateblue","darkviolet","deeppink4"), main=".", xlab="Unit/Treatment", ylab="% Relative abundance")
detach(data)

legend("topright", inset=c(-0.2,0),
       legend = c("Unassigned", "Acidobacteria","Actinobacteria","Bacteroidetes","Chlorobi","Chloroflexi","Firmicutes","Gemmatimonadetes","Planctomycetes","Proteobacteria","Verrucromicrobia","Euryarchaeota","Crenarchaeota","Parvarchaeota"), 
       fill = c("aquamarine3","azure2","blue2","brown3","cadetblue3","deepskyblue3","firebrick3","gold3","darkorange3","darkorchid3","darkseagreen","darkslateblue","darkviolet","deeppink4"))
par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
layout(mat, widths = rep.int(1, ncol(mat)),
       heights = rep.int(1, nrow(mat)), respect = FALSE)

As a result, I get this:

Barchart attempt, where R plots my sample names as x_1 and thus moves the other labels. Also, my legend covers the majority of my barchart and I cannot seem to adjust it.

Thanks very much in advance- any help with getting the barchart decently-looking would be highly appreciated.

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
Ikol
  • 21
  • 2
  • You're going to want `ggplot`. See if this thread gets you closer: https://stackoverflow.com/questions/44334535/ggplot2-add-ordered-category-labels-to-stacked-bar-chart – Matt Jul 25 '17 at 16:51
  • If you want to stick with base R, I would probably split the plot region into two (`layout()`) then draw the barplot minus legend in the first plot region, then draw an empty plot in the second region (turn off the box, axes, and annotation) and then draw the legend in that blank plot region. Without a reproducible example it is tedious to go beyond that. If you want to use **ggplot2** you may need to rearrange your data, but getting the legend would be automagical. – Gavin Simpson Jul 25 '17 at 20:12
  • Thanks a lot for the suggestions, I'll give ggplot2 a go. How would the data have to be rearranged, if i decide to go with ggplot2? – Ikol Jul 25 '17 at 21:21

0 Answers0