ima
ima

Reputation: 155

R: Boxplot how to formulate data in effective way

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.

Upvotes: 1

Views: 68

Answers (1)

pogibas
pogibas

Reputation: 28369

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)

Upvotes: 1

Related Questions