r – ordering of ggplot not working with factors

I have created a vector with the order of a dot plot mentioned but it doesn’t plot in that order/ Thanks for the suggestions.

order <- sav %>%
  filter(Subject == "Food") %>%
  arrange(desc(Percentage)) %>%
  select(Location) %>% 
  unlist() %>%
  unname()

order <- replace(order, c(1, 8), order[c(8, 1)])

sav %>%
  ggplot(aes(x = factor(Location, levels = order), y = Percentage,
             color = Subject))+
  geom_point(data = filter(sav, Location != "IRELAND"),
             size = 4, position = position_dodge(0.5))+
  geom_point(data = filter(sav, Location == "IRELAND"),
             size = 6, position = position_dodge(1))+
  geom_linerange(data = filter(sav, Location == "IRELAND"),
                 aes(ymin = 0, ymax = Percentage), 
                 position = position_dodge(1),
                linetype = "dotdash") + 
  geom_linerange(data = filter(sav, Location != "IRELAND"),
                aes(ymin = 0, ymax = Percentage),
                 position = position_dodge(0.5), linetype = "dotdash") + 
  coord_flip()+
  ggtitle(label = "Increase in inflation (by CPI) in Ireland compared to OECD and other countries in OECD")+
  xlab("Countries --> ") +
  ylab("Increase in CPI by % -->")+
  scale_y_continuous(breaks = round(seq(0, 20, by = 1),1))+
  scale_color_manual(name = "Type of Items", 
                    labels = c("Food", "Total", "Excluding food and energy"),
                    values=c(unname(colorblind_colors[2]), 
                              unname(colorblind_colors[3]), 
                              unname(colorblind_colors[4])))+
  theme(panel.grid.major.x = element_line(linewidth =.01, color="black"),
        panel.grid.major.y = element_blank(),
        legend.position = "top"
    )

Image output

> dput(order)
c("IRELAND", "NETHERLANDS", "SPAIN", "OECD", "ITALY", "FRANCE", 
"UNITED STATES", "GERMANY", "CANADA")

> dput(sav)
structure(list(Location = c("CANADA", "CANADA", "FRANCE", "FRANCE", 
"GERMANY", "GERMANY", "IRELAND", "IRELAND", "ITALY", "ITALY", 
"NETHERLANDS", "NETHERLANDS", "SPAIN", "SPAIN", "UNITED STATES", 
"UNITED STATES", "OECD", "OECD", "CANADA", "ITALY", "SPAIN", 
"FRANCE", "IRELAND", "UNITED STATES", "NETHERLANDS", "OECD", 
"GERMANY"), Subject = c("Food", "Total", "Food", "Total", "Food", 
"Total", "Food", "Total", "Food", "Total", "Food", "Total", "Food", 
"Total", "Food", "Total", "Food", "Total", "Total_Minus_Food_Energy", 
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy", "Total_Minus_Food_Energy", 
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy", "Total_Minus_Food_Energy", 
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy"), Frequency = c("Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly"), Time = c("2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12"
), Percentage = c(11.02015, 6.319445, 12.86678, 5.850718, 19.75631, 
8.550855, 11.74636, 8.224299, 13.14815, 11.63227, 16.7983, 9.586879, 
15.68565, 5.70769, 11.88275, 6.454401, 15.60381, 9.438622, 5.58275, 
4.469475, 4.442303, 3.36004, 4.999758, 5.707835, 6.034873, 7.221961, 
5.05511)), class = "data.frame", row.names = c(NA, -27L))

Read more here: Source link