title: R: Boxplot how to formulate data in effective way
body: <p>The data I'm using is </p>
  
  <pre><code>&gt; head(df2)
         Results Capacity Power  LDI  LDE      LB  PDC   D E1 E2 E3 E4 E5 E6 E7 E8 E9
  1 DCNoV2GYesDC       C1  PG11 LDI0 LDE0 LB0.045 PDC0 D10 30 NA 20 3 1 5 NA NA NA 20
  2 DCNoV2GYesDC    C0.95  PG11 LDI0 LDE2 LB0.045 PDC0 D10 8  3  NA  8  9 NA NA NA NA
  3 DCNoV2GYesDC     C0.9  PG11 LDI0 LDE2 LB0.045 PDC0 D10 8  NA  5  NA  6 7 NA NA NA
  4 DCNoV2GYesDC    C0.85  PG11 LDI0 LDE2 LB0.045 PDC0 D10 NA NA NA NA NA NA NA NA NA
  5 DCNoV2GYesDC     C0.8  PG11 LDI0 LDE3 LB0.045 PDC0 D10 NA NA NA NA NA NA NA NA NA
  6 DCNoV2GYesDC    C0.75  PG11 LDI0 LDE3 LB0.045 PDC0 D10 NA NA  1  1 NA  1 NA 50 70
  </code></pre>
  
  <p>I wrote a loop to plot multiple bowplot in one script: </p>
  
  <pre><code>df2 &lt;- myfun2(Impact$X__3, EV)
  Box.graph &lt;- function(df2, na.rm = TRUE, ...){
    Caplist &lt;- unique(df2$Capacity) 
  y &lt;- df2[df2$Capacity==Caplist[i],1:9]
    for (i in seq_along(Caplist)){
      boxplot &lt;- 
        ggplot(subset(df2, df2$Capacity==Caplist[i]),
               aes(LDI, y=value , colour = LDI), group = df2$Capacity) +
        geom_boxplot() +
        theme(axis.text.x = element_text(size=14))+
        facet_wrap( ~ PDC, ncol =1)+ 
        theme(legend.position = "top")+
        scale_y_continuous("time")+
        scale_x_continuous("LDI")+
        ggtitle(paste(Caplist[i], ' LDE \n', 
                      "time \n",
                      sep=''))
      #save plot as PNG 
      ggsave(plot = last_plot(), file= paste(StoreResults, '/Results/',
                                             Caplist[i], "YesDCNoV2G.png", sep=''), scale=2)
      print(boxplot)
    }
  }
  #Run the function  
  Box.graph(df2)
  </code></pre>
  
  <p>The problem I have is this code does not give an error, nor does it run. I think that the problem is that the y=value part is incorrect or not properly defined. </p>
  
  <p>I tried to fix the issues by adding a line   <code>y &lt;- df2[df2$Capacity==Caplist[i],1:9]</code>and also added   `y= df2[df2$Capacity==Caplist[i],1:9]' in the ggplot part as suggested <a href="https://stackoverflow.com/questions/46114761/boxplot-over-multiple-columns-within-loop?noredirect=1#comment79199160_46114761">here</a>. Still no result. Someone also pointed out that I should melt the data: but I don't know what the most effective way of doing that is with my data. </p>
  
  <p>The desired output is a several boxplots with the values of <code>E1,E2,E3,E4,E5,E6,E7,E8 and E9</code>. So for example the first boxplot should include the values: <code>30 NA 20 3 1 5 NA NA NA 20</code>. </p>