Import your data

data <- readxl::read_excel("myData.xlsx")

Chapter 15

Create a factor

data %>% count(number)
## # A tibble: 27 × 2
##    number     n
##     <dbl> <int>
##  1      1     1
##  2      2     1
##  3      3     1
##  4      4     1
##  5      5     1
##  6      6     1
##  7      7     1
##  8      8     1
##  9      9     1
## 10     10     1
## # ℹ 17 more rows
number_levels <- c("10", "9", "8")

data_rev <- data %>%
    mutate(number=number %>% factor(levels =number_levels))

Modify factor order

Make two bar charts here - one before ordering another after

data_summary <- data %>%
    group_by(film) %>%
    summarise(
        number=mean(number,na.rm=TRUE),
)

data_summary 
## # A tibble: 27 × 2
##    film          number
##    <chr>          <dbl>
##  1 A Bug's Life       2
##  2 Brave             13
##  3 Cars               7
##  4 Cars 2            12
##  5 Cars 3            18
##  6 Coco              19
##  7 Finding Dory      17
##  8 Finding Nemo       5
##  9 Incredibles 2     20
## 10 Inside Out        15
## # ℹ 17 more rows
ggplot(data_summary, aes(number, film)) + geom_point()

ggplot(data_summary, aes(number, fct_reorder(film, number))) + geom_point()

Modify factor levels

Show examples of three functions:

  • fct_recode
data %>%
    mutate(film = fct_recode(film,
                             "A Bug's Life" = "A Bug's Life",
                             "Brave" = "Brave",
                             "Cars" = "Cars",
                             "Cars 2" = "Cars 2",
                             "Cars 3" = "Cars 3",
                             "Coco" = "Coco",
                             "Finding Dory" = "Finding Dory",
                             "Finding Nemo" = "Finding Nemo",
                             "Incredibles 2" = "Incredibles 2",
                             "Inside Out" = "Inside Out")) %>%
    count(film)
## # A tibble: 27 × 2
##    film              n
##    <fct>         <int>
##  1 A Bug's Life      1
##  2 Brave             1
##  3 Cars              1
##  4 Cars 2            1
##  5 Cars 3            1
##  6 Coco              1
##  7 Finding Dory      1
##  8 Finding Nemo      1
##  9 Incredibles 2     1
## 10 Inside Out        1
## # ℹ 17 more rows
  • fct_collapse
data %>%
    mutate(film= fct_collapse(film,
                             "Cars Movies" = c("Cars", "Cars 2", "Cars 3"),
    "Finding Nemo Movies" = c("Finding Nemo", "Finding Dory"),
    "Toy Story Movies" = c("Toy Story", "Toy Story 2", "Toy Story 3", "Toy Story 4")
  )) %>%
  count(film)
## # A tibble: 21 × 2
##    film                    n
##    <fct>               <int>
##  1 A Bug's Life            1
##  2 Brave                   1
##  3 Cars Movies             3
##  4 Coco                    1
##  5 Finding Nemo Movies     2
##  6 Incredibles 2           1
##  7 Inside Out              1
##  8 Lightyear               1
##  9 Luca                    1
## 10 Monsters University     1
## # ℹ 11 more rows
  • fct_lump
data %>%
    mutate(film = fct_lump(film)) %>%
    count()
## # A tibble: 1 × 1
##       n
##   <int>
## 1    27

Chapter 16

No need to do anything here.