R を使用して積み上げ棒グラフを作成しようとしています。主な問題点は、プロットの色列の色を適切に使用することです。
プロットの要件:
- 各バー (x 軸) は時間を表す必要があります。
- 各種は、豊富さ (y 軸) を反映するバープロット上のスペースを使用して、適切な色 (色の列で指定) にする必要があります。
- 各バー内で、同じ門の種をグループ化する必要があります。
- バーの幅を設定するのは非常に便利ですが、必須ではありません。
データセットの特徴:
- それぞれの種には個別の色があり、種の色は門によってグラデーションになっています。
- 時間内の種の存在量の合計は 100 になります。
- すべての種が常にあるわけではありません
- 7回、8門、132種あります
これらのデータを表現する方法に関する他のアイデアは大歓迎です。
代表的なデータ:
phyla species abundance color time
Actinobacteria Bifidobacterium_adolescentis 18.73529 #F7FBFF D30
Firmicutes Faecalibacterium_prausnitzii 14.118 #F7FCF5 D30
Firmicutes Catenibacterium_mitsuokai 12.51944 #F3F9F2 D30
Bacteroidetes Bacteroides_ovatus 7.52241 #FFF5EB D30
Firmicutes Faecalibacterium_prausnitzii 21.11866 #F7FCF5 D7
Firmicutes Ruminococcus_sp_5_1_39BFAA 13.54397 #92B09C D7
Actinobacteria Bifidobacterium_adolescentis 10.21989 #F7FBFF D7
Actinobacteria Bifidobacterium_adolescentis 38.17028 #F7FBFF D90
Firmicutes Catenibacterium_mitsuokai 11.04982 #F3F9F2 D90
Firmicutes Faecalibacterium_prausnitzii 9.82507 #F7FCF5 D90
Actinobacteria Collinsella_aerofaciens 5.2334 #D4DEE9 D90
前もって感謝します; これで頭を壁にぶつけています。
コードは Robert に感謝します。
#reshape the dataframes as matrices
#species are row names and times are columns (abundance data makes up matrix)
#put the matrix times in the correct order
#create stacked barplot that has the width of column reflecting shannon index
#save the stacked barplots in files named by the entry list
for(i in 1:n){
phyl=aggregate(abundance ~ phyla+species+color+time, dfs[[i]], sum)
phyl=phyl[with(phyl,order(phyla,species,time)),]
wide <- reshape(phyl, idvar = c("phyla","species","color"),
timevar = "time", direction = "wide")
wide[is.na(wide)]<-0
wide
res1=as.matrix(wide[,-c(1:3)],ncol=dim(wide[,-c(1:3)])[2])
colnames(res1)=
unlist(strsplit(colnames(res1), ".", fixed = TRUE)) [seq(2,length(colnames(res1))*2,by=2)]
rownames(res1)=wide$species
res1 <- res1[,c('E','FMT','PA','PF','D7','D30','D90')]
bar.width <- as.matrix(div.dfs[[i]]['frac'])
mypath <- file.path(output.path,paste(project.name, "_", lhs[i], ".tiff", sep = ""))
tiff(file=mypath)
mytitle = paste(project.name, lhs[i])
barplot(res1,col=wide$color,beside = F, width = c(bar.width), main = mytitle, legend.text=F,args.legend=
list(x = "top",bty="n",cex=.6,ncol=2))
dev.off()
rm(res1)
}
#makes the legend and exports as a eps file
setwd(output.path)
plot_colors <- database$color
text <- database$species
SetEPS()
postscript('legend.eps')
plot.new()
par(xpd=TRUE)
legend("center",legend = text, text.width = max(sapply(text, strwidth)),
col=plot_colors, lwd=1, cex=.2, horiz = F, ncol=2, bty='n')
par(xpd=FALSE)
dev.off()