试图在R的循环外打印单个图

时间:2017-09-19 12:38:44

标签: r ggplot2

我将数据帧分成3个其他数据帧。我正在迭代它们并根据它绘制直方图。我将这些图存储在单独的图中。但是,在循环之外,我只能打印最后一次迭代的图而不是前两次。我能够在循环内打印所有三个图。这是我的代码:

catCust1 <- myData[(myData$meanVal > 0 & myData$meanVal <= 20),]
catCust2 <- myData[(myData$meanVal > 20 & myData$meanVal <= 40),]
catCust3 <- myData[(myData$meanVal > 40 & myData$meanVal <= 60),]

for(i in 1:3) {
  if(i == 1) {
    catCust <- catCust1
  } else if(i == 2) {
    catCust <- catCust2
  } else if(i == 3) {
    catCust <- catCust3
  } 

  catCust <- na.omit(catCust)
  numOrdersCatCust <- ddply(catCust, .(ORDERDATE), nrow)
  numOrdersCatCust$numDay <- 1:nrow(numOrdersCatCust)
  catCust$orderCount <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 2]
  catCust$numDay <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 3]
  setDT(catCust)[, uniqueCounter := .GRP, by = CUSTOMERID]
  apply(catCust, 2, function(x)length(unique(x)))
  threshold25catOne <- catCust[(catCust$uniqueCounter == as.integer(0.25 * max(uniqueCounter))), ]
  threshold25catOneVal <- threshold25catOne$numDay

  catCustPlot <- ggplot(data=catCust, aes(catCust$numDay)) +
    geom_histogram(binwidth = 1, col="black", fill="white", alpha=0.1) +
    labs(x="Day Number (Since 01-09-2016)", y="Orders") +
    ggtitle("GRAPH TITLE") +
    theme(plot.title = element_text(hjust = 0.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
    geom_vline(xintercept = threshold25catOneVal[1], color="purple") +
    annotate("text", x = threshold25catOneVal[1]-7, y = max(catCust$orderCount) + 1000, angle = 0, label = threshold25catOneVal[1], vjust = 1.2, parse = TRUE)
  # ABLE TO PRINT DIFFERENT PLOTS HERE
  if(i == 1) {
    catCustPlot1 <- catCustPlot
    print(catCustPlot1)
  } else if(i == 2) {
    catCustPlot2 <- catCustPlot
    print(catCustPlot2)
  } else if(i == 3) {
    catCustPlot3 <- catCustPlot
    print(catCustPlot3)
  } 
}

# PRINTS ONLY catCustPlot3
print(catCustPlot1)
print(catCustPlot2)
print(catCustPlot3)

其他两个图给我一个错误:

  

错误:美学必须是长度1或与数据相同

更新:head(myData)

meanVal  sumVal    countCat
75.98  75.98000    (60,80]
36.37  80.55727    (80,100]
50.96  52.67500    (40,60]
15.33  15.33000    (0,20]
17.48  27.65000    (20,40]
51.35 101.64900    (100,1e+04]

1 个答案:

答案 0 :(得分:0)

我只是简化了循环并将ggplot个对象存储到resPlots(没有打印输出)。循环结束后,您可以访问/打印它们。

myData <- na.omit(myData)
resPlots <- list()
for(i in 1:3) {
    if (i == 1) {
        catCust <- myData[(myData$meanVal > 0 & myData$meanVal <= 20), ]
    } else if (i == 2) {
        catCust <- myData[(myData$meanVal > 20 & myData$meanVal <= 40), ]
    } else if (i == 3) {
        catCust <- myData[(myData$meanVal > 40 & myData$meanVal <= 60), ]
    } 

    numOrdersCatCust <- ddply(catCust, .(ORDERDATE), nrow)
    numOrdersCatCust$numDay <- 1:nrow(numOrdersCatCust)
    catCust$orderCount <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 2]
    catCust$numDay <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 3]
    setDT(catCust)[, uniqueCounter := .GRP, by = CUSTOMERID]
    apply(catCust, 2, function(x)length(unique(x)))
    threshold25catOne <- catCust[(catCust$uniqueCounter == as.integer(0.25 * max(uniqueCounter))), ]
    threshold25catOneVal <- threshold25catOne$numDay

    resPlots[[i]] <- ggplot(catCust, aes(catCust$numDay)) +
        geom_histogram(binwidth = 1, col = "black", fill = "white", alpha = 0.1) +
        labs(x="Day Number (Since 01-09-2016)", y="Orders") +
        ggtitle("GRAPH TITLE") +
        theme(plot.title = element_text(hjust = 0.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
        geom_vline(xintercept = threshold25catOneVal[1], color="purple") +
        annotate("text", x = threshold25catOneVal[1]-7, y = max(catCust$orderCount) + 1000, angle = 0, label = threshold25catOneVal[1], vjust = 1.2, parse = TRUE)
}

# First plot    
resPlots[[1]]