Say your data is in the data.frame DF file
DF <- read.table(textConnection( "Acidobacteria 47 Actinobacteria 497 Apicomplexa 7 Aquificae 16 Arthropoda 26 Ascomycota 101 Bacillariophyta 1 Bacteroidetes 50279"), stringsAsFactors=FALSE) names(DF) <- c("Species","Count")
Then you can determine which species are at the top of 5 on
top5Species <- DF[rev(order(DF$Count)),"Species"][1:5]
Each of the data sets can then be converted to these 5 and “others” using
DF$Group <- ifelse(DF$Species %in% top5Species, DF$Species, "Other") DF$Group <- factor(DF$Group, levels=c(top5Species, "Other")) DF.summary <- ddply(DF, .(Group), summarise, total=sum(Count)) DF.summary$prop <- DF.summary$total / sum(DF.summary$total)
When creating a Group coefficient stores them all in the same order in DF.summary (from the largest to the smallest in the first data set).
Then you simply stack them and plot them in the same way as in your example.
source share