1

The data I'm using is

> 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

I wrote a loop to plot multiple bowplot in one script:

df2 <- myfun2(Impact$X__3, EV)
Box.graph <- function(df2, na.rm = TRUE, ...){
  Caplist <- unique(df2$Capacity) 
y <- df2[df2$Capacity==Caplist[i],1:9]
  for (i in seq_along(Caplist)){
    boxplot <- 
      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)

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.

I tried to fix the issues by adding a line y <- df2[df2$Capacity==Caplist[i],1:9]and also added `y= df2[df2$Capacity==Caplist[i],1:9]' in the ggplot part as suggested here. 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.

The desired output is a several boxplots with the values of E1,E2,E3,E4,E5,E6,E7,E8 and E9. So for example the first boxplot should include the values: 30 NA 20 3 1 5 NA NA NA 20.

ima
  • 155
  • 12
  • There's no `value` in `df2`, that are you plotting? – pogibas Sep 19 '17 at 16:08
  • @PoGibas I new to R and the loop approach looked like a nice method to seperatly store the different experiments. How would you reformulate my question with facets? And what would be the advantage? (I'm very curious) – ima Sep 19 '17 at 16:09
  • @PiGibas I'm trying to plot the values of column E1,E2,E3,E4,E5,E6,E7,E8 and E9 in one boxplot for every row. So i.e. for Capacity C1 I would like to create a boxplot of the values (30 NA 20 3 1 5 NA NA NA 20) – ima Sep 19 '17 at 16:11

1 Answers1

1

Please try this simplified function (hard to test without having real data):

Box.graph <- function(df2, naRM = TRUE) {
    library(data.table)
    library(ggplot2)

    setDT(df2)
    foo <- melt(df2, c("LDI", "PDC", "Capacity"))[variable %in% paste0("E", 1:9)]
    if (naRM) {
        foo <- foo[!is.na(value)]
    }
    p <- ggplot(foo, aes(LDI, value, fill = LDI)) +
        geom_boxplot() +
        facet_wrap(Capacity ~ PDC)
    ggsave(plot = p, file = paste0(StoreResults, "/Results/YesDCNoV2G.png"), scale=2)
    return(NULL)
}
Box.graph(df2)
pogibas
  • 27,303
  • 19
  • 84
  • 117
  • After running the file I receive the following error: Warning message: In melt.data.table(df2, c("LDI", "PDC", "Capacity")) : ' 'measure.vars' [Results, Power, LDE, LB, ...] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'character'. All measure variables not of type 'character' will be coerced to. Check DETAILS in ?melt.data.table for more on coercion.' – ima Sep 20 '17 at 07:25
  • 1
    @Ima It's a warning message, everything is fine :-) – pogibas Sep 20 '17 at 07:51
  • @Ima but are the files saved? – pogibas Sep 20 '17 at 08:12
  • @PiBas They are indeed saved! Sorry about that. But as I mentioned before I have a large data set and the problem is that the boxplot it saved is not excately as I would like.... Is there a way to melt the data within the subset I defined? `subset(df2, df2$Capacity==Caplist[i]` because the plots are now not in the right order. – ima Sep 20 '17 at 08:33
  • @PiBas By editing your code I finally got the results I wanted: – ima Sep 20 '17 at 12:29