简体   繁体   English

绘制R中表格中的前5个值

[英]plotting the top 5 values from a table in R

I'm very new to R so this may be a simple question. 我对R很新,所以这可能是一个简单的问题。 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. 表中约有50种。 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. 我希望有一个堆积的条形图,其中前5个物种的百分比和一个“其他”类别具有所有其他百分比的总和。 So my barplot would have 6 categories total (top 5 and other). 所以我的条形图总共有6个类别(前5名和其他)。

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. 我有3个额外的数据集(样本网站),我想做同样的事情,只在每个数据集中突出显示第一个数据集的前5个,并将它们全部放在同一个图表上。 The final graph would have 4 stacked bars showing how the top species in the first dataset change in each additional dataset. 最终图表将有4个堆叠条形图,显示第一个数据集中的顶级物种如何在每个附加数据集中发生变化。

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 我手工绘制了一个样本图(将数据列在R之外,并在最终的百分比表中输入),让您了解我正在寻找的内容: 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. 我想将这些步骤放入R脚本中,以便为多个数据集创建这些图。

Thanks! 谢谢!

Say your data is in the data.frame DF 假设您的数据位于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 然后,您可以确定哪些物种位于前5位

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). 使Group成为一个因子使它们在DF.summary保持相同的顺序(每个第一个数据集的最大到最小)。

Then you just put them together and plot them as you did in your example. 然后你就把它们放在一起并像你在你的例子中那样绘制它们。

We should make it a habit to use data.table wherever possible: 我们应该养成尽可能使用data.table的习惯:

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM