Reorder columns in ggplot with facet_wrap

Hi! How do I change the order of columns for line graphs created with facet wrap?

My data and code looks like this:

Scenario Iteration LB UB Eps
BP = 0, BI = 50 1 1 4.60880829 3.60880829
BP = 0, BI = 50 2 4.60880829 4.60880829 0
BP = 0, BI = 50 3 4.60880829 4.60880829 0
BP = 0, BI = 100 1 1 7.294041451 6.294041451
BP = 0, BI = 100 2 7.292746114 7.294041451 0.00017762
BP = 0, BI = 100 3 7.292746114 7.294041451 0.00017762
BP = 0, BI = 150 1 1 10.16450777 9.164507772
BP = 0, BI = 150 2 10.1619171 10.16450777 0.000254939
BP = 0, BI = 150 3 10.1619171 10.16450777 0.000254939
BP = 0, BI = 200 1 1 12.89248705 11.89248705
BP = 0, BI = 200 2 12.89248705 12.89248705 0
BP = 0, BI = 200 3 12.89248705 12.89248705 0

plot6 <- ggplot(data = ConvergenceByBudget, aes(x = Iteration)) + geom_line(aes(y=LB), color = "cornflowerblue", lwd = 0.9) +
geom_line(aes(y=UB), color = "chocolate1", linetype = "twodash", lwd = 0.9) +
facet_wrap(~Scenario, ncol = 4)
print(plot6)

In the plot I made, I'd like the column on the far right to be placed all the way to the left instead, and the other columns stay in the same order.

Thanks!

Convert the column Scenario into a factor with the appropriate order.

I think you have two options - turn them alphabetical, or make a factor.

library(tidyverse)

dat <- tribble(
  
  ~Scenario,    ~Iteration, ~LB,    ~UB,    ~Eps,
  "BP = 0, BI = 50",  1,    1,  4.60880829  ,3.60880829,
  "BP = 0, BI = 50",    2,  4.60880829    ,4.60880829,  0,
  "BP = 0, BI = 50",  3,    4.60880829    ,4.60880829,  0,
  "BP = 0, BI = 100", 1,    1,  7.294041451 ,6.294041451,
  "BP = 0, BI = 100", 2,    7.292746114   ,7.294041451, 0.00017762,
  "BP = 0, BI = 100", 3,    7.292746114   ,7.294041451, 0.00017762,
  "BP = 0, BI = 150", 1,    1,  10.16450777 ,9.164507772,
  "BP = 0, BI = 150", 2,    10.1619171    ,10.16450777, 0.000254939,
  "BP = 0, BI = 150", 3,    10.1619171    ,10.16450777, 0.000254939,
  "BP = 0, BI = 200", 1,    1,  12.89248705 ,11.89248705,
  "BP = 0, BI = 200", 2,    12.89248705   ,12.89248705, 0,
  "BP = 0, BI = 200", 3,    12.89248705   ,12.89248705, 0
  
)

dat %>%
  mutate(Scenario = str_replace_all(Scenario, "BI = 50", "BI = 050")) %>%
  ggplot(aes(x = Iteration)) + 
  geom_line(aes(y = LB), color = "cornflowerblue", lwd = 0.9) +
  geom_line(aes(y = UB),
            color = "chocolate1",
            linetype = "twodash",
            lwd = 0.9) +
  facet_wrap( ~ Scenario, ncol = 4)

dat %>% 
  mutate(Scenario = fct_inorder(Scenario)) %>%
  ggplot(aes(x = Iteration)) + 
  geom_line(aes(y = LB), color = "cornflowerblue", lwd = 0.9) +
  geom_line(aes(y = UB),
            color = "chocolate1",
            linetype = "twodash",
            lwd = 0.9) +
  facet_wrap( ~ Scenario, ncol = 4)

Created on 2022-06-21 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.