Home > Software design >  ggplot2 - unable to keep factor order of country names when using coord_flip
ggplot2 - unable to keep factor order of country names when using coord_flip

Time:07-29

I am attempting to order a geom_bar() graph as it stands in my dataframe but when it is plotted my ordering changes completely.

Here is my data:

    > fert
    A tibble: 28 × 4
   country      value fert_import_origin country_f   
   <chr>        <dbl> <chr>              <fct>       
 1 DRC           36.6 Russia             DRC         
 2 Morocco       36.4 Russia             Morocco     
 3 Cameroon      31.6 Russia             Cameroon    
 4 Ivory Coast   26.3 Russia             Ivory Coast 
 5 Senegal       25.8 Russia             Senegal     
 6 Ghana         21   Russia             Ghana       
 7 Rwanda        20.3 Russia             Rwanda      
 8 Nigeria       20   Russia             Nigeria     
 9 Burkina Faso  18.5 Russia             Burkina Faso
10 Mozambique    17.7 Russia             Mozambique  
11 Tanzania      16.9 Russia             Tanzania    
12 Mali          16.5 Russia             Mali        
13 Uganda        15.7 Russia             Uganda      
14 South Africa  14.7 Russia             South Africa
15 DRC           63.4 Rest of World      DRC         
16 Morocco       63.6 Rest of World      Morocco     
17 Cameroon      68.4 Rest of World      Cameroon    
18 Ivory Coast   73.7 Rest of World      Ivory Coast 
19 Senegal       74.2 Rest of World      Senegal     
20 Ghana         79   Rest of World      Ghana       
21 Rwanda        79.7 Rest of World      Rwanda      
22 Nigeria       80   Rest of World      Nigeria     
23 Burkina Faso  81.5 Rest of World      Burkina Faso
24 Mozambique    82.3 Rest of World      Mozambique  
25 Tanzania      83.1 Rest of World      Tanzania    
26 Mali          83.5 Rest of World      Mali        
27 Uganda        84.3 Rest of World      Uganda      
28 South Africa  85.3 Rest of World      South Africa

As suggested by other answers, I created a factor for the country column to keep my levels as I'd like them to be:

fert$country_f <- NA
fert$country_f <- factor(fert$country)

Here is my code for the final plot:

ggplot(fert, aes(fill = fert_import_origin, x = country_f, y = value, 
                 label = paste0(value, sep = "%")))  
  
  geom_bar(stat = "identity", width = 0.7)  
  
  geom_text(size = 4, position = position_stack(vjust = 0.5), family = "Lato")  
  
  scale_y_continuous(breaks = pretty_breaks(n = 6), labels = percent_format(scale = 1))  
  
  scale_fill_manual(values = c("#0C7BDC","#96426C"),
                    breaks = c("Russia", "Rest of World"))  
  
  labs(x = "", y = "", title = "Fertiliser import by origin in Africa (%) | 2021",
       caption = paste0("Source: Trade Map, 2021\n",
                        "Graphic: Monique Bennett"))  

  coord_flip()

Here's my plot with the incorrect ordering:

Image with the incorrect ordering here

CodePudding user response:

Perhaps instead of using a factor variable, you can use fct_reorder and a numerical variable. I haven't tested it yet since I couldn't reproduce your data easily.

fert$country_f <- 1:nrow(fert)

ggplot(fert, aes(fill = fert_import_origin, x = fct_reorder(country, country_f), y = value, 
                 label = paste0(value, sep = "%")))  
  
  geom_bar(stat = "identity", width = 0.7)  
  
  geom_text(size = 4, position = position_stack(vjust = 0.5), family = "Lato")  
  
  scale_y_continuous(breaks = pretty_breaks(n = 6), labels = percent_format(scale = 1))  
  
  scale_fill_manual(values = c("#0C7BDC","#96426C"),
                    breaks = c("Russia", "Rest of World"))  
  
  labs(x = "", y = "", title = "Fertiliser import by origin in Africa (%) | 2021",
       caption = paste0("Source: Trade Map, 2021\n",
                        "Graphic: Monique Bennett"))  
  
  coord_flip()

CodePudding user response:

Welcome to StackOverflow. It is difficult to evaluate your problem without a reproducible example. Neverhteless, I think your problem comes from the order of the levels of your factor type variable.

The order in your plot is the same you get running levels(country_f).

The solution for your problem is to "relevel" country_f acording to the order you want. This is done by:

fert$country_f <- factor(fert$country_f, levels = fert$country)

Then you can run what you did to make the graphical representation

CodePudding user response:

I think you just need to add the levels to the factors in your desired order:

fert <- tibble::tribble(
        ~country, ~value, ~fert_import_origin,     ~country_f,
           "DRC",   36.6,            "Russia",          "DRC",
       "Morocco",   36.4,            "Russia",      "Morocco",
      "Cameroon",   31.6,            "Russia",     "Cameroon",
   "Ivory Coast",   26.3,            "Russia",  "Ivory Coast",
       "Senegal",   25.8,            "Russia",      "Senegal",
         "Ghana",     21,            "Russia",        "Ghana",
        "Rwanda",   20.3,            "Russia",       "Rwanda",
       "Nigeria",     20,            "Russia",      "Nigeria",
  "Burkina Faso",   18.5,            "Russia", "Burkina Faso",
    "Mozambique",   17.7,            "Russia",   "Mozambique",
      "Tanzania",   16.9,            "Russia",     "Tanzania",
          "Mali",   16.5,            "Russia",         "Mali",
        "Uganda",   15.7,            "Russia",       "Uganda",
  "South Africa",   14.7,            "Russia", "South Africa",
           "DRC",   63.4,     "Rest of World",          "DRC",
       "Morocco",   63.6,     "Rest of World",      "Morocco",
      "Cameroon",   68.4,     "Rest of World",     "Cameroon",
   "Ivory Coast",   73.7,     "Rest of World",  "Ivory Coast",
       "Senegal",   74.2,     "Rest of World",      "Senegal",
         "Ghana",     79,     "Rest of World",        "Ghana",
        "Rwanda",   79.7,     "Rest of World",       "Rwanda",
       "Nigeria",     80,     "Rest of World",      "Nigeria",
  "Burkina Faso",   81.5,     "Rest of World", "Burkina Faso",
    "Mozambique",   82.3,     "Rest of World",   "Mozambique",
      "Tanzania",   83.1,     "Rest of World",     "Tanzania",
          "Mali",   83.5,     "Rest of World",         "Mali",
        "Uganda",   84.3,     "Rest of World",       "Uganda",
  "South Africa",   85.3,     "Rest of World", "South Africa"
  )


fert$country_f <- factor(fert$country, levels = rev(unique(fert$country)))

library(ggplot2)
library(scales)

ggplot(fert, aes(fill = fert_import_origin, x = country_f, y = value, 
                 label = paste0(value, sep = "%")))  
  
  geom_bar(stat = "identity", width = 0.7)  
  
  geom_text(size = 4, position = position_stack(vjust = 0.5), family = "Lato")  
  
  scale_y_continuous(breaks = pretty_breaks(n = 6), labels = percent_format(scale = 1))  
  
  scale_fill_manual(values = c("#0C7BDC","#96426C"),
                    breaks = c("Russia", "Rest of World"))  
  
  labs(x = "", y = "", title = "Fertiliser import by origin in Africa (%) | 2021",
       caption = paste0("Source: Trade Map, 2021\n",
                        "Graphic: Monique Bennett"))  
  
  coord_flip()

enter image description here

  • Related