Aller au contenu. | Aller à la navigation

Outils personnels

Navigation

R stackedbarplot fig1

### 16s - frequencies stacked barplots - figure 1
# applied to taxotofreq_script output
# paper version


rm(list=ls())


setwd()

freq=read.table("filename_taxotofreq.txt", header=T, sep="\t")

# removal of unclassified sequences - Order
freq$Bacteria_unclassified = NULL
freq$Bacilli_unclassified = NULL

# removal of unclassified sequences - Family
freq$Lactobacillales_unclassified = NULL

# removal of unclassified sequences - Genus
freq$Var.2 = NULL


####################################################################################

library(RColorBrewer)
color_palette=function(freq){
all_table=colnames(freq)[4:ncol(freq)]
color=colorRampPalette(c("darkred","red","orange","yellow","green","blue", "purple","pink"))(length(all_table))
all_color=data.frame(Family=all_table, color)
return(all_color)
}


description_echantillon=function(ech, colors="new", sample_name="none", threshold=0, size=0.7, max=8){
# Sommes des fr?quences des ?chantillons choisis
subfreq=freq[freq$sample.ID%in%ech,]
rownames(subfreq)=subfreq[,1]
data_table=subfreq[,-c(1:3)]

# Proportions
data_prop=apply(data_table,1,function(x){round(100*x/sum(x),2)})
other=data_prop[rowSums(data_prop)<threshold,]
other=colSums(other)
data_prop=data_prop[rowSums(data_prop)>=threshold,]
fam=rownames(data_prop)
data_prop=data_prop[rowSums(data_prop)!=0,]
if(length(colors)==1){
colors=color_palette(subfreq[,c(1:3,match(rownames(data_prop),colnames(subfreq)))])
}
colors=colors[match(rownames(data_prop), colors$Family),]
data_prop=rbind(data_prop,other)
colors$Family=as.character(colors$Family)
colors=rbind(colors, c("other","#838B8B"))
# barplot
par(mfrow=c(1,2),mar=c(10,5,4,5), cex=size)
bp=barplot(data_prop, col=as.character(colors$color), horiz=T, las=1, xlim=c(0,100), width = 0.8, ylim=c(0,max))
# text(data_prop+5,bp, data_prop, col=as.character(colors[match(names(data_prop), colors$Family),]$color))
if(sample_name=="none"){
sample_title=paste(ech, collapse = " + ")
}else{
sample_title=sample_name
}
title(paste("proportion of each family in : \n", sample_title))
plot.new()
legend("top", legend=colors$Family[order(colors$Family,decreasing=T)], col=as.character(colors$color[order(colors$Family,decreasing=T)]), pch=15, cex=1.5)
}


COLORS=color_palette(freq)
COLORS$color=as.character(COLORS$color)


####################################################################################

## used for paper figures:
description_echantillon(ech=freq$sample.ID, color=COLORS, sample_name = "yw parents flies samples", threshold = 1, size=0.8)