2

我正在尝试从以下数据集和 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
4

1 回答 1

1

这是我用于生成大量图表的工作流程,适用于您的数据集(或者,我对它的解释)。plyr我认为,这很好地说明了 的力量。对于您的应用程序,我认为计算时间并不重要。对您来说更重要的是生成易于阅读的代码,我认为 plyr 对此有好处。

#Load packages
require(plyr)
require(reshape)
require(ggplot2)

#Recreate your data set, with only two species
setID <- rep(1:5, each=4, times=1)
fishery <- gl(10, 2)
blackdog <- sample(1:5, size=20, replace=TRUE)
smoothdog <- sample(1:5, size=20, replace=TRUE)
df <- data.frame(setID, fishery, blackdog, smoothdog)

#Melt the data frame
dfm <- melt(df, id.vars <- c("setID", "fishery"))

#Calculate LOGcpue for each fish at each fishery

cpueDF <- ddply(dfm, c("fishery", "variable"), summarise, LOGcpue = log(sum(value)/length(value)))

#Plot all the data in one (potentially huge) faceted plot.
#(I often use huge plots like this for onscreen analysis 
#  - obviouly it can't be printed in practice, but you can get a visual overview of the data)
ggplot(cpueDF, aes(x=fishery, y=LOGcpue)) + geom_bar() + coord_flip() + facet_wrap(~variable)
ggsave("giant plot.pdf", height=30, width=30, units="in")

#Print each plot individually to screen, and save it, and put it in a list
printGraph <- function(df) {
  p <-ggplot(df, aes(x=fishery, y=LOGcpue)) +
geom_bar() + coord_flip()
  print(p)
  fn <- paste(df$variable[1], ".png")
  ggsave(fn)
  printGraph <- p
}
plotList <- dlply(cpueDF, .(variable), printGraph)

#Now pick out the top n fisheries for each fish
cpueDFtopN <- ddply(cpueDF, .(variable), function(x) head(x[order(x$LOGcpue, decreasing=T),], n=5))
ggplot(cpueDFtopN, aes(x=fishery, y=LOGcpue)) + geom_bar() + 
  coord_flip() + facet_wrap(~variable, scales="free")

在此处输入图像描述

于 2012-10-18T16:10:08.540 回答