2017-01-31 12 views
0

3つの異なる季節に2つの場所で分類群の量を記述する積み重ねたバカルートを作りたいと思います。私はggplot2を使用しています。プロットを作るのは大丈夫ですが、私は48の分類群を持っていますので、バーにはさまざまな色がたくさんあります。頻繁かつ豊富に発生する分類群が8つしかないので、他の分類群を「その他」にグループ化してプロットしたいと考えています。「その他」(ggplot2を使用)を含むR積み重ね棒グラフ

私のデータは次のようになります。

y <- rowSums(invert[6:54]) 
x<-invert[6:54]/y 
x<-invert[,order(-colSums(x))] 

#Extract list of top N Taxa 
N<-8 
taxa_list<-colnames(x)[1:N] 

#remove "__Unknown__" and add it to others 
taxa_list<-taxa_list[!grepl("Unknown",taxa_list)] 
N<-length(taxa_list) 

#Generate a new table with everything added to Others 
new_x<-data.frame(x[,colnames(x) %in% taxa_list], 
       Others=rowSums(x[,!colnames(x) %in% taxa_list])) 
df<-NULL 
for (i in 1:dim(new_x)[2]){ 
    tmp<-data.frame(row.names=NULL,Sample=rownames(new_x), 
    Taxa=rep(colnames(new_x)[i],dim(new_x) [1]),Value=new_x[,i],Type=grouping_info[,1]) 
    if(i==1){df<-tmp} else {df<-rbind(df,tmp)} 
} 

グラフをプロットするには:

SampleID  TransectID  SampleYear  Season  Location Taxa1  Taxa2  Taxa3 .... Taxa48 
BW15001    1   2015  fall  SiteA   25   0   0   0 
BW15001    2   2015  fall  SiteA   32   0   0   2 
BW15001    2   2015  fall  SiteA   6   0  45   0 
BW15001    3   2015  fall  SiteA   78   1   2   0 

これは私が(hereから変更)しようとしたものです

colours <- c("#F0A3FF", "#0075DC", "#993F00","#4C005C","#2BCE48","#FFCC99","#808080","#94FFB5","#8F7C00","#9DCC00","#C20088","#003380","#FFA405","#FFA8BB","#426600","#FF0010","#5EF1F2","#00998F","#740AFF","#990000","#FFFF00"); 

library(ggplot2) 
p<-ggplot(df,aes(Sample,Value,fill=Taxa))+ 
    geom_bar(stat="identity")+ 
    facet_grid(. ~ Type, drop=TRUE,scale="free",space="free_x") 
p<-p+scale_fill_manual(values=colours[1:(N+1)]) 
p<-p+theme_bw()+ylab("Proportions") 
p<-p+ scale_y_continuous(expand = c(0,0))+ 
    theme(strip.background = element_rect(fill="gray85"))+ 
    theme(panel.spacing = unit(0.3, "lines")) 
p<-p+theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) 
p 

メイン私が今日援助したいと思う問題は、主要な分類群を引き出し、残りを「その他」とすることです。私は、後でfacet_grid()を使って季節と場所でグラフをグループ化する方法を考え出すことができると思います...

ありがとう!それをやっての

+0

方法あなたは主な分類群を定義しますか?全体的な発生?発生頻度? – Haboryme

+0

['forcats'](https://blog.rstudio.org/2016/08/31/forcats-0-1-0/)パッケージを見てください。具体的に 'fct_lump()' – JasonAizkalns

+0

主要な分類は全存在量の90%を超えています。 – ayesha

答えて

0

一つの方法:

library(plyr) 
d=data.frame(SampleID=rep('BW15001',4), 
      TransectID=c(1,2,2,3), 
      SampleYear=rep(2015,4), 
      Taxa1=c(25,32,6,78), 
      Taxa2=c(0,0,0,1), 
      Taxa3=c(0,0,45,3)) 
#Reshape the df so that all taxa columns are melted into two 
d=melt(d,id=colnames(d[,1:3])) 
d$variable=as.character(d$variable) 

# rename all uninteresting taxa as 'other' 
`%ni%` <- Negate(`%in%`) # Here I decided to select the ones to keep, but the other way around is fine as well of course 
d[d$variable %ni% c('Taxa1','Taxa2'),'variable']='Other' #here you could add a function to automatically determine which taxta you want to keep, as you already did 

# aggregate all data for 'other' 
d=ddply(d,colnames(d[,1:4]),summarise,value=sum(value)) 

#make your plot, this one is just a bad example 
ggplot(d,aes(SampleID,value,fill=variable))+ 
    geom_bar(stat="identity")+ 
    facet_grid(. ~ Type, drop=TRUE,scale="free",space="free_x") 
+0

ありがとうございます。どの分類を自動的に保存するかを決定する関数を追加する方法を説明できますか?例えば、試料の90%未満に存在するもの、または全存在量の90%超を占めるものは? (または両方。) – ayesha

0

私のコメントに拡大。 forcatsパッケージをご覧ください。完全な例がなければ、それは言うのは難しいのですが、以下は動作するはずです:

library(tidyverse) 
library(forcats) 

temp <- df %>% 
    gather(taxa, amount, -c(1:5)) 

# Reshape the data so that that there is one record per each amount 
tidy_df <- temp[rep(rownames(temp), times = temp$amount), ] 

tidy_df %>% 
    select(-amount) %>% 
    mutate(taxa = fct_lump(taxa, n = 2)) %>%  # Check out this line 
    ggplot(., aes(x = SampleID, fill = taxa)) + 
    geom_bar() 

あなたはグループにfct_lump(taxa, n = 8)にトップ8カテゴリをfct_lump(taxa, n = 2)を変更することができます。また、fct_lump(taxa, prop = 0.9)を使用すると、物事を一定割合で整理することができます。あなたは、単に(値または量をしていない)サンプル中分類群の「存在」の後に予定している場合

、物事は少し単純で、おそらく1本のパイプで扱うことができます。

df %>% 
    gather(taxa, amount, -c(1:5)) %>% 
    mutate(amount = na_if(amount, 0)) %>% 
    na.omit() %>% 
    mutate(taxa = fct_lump(taxa, n = 2)) %>% 
    ggplot(., aes(x = SampleID, fill = taxa)) + 
    geom_bar() 
関連する問題