Import your data

# excel file
refugee <- read_excel("../00_data/mydata.xlsx") %>% janitor::clean_names()

Chapter 15

Create a factor

Modify factor order

refugees_by_country <- refugee %>%
    
    group_by(country_of_origin_name) %>%
    summarise(
        Num_of_refugees = mean(the_number_of_refugees, na.rm = TRUE)
    )

# Plot
refugees_by_country %>%
    
    ggplot(aes(x = Num_of_refugees, y = country_of_origin_name)) +
    geom_point()

refugees_by_country
## # A tibble: 210 × 2
##    country_of_origin_name Num_of_refugees
##    <chr>                            <dbl>
##  1 Afghanistan                   32607.  
##  2 Albania                         507.  
##  3 Algeria                         104.  
##  4 Andorra                           2.77
##  5 Angola                          637.  
##  6 Anguilla                          1.25
##  7 Antigua and Barbuda              26.9 
##  8 Argentina                        19.7 
##  9 Armenia                         346.  
## 10 Aruba                             0   
## # ℹ 200 more rows
refugees_by_country %>%
    
    ggplot(aes(x = Num_of_refugees, y = fct_reorder(.f = country_of_origin_name, .x = Num_of_refugees))) +
    geom_point() +
    
    # Labeling
    labs(y = NULL, x = "Mean number of refugees")

### Modify factor levels Show examples of three functions:

#fct_recode 
refugee %>%
    
    mutate(country_of_origin_name_rev = fct_recode(country_of_origin_name, "French" = "FRENCHIE"))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `country_of_origin_name_rev = fct_recode(country_of_origin_name,
##   French = "FRENCHIE")`.
## Caused by warning:
## ! Unknown levels in `f`: FRENCHIE
## # A tibble: 64,809 × 17
##    the_year_of_data_collection country_of_origin_name     country_of_origin_un…¹
##                          <dbl> <chr>                      <chr>                 
##  1                        2010 Afghanistan                AFG                   
##  2                        2010 Iran (Islamic Rep. of)     IRN                   
##  3                        2010 Iraq                       IRQ                   
##  4                        2010 Pakistan                   PAK                   
##  5                        2010 Egypt                      ARE                   
##  6                        2010 China                      CHI                   
##  7                        2010 Palestinian                GAZ                   
##  8                        2010 Iraq                       IRQ                   
##  9                        2010 Serbia and Kosovo: S/RES/… SRB                   
## 10                        2010 Türkiye                    TUR                   
## # ℹ 64,799 more rows
## # ℹ abbreviated name: ¹​country_of_origin_unhcr_code
## # ℹ 14 more variables: country_of_origin_iso_code <chr>,
## #   country_of_asylum_name <chr>, country_of_asylum_unhcr_code <chr>,
## #   country_of_asylum_iso_code <chr>, the_number_of_refugees <dbl>,
## #   the_number_of_asylum_seekers <dbl>, the_number_of_returned_refugees <dbl>,
## #   the_number_of_internally_displaced_persons <dbl>, …
#FCT COLLAPSE
refugees_by_country %>%
    
    ggplot(aes(x = Num_of_refugees, y = fct_collapse(.f = country_of_origin_name, .x = Num_of_refugees))) +
    geom_point() +
    
    # Labeling
    labs(y = NULL, x = "Mean number of refugees")
## Warning: Unknown levels in `f`: 32606.7556325823, 507.175141242938,
## 104.142156862745, 2.76923076923077, 636.650420168067, 1.25, 26.8571428571429,
## 19.6646341463415, 345.7375, 0, 6.27450980392157, 5.11428571428571,
## 657.360515021459, 108.953488372093, 31.0377358490566, 283.055788005579,
## 58.03125, 150.826697892272, 10.9117647058824, 14.3541666666667,
## 21.8056426332288, 0, 1731.67816091954, 29.4354838709677, 876.25181598063,
## 27.6022727272727, 67.0493827160494, 0, 80.5049504950495, 284.271276595745,
## 4162.2625, 2.98591549295775, 692.020408163265, 580.546113989637,
## 13.1666666666667, 5.7, 9667.50483870968, 591.373897707231, 44.4605263157895,
## 3738.12241887906, 9.38775510204082, 1.78260869565217, 6619.29268292683,
## 62.6298701298701, 194.427952329361, 35.0740740740741, 816.727085478888,
## 1968.97916666667, 155.262917933131, 29.1666666666667, 1.9375, 105.464,
## 69.1864406779661, 6617.40740740741, 2.06451612903226, 95.7258883248731,
## 8.90476190476191, 25.3364055299539, 55.1826923076923, 310.054637865311,
## 856.447028423773, 16.1224489795918, 4906.64430147059, 43.5714285714286,
## 21.0842105263158, 1192.41463414634, 152.194805194805, 2.59090909090909,
## 9.85869565217391, 0.733333333333333, 22.6555555555556, 260.638766519824,
## 210.724609375, 14.9417475728155, 404.866666666667, 13.952380952381,
## 57.2325581395349, 2.125, 727.694029850746, 363.385234899329, 62.9424242424242,
## 66.8602150537634, 1270.009375, 0, 756.871473354232, 192.354651162791,
## 2.14285714285714, 261.8064, 776.030172413793, 1233.74606116775,
## 5656.6362217018, 1.61111111111111, 48.3411214953271, 7.95495495495495,
## 151.895705521472, 20.1470588235294, 58.7544554455446, 92.5789473684211,
## 207.832335329341, 9.38461538461539, 56.5915492957746, 91.8094059405941,
## 725.717557251908, 30.2540983606557, 159.722675367047, 1.85714285714286,
## 310.794573643411, 203.554858934169, 2.5, 21.2016806722689, 3.75,
## 31.6356589147287, 44.2931034482759, 56.6219512195122, 9.72463768115942,
## 2674.99112426035, 2.21052631578947, 5.5, 0, 1310.66666666667, 15.1881188118812,
## 571.365671641791, 0, 4.5, 107.285714285714, 113.287581699346, 84.8144712430427,
## 6.04444444444444, 18558.5405904059, 119.695238095238, 3, 204.171370967742,
## 11.2903225806452, 10.6774193548387, 200.018587360595, 211.301939058172,
## 2410.42939481268, 15.25, 140.912891986063, 2.4390243902439, 9.42622950819672,
## 1590.9604519774, 1.66666666666667, 1359.35889243876, 12.4761904761905,
## 157.137931034483, 11.1964285714286, 147.6401179941, 23.4803370786517,
## 107.434210526316, 4.22352941176471, 14.5581395348837, 34.5645161290323,
## 137.94670846395, 107.519379844961, 1234.21863354037, 3045.36276849642, 13.1875,
## 202.422222222222, 294.18, 0.722222222222222, 10.625, 49.8557692307692,
## 505.445134575569, 1616.15917602996, 7.79411764705882, 112.059490084986,
## 9.90769230769231, 92.4, 7.15, 16.7906976744186, 9341.79033457249,
## 21.2730375426621, 38800.9930875576, 5.30894308943089, 1933.25951557093,
## 494.304186046512, 7046.05700325733, 3.90625, 4.09433962264151,
## 5.07692307692308, 42308.1856508876, 46.3641160949868, 16.6797752808989,
## 3939.32608695652, 3.92307692307692, 241.5, 12.695652173913, 49.5487804878049,
## 52.1663113006397, 30.4535714285714, 8.33333333333333, 0, 1334.39772727273,
## 129.178260869565, 10167.9779411765, 28.0990099009901, 14.3, 28.0383631713555,
## 104.032142857143, 3118.98357963875, 12.1363636363636, 124.280977312391,
## 0.277777777777778, 1586.4645030426, 8016.79754601227, 9557.12179487179,
## 287.656542056075, 19.7167630057803, 465.351694915254

## Warning: Unknown levels in `f`: 32606.7556325823, 507.175141242938,
## 104.142156862745, 2.76923076923077, 636.650420168067, 1.25, 26.8571428571429,
## 19.6646341463415, 345.7375, 0, 6.27450980392157, 5.11428571428571,
## 657.360515021459, 108.953488372093, 31.0377358490566, 283.055788005579,
## 58.03125, 150.826697892272, 10.9117647058824, 14.3541666666667,
## 21.8056426332288, 0, 1731.67816091954, 29.4354838709677, 876.25181598063,
## 27.6022727272727, 67.0493827160494, 0, 80.5049504950495, 284.271276595745,
## 4162.2625, 2.98591549295775, 692.020408163265, 580.546113989637,
## 13.1666666666667, 5.7, 9667.50483870968, 591.373897707231, 44.4605263157895,
## 3738.12241887906, 9.38775510204082, 1.78260869565217, 6619.29268292683,
## 62.6298701298701, 194.427952329361, 35.0740740740741, 816.727085478888,
## 1968.97916666667, 155.262917933131, 29.1666666666667, 1.9375, 105.464,
## 69.1864406779661, 6617.40740740741, 2.06451612903226, 95.7258883248731,
## 8.90476190476191, 25.3364055299539, 55.1826923076923, 310.054637865311,
## 856.447028423773, 16.1224489795918, 4906.64430147059, 43.5714285714286,
## 21.0842105263158, 1192.41463414634, 152.194805194805, 2.59090909090909,
## 9.85869565217391, 0.733333333333333, 22.6555555555556, 260.638766519824,
## 210.724609375, 14.9417475728155, 404.866666666667, 13.952380952381,
## 57.2325581395349, 2.125, 727.694029850746, 363.385234899329, 62.9424242424242,
## 66.8602150537634, 1270.009375, 0, 756.871473354232, 192.354651162791,
## 2.14285714285714, 261.8064, 776.030172413793, 1233.74606116775,
## 5656.6362217018, 1.61111111111111, 48.3411214953271, 7.95495495495495,
## 151.895705521472, 20.1470588235294, 58.7544554455446, 92.5789473684211,
## 207.832335329341, 9.38461538461539, 56.5915492957746, 91.8094059405941,
## 725.717557251908, 30.2540983606557, 159.722675367047, 1.85714285714286,
## 310.794573643411, 203.554858934169, 2.5, 21.2016806722689, 3.75,
## 31.6356589147287, 44.2931034482759, 56.6219512195122, 9.72463768115942,
## 2674.99112426035, 2.21052631578947, 5.5, 0, 1310.66666666667, 15.1881188118812,
## 571.365671641791, 0, 4.5, 107.285714285714, 113.287581699346, 84.8144712430427,
## 6.04444444444444, 18558.5405904059, 119.695238095238, 3, 204.171370967742,
## 11.2903225806452, 10.6774193548387, 200.018587360595, 211.301939058172,
## 2410.42939481268, 15.25, 140.912891986063, 2.4390243902439, 9.42622950819672,
## 1590.9604519774, 1.66666666666667, 1359.35889243876, 12.4761904761905,
## 157.137931034483, 11.1964285714286, 147.6401179941, 23.4803370786517,
## 107.434210526316, 4.22352941176471, 14.5581395348837, 34.5645161290323,
## 137.94670846395, 107.519379844961, 1234.21863354037, 3045.36276849642, 13.1875,
## 202.422222222222, 294.18, 0.722222222222222, 10.625, 49.8557692307692,
## 505.445134575569, 1616.15917602996, 7.79411764705882, 112.059490084986,
## 9.90769230769231, 92.4, 7.15, 16.7906976744186, 9341.79033457249,
## 21.2730375426621, 38800.9930875576, 5.30894308943089, 1933.25951557093,
## 494.304186046512, 7046.05700325733, 3.90625, 4.09433962264151,
## 5.07692307692308, 42308.1856508876, 46.3641160949868, 16.6797752808989,
## 3939.32608695652, 3.92307692307692, 241.5, 12.695652173913, 49.5487804878049,
## 52.1663113006397, 30.4535714285714, 8.33333333333333, 0, 1334.39772727273,
## 129.178260869565, 10167.9779411765, 28.0990099009901, 14.3, 28.0383631713555,
## 104.032142857143, 3118.98357963875, 12.1363636363636, 124.280977312391,
## 0.277777777777778, 1586.4645030426, 8016.79754601227, 9557.12179487179,
## 287.656542056075, 19.7167630057803, 465.351694915254

#fct_lump
refugees_by_country %>%
    
    ggplot(aes(x = Num_of_refugees, y = fct_lump(country_of_origin_name))) +
    geom_point() +
    
    # Labeling
    labs(y = NULL, x = "Mean number of refugees")

Chapter 16

No need to do anything here.