0

How can I modify this code to give me the plots based on the order of the variables. I mean, The first plot for AverageTime, the second one for A and third one for AverageCost.

Type<-c("a","b","c","d","e","f","g","h","i","j","k","OG","HH")
    AverageTime<-c(66,367,55,700,200,45,44,55,55,6,66,66,400)
    A<-c(11,3,13,70,55,66,44,8,55,6,66,9,400)
    AverageCost<-c(300,10000,200,20000,500000,NA,700,NA,400000,NA,120000,1200,450)
    df<-data.frame(Type,AverageTime,A,AverageCost)

    df %>%
      mutate(AverageCost=as.numeric(AverageCost),A=as.numeric(A), AverageTime=as.numeric(AverageTime)) %>%
      gather(variable, value, -Type) %>%
      ggplot(aes(x=Type, y=value, colour=variable, group=variable)) +
      geom_line() +
      facet_wrap(~variable, scales="free_y")
shoorideh
  • 173
  • 5
  • 16
  • You can reorder the factor levels. However, with your code I do indeed get those three plots in that order. – David Robinson Jan 27 '16 at 19:45
  • @David Robinson, When I run the code plot of A comes first, then Average cost and finally Average time. – shoorideh Jan 27 '16 at 19:57
  • See duplicate. In your case, you could add the step `mutate(variable = factor(variable, levels = c("AverageTime", "A", "AverageCost")))` right after the `gather` step – David Robinson Jan 27 '16 at 19:59
  • @David Robinson, this is a good suggestion in the case when the number of levels (variables) are small. – shoorideh Jan 27 '16 at 20:10
  • @shoorideh It generalizes as well as you can generalize. The default order for factors is alphabetical, but if you can write a function that outputs the levels in the order you want, you can put them in that order. You can also look at built-in utility functions like `?reorder` for ordering levels by another variable and `?relevel` for moving any one level to be the first level. – Gregor Thomas Jan 27 '16 at 21:15

0 Answers0