我正在尝试从以下数据集和 for 循环中获取一系列图:
> head(all5new[c(6,70,22:23)])#This is a snapshot of my dataset. There is more species, see below.
setID fishery blackdog smoothdog
11 1 TRAWL-PAND.BOR. 0 0
12 1 TRAWL-PAND.BOR. 0 0
13 1 TRAWL-REDFISH 0 0
14 1 TRAWL-PAND.BOR. 0 0
21 10 TRAWL-PAND.BOR. 0 0
22 10 TRAWL-PAND.BOR. 0 0
> elasmo #This is the list of the species for which I would like to have individual barplots
[1] "blackdog" "smoothdog" "spinydog" "mako" "porbeagle"
[6] "blue" "greenland" "portuguese" "greatwhite" "mackerelNS"
[11] "dogfish" "basking" "thresher" "deepseacat" "atlsharp"
[16] "oceanicwt" "roughsagre" "dusky" "sharkNS" "sand"
[21] "sandbar" "smoothhammer" "tiger" "wintersk" "abyssalsk"
[26] "arcticsk" "barndoorsk" "roundsk" "jensensk" "littlesk"
[31] "richardsk" "smoothsk" "softsk" "spinysk" "thorny"
[36] "whitesk" "stingrays" "skateNS" "manta" "briersk"
[41] "pelsting" "roughsting" "raysNS" "skateraysNS" "allSHARK"
[46] "allSKATE" "PELAGIC"
这是我的 for 循环。当我为一个物种运行它时,代码运行良好,但是当我为所有物种运行它时,我总是得到相同的条形图。我知道它必须只是在代码中某处添加例如 [[i]] 的快速修复,但我尝试了不同的东西但没有任何成功。
for (i in elasmo) {
# CALUCLATE THE CATCH PER UNIT OF EFFORT (KG/SET) FOR ALL SPECIES FOR EACH FISHERY
test<-ddply(all5new,.(fishery),summarize, sets=length(as.factor(setID)),LOGcpue=log((sum(i)/length(as.factor(setID)))))
#TAKE THE FIRST 10 FISHERY WITH THE HIGHEST LOGcpue
x<-test[order(-test$LOGcpue)[1:10],]
#REORDER THE FISHERY FACTOR ACCORDINGLY (FOR GGPLOT2, TO HAVE EACH LEVEL IN ORDER)
list<-x$fishery
x$fishery <- factor(x$fishery, levels =list)
#BAR PLOT
graph<-ggplot(x, aes(fishery,LOGcpue)) + geom_bar() + coord_flip() +
geom_text(aes(label=sets,hjust=0.5,vjust=-1),size=4,angle = 270)
#SAVE GRAPH IN NEW DIR
ggsave(graph,filename=paste("barplot",i,".png",sep=""))
}
这是融化后我的数据集的一个子集:mydata。
> data.melt<-melt(all5new, id.vars=c("tripID","setID","fishery"), measure.vars = c(22:23))
> head(data.melt);dim(data.melt)
tripID setID fishery variable value
1 1 1 TRAWL-PAND.BOR. blackdog 0
2 1 1 TRAWL-PAND.BOR. blackdog 0
3 1 1 TRAWL-REDFISH blackdog 0
4 1 1 TRAWL-PAND.BOR. blackdog 0
5 1 10 TRAWL-PAND.BOR. blackdog 0
6 1 10 TRAWL-PAND.BOR. blackdog 0
[1] 350100 5