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ループです。コードは、1つの種に対して実行すると正常に機能しますが、すべてに対して実行すると、常に同じ棒グラフが得られます。たとえば、コードのどこかに[[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 に答える