Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting the top 5 values from a table in R

Tags:

plot

r

I'm very new to R so this may be a simple question. I have a table of data that contains frequency counts of species like this:

  Acidobacteria              47
  Actinobacteria            497
  Apicomplexa                 7
  Aquificae                  16
  Arthropoda                 26
  Ascomycota                101
  Bacillariophyta             1
  Bacteroidetes           50279
  ...

There are about 50 species in the table. As you can see some of the values are a lot larger than the others. I would like to have a stacked barplot with the top 5 species by percentage and one category of 'other' that has the sum of all the other percentages. So my barplot would have 6 categories total (top 5 and other).

I have 3 additional datasets (sample sites) that I would like to do the same thing to only highlighting the first dataset's top 5 in each of these datasets and put them all on the same graph. The final graph would have 4 stacked bars showing how the top species in the first dataset change in each additional dataset.

I made a sample plot by hand (tabulated the data outside of R and just fed in the final table of percentages) to give you an idea of what I'm looking for: http://dl.dropbox.com/u/1938620/phylumSum2.jpg

I would like to put these steps into an R script so I can create these plots for many datasets.

Thanks!

like image 856
helicase Avatar asked Sep 07 '11 18:09

helicase


People also ask

How do you get top 5 values in R?

To get the top values in an R data frame, we can use the head function and if we want the values in decreasing order then sort function will be required. Therefore, we need to use the combination of head and sort function to find the top values in decreasing order.

How do I find data points in R?

R offers the possibility to do that by conveniently using your mouse. The function “identify()” will allow you to determine which record correspond to a particular data point on the graph, simply by clicking on it.


2 Answers

Say your data is in the data.frame DF

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 in the top 5 by

top5Species <- DF[rev(order(DF$Count)),"Species"][1:5]

Each of the data sets can then be converted to these 5 and "Other" by

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)

Making Group a factor keeps them all in the same order in DF.summary (largest to smallest per the first data set).

Then you just put them together and plot them as you did in your example.

like image 126
Brian Diggs Avatar answered Oct 23 '22 03:10

Brian Diggs


We should make it a habit to use data.table wherever possible:

library(data.table)
DT<-data.table(DF,key="Count")
DT[order(-rank(Count), Species)[6:nrow(DT)],Species:="Other"]
DT<-DT[, list(Count=sum(Count),Pcnt=sum(Count)/DT[,sum(Count)]),by="Species"]
like image 21
andrekos Avatar answered Oct 23 '22 04:10

andrekos