ggplot2循环仅保存/显示最后一个绘图

时间:2016-10-26 05:31:36

标签: r loops ggplot2 bar-chart

每当我使用我的代码生成一堆图形然后在页面中绘制它们时,只有最后一个图形被保存到列表中,所以我得到的是一个包含最后一个图形的页面。我希望你们能帮助我解决这个问题。

首先,我使用Cookbook for R的多时间代码(附后),然后输入以下代码:

`

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
plots <- c(list(...), plotlist)

  numPlots = length(plots)

   if (is.null(layout)) {
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
 }

if (numPlots==1) {
    print(plots[[1]])

  } else {
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    for (i in 1:numPlots) {
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                  layout.pos.col = matchidx$col))
    }
  }
}

dataset <-read.csv("Example.csv")
plot<-list()
for(i in 1:6){
hour<-dataset[,4*i-3]
Company<-dataset[,4*i-2]
Production<-dataset[,4*i-1]
plot<-ggplot()+
plot[[i]]<-geom_bar(aes(x=hour, y=Production, fill=Company),stat="identity") 
+ ggtitle("1/1/2016")
print(plot)
}
multiplot(plotlist=listname)

我的数据如下:

    structure(list(Hour = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 
24L, 24L,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2L, 1L, 4L, 4L, 3L, 4L, 4L, 4L, 2L, 2L, 4L, 1L, 1L, 1L, 1L, 
4L, 1L, 4L, 1L, 4L, 1L, 1L, 2L, 1L, 4L, 3L, 1L, 4L, 4L, 1L, 1L, 
4L, 4L, 4L, 1L, 4L, 4L, 3L, 4L, 4L, 1L, 2L, 4L, 1L, 4L, 1L, 4L, 
1L, 4L, 4L, 1L, 3L, 4L, 3L, 4L, 3L, 2L, 4L, 2L, 2L, 1L, 4L, 1L, 
1L, 2L, 4L, 3L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 4L, 1L, 3L, 
4L, 2L, 1L, 1L, 4L, 4L, 4L, 3L, 4L, 4L, 2L, 4L, 2L, 3L, 4L, 4L, 
2L, 2L, 4L, 1L), .Label = c("A", "B", "C", "D"), class = "factor"), 
    Production = c(73L, 25L, 73L, 21L, 140L, 6L, 196L, 103L, 
    123L, 93L, 137L, 53L, 82L, 72L, 27L, 160L, 135L, 112L, 17L, 
    42L, 67L, 32L, 89L, 54L, 92L, 122L, 185L, 177L, 80L, 137L, 
    58L, 79L, 39L, 173L, 111L, 28L, 178L, 81L, 136L, 30L, 170L, 
    181L, 151L, 19L, 47L, 114L, 159L, 142L, 51L, 78L, 26L, 160L, 
    138L, 170L, 142L, 5L, 145L, 146L, 161L, 137L, 143L, 175L, 
    87L, 110L, 162L, 104L, 40L, 123L, 111L, 28L, 41L, 143L, 21L, 
    164L, 151L, 77L, 46L, 83L, 40L, 74L, 55L, 82L, 13L, 74L, 
    131L, 130L, 123L, 125L, 29L, 44L, 148L, 16L, 111L, 21L, 134L, 
    200L, 176L, 11L, 192L, 167L, 41L, 71L, 184L, 114L, 175L, 
    179L, 40L, 54L, 74L, 168L, 110L, 51L, 168L, 123L, 75L, 42L, 
    146L, 172L, 171L, 142L, 5L, 38L, 190L, 164L, 72L, 18L, 104L, 
    35L, 118L, 167L, 68L, 97L, 153L, 74L, 57L, 23L, 108L, 15L, 
    41L, 41L, 76L, 170L, 75L, 171L, 178L, 49L, 146L, 44L, 124L, 
    80L, 106L, 30L, 164L, 87L, 145L, 153L, 28L, 50L, 100L, 25L, 
    30L, 179L, 198L, 41L, 63L, 30L, 71L, 183L, 67L, 159L, 139L, 
    48L, 183L, 97L, 124L, 180L, 87L, 185L, 7L, 190L, 179L, 196L, 
    189L, 129L, 10L, 89L, 165L, 98L, 147L, 73L, 28L, 192L, 59L, 
    70L, 75L, 105L, 112L, 40L, 108L, 131L, 182L, 12L, 179L, 123L, 
    125L, 46L, 74L, 115L, 77L, 109L, 25L, 164L, 154L, 88L, 180L, 
    184L, 113L, 47L, 47L, 88L, 35L, 185L, 26L, 27L, 106L, 57L, 
    190L, 34L, 54L, 39L, 169L, 98L, 69L, 179L, 178L, 114L, 66L, 
    167L, 58L, 117L, 199L, 23L, 185L, 6L, 180L, 87L, 69L, 117L, 
    69L, 131L, 191L, 131L, 167L, 186L, 49L, 142L, 177L, 199L, 
    74L, 128L, 196L, 164L, 107L, 98L, 139L, 82L, 131L, 149L, 
    193L, 68L, 126L, 15L, 115L, 151L, 185L, 33L, 34L, 111L, 72L, 
    14L, 34L, 18L, 85L, 195L, 81L, 191L, 138L, 111L, 21L, 71L, 
    143L, 30L, 101L, 89L, 93L, 188L, 63L, 108L, 133L, 79L, 30L, 
    179L, 139L, 9L, 112L, 40L, 196L, 8L, 135L, 6L, 165L, 14L, 
    104L, 9L, 140L, 78L, 114L, 11L, 191L, 81L, 94L, 47L, 161L, 
    66L, 142L, 64L, 168L, 9L, 76L, 150L, 42L, 90L, 14L, 189L, 
    135L, 51L, 31L, 95L, 40L, 95L, 61L), X = c(NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA), Hour.1 = c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 
    7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 
    13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
    13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
    14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
    15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 
    17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 
    18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 
    19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
    19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
    21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
    22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 
    23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 
    24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L), Company.1 = structure(c(3L, 
    4L, 4L, 3L, 1L, 3L, 3L, 1L, 4L, 1L, 3L, 4L, 4L, 1L, 4L, 1L, 
    4L, 4L, 2L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 2L, 1L, 2L, 2L, 4L, 
    4L, 1L, 2L, 2L, 3L, 1L, 3L, 2L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 
    1L, 4L, 3L, 2L, 2L, 4L, 4L, 4L, 3L, 4L, 2L, 4L, 4L, 4L, 1L, 
    2L, 4L, 4L, 1L, 4L, 1L, 3L, 4L, 4L, 3L, 2L, 4L, 4L, 2L, 1L, 
    2L, 4L, 4L, 3L, 3L, 2L, 4L, 2L, 3L, 3L, 1L, 4L, 2L, 1L, 1L, 
    2L, 3L, 4L, 1L, 4L, 3L, 3L, 4L, 4L, 4L, 1L, 3L, 1L, 3L, 4L, 
    3L, 1L, 4L, 3L, 4L, 1L, 2L, 4L, 3L, 2L, 4L, 4L, 1L, 2L, 3L, 
    4L, 1L, 4L, 1L, 4L, 4L, 3L, 2L, 4L, 2L, 1L, 4L, 1L, 1L, 1L, 
    1L, 4L, 2L, 4L, 2L, 4L, 4L, 1L, 4L, 2L, 2L, 1L, 3L, 2L, 2L, 
    1L, 4L, 2L, 1L, 3L, 4L, 4L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 4L, 
    1L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 4L, 4L, 1L, 2L, 3L, 4L, 
    3L, 3L, 2L, 1L, 3L, 3L, 2L, 4L, 2L, 4L, 1L, 3L, 4L, 3L, 2L, 
    4L, 2L, 3L, 1L, 2L, 3L, 3L, 1L, 2L, 4L, 4L, 4L, 1L, 4L, 4L, 
    2L, 1L, 4L, 4L, 1L, 3L, 1L, 4L, 2L, 1L, 4L, 4L, 2L, 1L, 2L, 
    4L, 1L, 2L, 1L, 2L, 4L, 4L, 4L, 1L, 4L, 3L, 4L, 1L, 4L, 4L, 
    2L, 4L, 4L, 2L, 2L, 4L, 3L, 4L, 1L, 2L, 1L, 4L, 4L, 1L, 4L, 
    1L, 1L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 2L, 1L, 1L, 3L, 4L, 
    1L, 1L, 3L, 4L, 4L, 1L, 3L, 2L, 2L, 4L, 2L, 4L, 3L, 1L, 4L, 
    2L, 4L, 3L, 3L, 1L, 4L, 1L, 1L, 2L, 1L, 2L, 2L, 4L, 3L, 2L, 
    1L, 2L, 2L, 2L, 4L, 4L, 3L, 4L, 2L, 1L, 4L, 2L, 2L, 1L, 2L, 
    2L, 2L, 4L, 1L, 1L, 4L, 1L, 4L, 2L, 4L, 4L, 1L, 1L, 4L, 4L, 
    2L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("A", 
    "B", "C", "D"), class = "factor"), Production.1 = c(30L, 
    11L, 73L, 124L, 138L, 73L, 64L, 133L, 40L, 144L, 47L, 107L, 
    165L, 90L, 177L, 43L, 91L, 175L, 127L, 35L, 152L, 62L, 114L, 
    154L, 63L, 77L, 141L, 22L, 113L, 34L, 74L, 194L, 34L, 43L, 
    108L, 45L, 12L, 59L, 150L, 151L, 101L, 18L, 26L, 49L, 92L, 
    10L, 18L, 196L, 34L, 76L, 144L, 199L, 146L, 61L, 166L, 62L, 
    118L, 127L, 182L, 21L, 165L, 192L, 69L, 46L, 50L, 111L, 15L, 
    136L, 58L, 139L, 170L, 146L, 113L, 143L, 71L, 174L, 19L, 
    104L, 23L, 75L, 68L, 176L, 32L, 155L, 10L, 62L, 15L, 98L, 
    13L, 127L, 64L, 96L, 159L, 119L, 197L, 101L, 22L, 74L, 129L, 
    7L, 110L, 48L, 111L, 177L, 34L, 105L, 61L, 173L, 160L, 70L, 
    9L, 59L, 146L, 129L, 66L, 113L, 88L, 148L, 99L, 187L, 76L, 
    183L, 114L, 196L, 19L, 22L, 117L, 179L, 172L, 92L, 42L, 75L, 
    128L, 84L, 75L, 142L, 29L, 29L, 185L, 72L, 121L, 38L, 165L, 
    198L, 98L, 26L, 123L, 8L, 159L, 195L, 19L, 198L, 6L, 32L, 
    34L, 119L, 166L, 36L, 98L, 135L, 24L, 165L, 57L, 135L, 86L, 
    163L, 114L, 26L, 159L, 28L, 119L, 89L, 199L, 180L, 64L, 10L, 
    181L, 141L, 80L, 113L, 22L, 75L, 79L, 88L, 179L, 133L, 176L, 
    163L, 160L, 19L, 124L, 123L, 187L, 48L, 25L, 71L, 37L, 119L, 
    50L, 97L, 14L, 133L, 166L, 14L, 175L, 97L, 183L, 105L, 133L, 
    20L, 119L, 180L, 21L, 30L, 133L, 186L, 179L, 58L, 66L, 104L, 
    102L, 149L, 73L, 144L, 162L, 167L, 167L, 26L, 76L, 37L, 192L, 
    178L, 52L, 139L, 125L, 86L, 157L, 153L, 18L, 54L, 174L, 25L, 
    36L, 124L, 128L, 61L, 156L, 109L, 81L, 12L, 112L, 11L, 185L, 
    158L, 61L, 87L, 50L, 156L, 193L, 33L, 157L, 120L, 6L, 36L, 
    53L, 101L, 52L, 124L, 146L, 104L, 186L, 17L, 139L, 141L, 
    139L, 118L, 178L, 194L, 154L, 186L, 94L, 53L, 42L, 154L, 
    15L, 91L, 188L, 123L, 100L, 79L, 151L, 196L, 50L, 126L, 13L, 
    138L, 47L, 131L, 53L, 36L, 94L, 109L, 142L, 46L, 169L, 57L, 
    118L, 47L, 113L, 197L, 25L, 156L, 94L, 163L, 59L, 141L, 120L, 
    190L, 169L, 71L, 13L, 64L, 12L, 173L, 98L, 89L, 114L, 56L, 
    121L, 81L, 168L, 41L, 62L, 53L, 150L, 170L, 174L, 156L, 19L, 
    77L, 37L), X.1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA), Hour.2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 
    13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 
    14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 
    15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
    17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
    18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 
    19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 
    21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
    22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
    22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 
    23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
    24L, 24L, 24L, 24L, 24L, 24L), Company.2 = structure(c(1L, 
    4L, 4L, 4L, 4L, 3L, 4L, 3L, 1L, 2L, 1L, 1L, 4L, 4L, 1L, 1L, 
    1L, 4L, 1L, 4L, 4L, 1L, 1L, 4L, 1L, 1L, 2L, 4L, 4L, 4L, 4L, 
    4L, 1L, 3L, 1L, 1L, 4L, 4L, 4L, 4L, 2L, 1L, 4L, 3L, 3L, 3L, 
    4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 1L, 4L, 4L, 3L, 4L, 1L, 
    4L, 1L, 4L, 3L, 2L, 4L, 1L, 3L, 3L, 4L, 1L, 3L, 1L, 3L, 4L, 
    4L, 4L, 1L, 4L, 4L, 3L, 3L, 1L, 1L, 2L, 4L, 4L, 1L, 1L, 4L, 
    3L, 4L, 4L, 2L, 2L, 4L, 1L, 1L, 2L, 2L, 1L, 1L, 4L, 2L, 4L, 
    3L, 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L, 89L, 54L, 76L, 105L, 172L, 165L, 
    25L, 95L, 173L, 19L, 163L, 92L, 163L, 166L, 33L, 194L, 12L, 
    180L, 196L, 36L, 86L, 64L, 14L, 182L, 190L, 59L, 93L, 107L, 
    113L, 25L, 61L, 116L, 188L, 95L, 95L, 77L, 37L, 54L, 58L, 
    158L, 150L, 133L, 190L, 96L, 183L, 134L, 164L, 100L, 185L, 
    11L, 156L, 104L, 13L, 5L, 8L, 24L, 119L, 86L, 158L, 182L, 
    13L, 79L, 66L, 178L, 42L, 136L, 19L, 56L, 195L, 56L, 73L, 
    149L, 28L, 64L, 186L, 98L, 19L, 173L, 126L, 151L, 58L, 193L, 
    176L, 8L, 160L, 190L, 153L, 86L, 60L, 138L, 82L, 127L, 36L, 
    109L, 156L, 110L, 142L, 61L, 95L, 25L, 158L, 30L, 119L, 90L, 
    7L, 73L, 129L, 17L, 194L, 18L, 130L, 117L, 82L, 179L, 73L, 
    153L, 152L, 95L, 111L, 76L, 112L, 42L, 181L, 124L, 14L, 38L, 
    11L, 147L, 80L, 105L, 197L, 19L, 180L, 76L, 173L, 57L, 163L, 
    161L, 107L, 79L, 55L, 100L, 192L, 196L, 13L, 151L, 165L, 
    63L, 72L, 107L, 108L, 52L, 82L, 137L, 141L, 181L, 114L, 53L, 
    76L, 9L, 93L, 73L, 99L, 19L, 6L, 108L, 197L, 148L, 94L, 34L, 
    135L, 156L, 139L, 136L, 93L, 36L, 195L, 6L, 74L, 61L, 166L, 
    39L, 93L, 55L, 48L, 148L, 127L, 164L, 109L, 18L, 157L, 112L, 
    90L, 71L, 98L, 175L, 149L, 55L, 143L, 139L, 157L, 151L, 25L, 
    92L, 46L, 47L, 56L, 97L, 171L, 24L, 91L, 134L, 166L, 149L, 
    34L, 100L, 176L, 57L, 103L, 56L, 150L, 11L, 175L, 108L, 29L, 
    28L, 95L, 121L, 133L, 185L, 191L, 17L, 173L, 11L, 71L, 124L, 
    125L, 171L, 5L, 19L, 171L, 45L, 30L, 133L, 20L, 50L, 80L, 
    5L, 175L, 146L, 144L, 42L, 87L, 18L, 94L, 9L, 54L, 15L, 85L, 
    115L, 172L, 51L, 191L, 83L, 115L, 43L, 200L, 84L, 126L, 125L, 
    107L, 81L, 131L, 170L, 130L, 138L, 191L, 190L, 5L, 59L, 122L, 
    178L, 59L, 49L, 36L, 86L, 19L, 187L, 38L, 46L, 19L, 182L, 
    40L, 27L, 85L, 91L, 63L, 155L, 122L, 184L, 97L, 156L, 33L, 
    96L, 56L, 29L, 142L, 145L, 11L, 151L, 136L, 152L, 168L, 166L, 
    56L, 92L, 163L, 40L, 91L, 51L, 10L, 15L, 79L, 54L, 81L, 119L, 
    48L, 117L, 117L, 46L, 86L, 148L, 172L, 71L, 144L)), .Names = c("Hour", 
"Company", "Production", "X", "Hour.1", "Company.1", "Production.1", 
"X.1", "Hour.2", "Company.2", "Production.2"), class = "data.frame", row.names = c(NA, 
-341L))

enter code here

1 个答案:

答案 0 :(得分:0)

此处的解决方案遵循您的逻辑(仅for (i in 1:3)),因为您的数据示例不再是列..

plot<-list()
for(i in 1:3){


  dataset$Hour <-dataset[,4*i-3]
  dataset$Company<-dataset[,4*i-2]
  dataset$Production<-dataset[,4*i-1]
  plot[[i]]<-ggplot()+
    geom_bar(data = dataset, aes(x=Hour, y=Production, fill=Company),stat="identity") +
    ggtitle("1/1/2016")

}



multiplot(plotlist=plot)

但是看一下你的代码,使用reshape更有意义,因此每个图表都有一个变量id或者你想要的,而不是列。另外,为什么Hour有3列,它们完全相同?