Import your data

palm <- read_excel("../00_data/palmtrees.xlsx")
## Warning: Coercing text to numeric in V1449 / R1449C22: '0.56675675700000006'
palm
## # A tibble: 2,557 × 29
##    spec_name            acc_genus acc_species palm_tribe palm_subfamily climbing
##    <chr>                <chr>     <chr>       <chr>      <chr>          <chr>   
##  1 Acanthophoenix crin… Acanthop… crinita     Areceae    Arecoideae     climbing
##  2 Acanthophoenix rous… Acanthop… rousselii   Areceae    Arecoideae     climbing
##  3 Acanthophoenix rubra Acanthop… rubra       Areceae    Arecoideae     climbing
##  4 Acoelorrhaphe wrigh… Acoelorr… wrightii    Trachycar… Coryphoideae   climbing
##  5 Acrocomia aculeata   Acrocomia aculeata    Cocoseae   Arecoideae     climbing
##  6 Acrocomia crispa     Acrocomia crispa      Cocoseae   Arecoideae     climbing
##  7 Acrocomia emensis    Acrocomia emensis     Cocoseae   Arecoideae     climbing
##  8 Acrocomia glaucesce… Acrocomia glaucescens Cocoseae   Arecoideae     climbing
##  9 Acrocomia hassleri   Acrocomia hassleri    Cocoseae   Arecoideae     climbing
## 10 Acrocomia intumesce… Acrocomia intumescens Cocoseae   Arecoideae     climbing
## # ℹ 2,547 more rows
## # ℹ 23 more variables: acaulescent <chr>, erect <chr>, stem_solitary <chr>,
## #   stem_armed <chr>, leaves_armed <chr>, max_stem_height_m <dbl>,
## #   max_stem_dia_cm <dbl>, understorey_canopy <chr>, max_leaf_number <dbl>,
## #   max__blade__length_m <dbl>, max__rachis__length_m <dbl>,
## #   max__petiole_length_m <dbl>, average_fruit_length_cm <dbl>,
## #   min_fruit_length_cm <dbl>, max_fruit_length_cm <dbl>, …

Chapter 15

Create a factor

palm %>% count(fruit_shape)
## # A tibble: 8 × 2
##   fruit_shape     n
##   <chr>       <int>
## 1 ellipsoid      53
## 2 elongate      110
## 3 fusiform        3
## 4 globose       800
## 5 ovoid         813
## 6 pyramidal      12
## 7 rounded         1
## 8 <NA>          765
fruit_levels <- c("globose", "rounded", "ellipsoid", "ovoid", "fusiform", "elongate", "pyramidal")

palm_rev <- palm %>%
    mutate(rank = fruit_shape %>% factor(levels = fruit_levels))

# Fruit organized by roundness
fruit_levels
## [1] "globose"   "rounded"   "ellipsoid" "ovoid"     "fusiform"  "elongate" 
## [7] "pyramidal"

Modify factor order

Make two bar charts here - one before ordering another after

palm_summary <- palm %>%
  group_by(fruit_shape) %>%
  summarise(
    max_fruit_width_cm = mean(max_fruit_width_cm, na.rm = TRUE)
  ) %>%
    drop_na(fruit_shape, max_fruit_width_cm)

palm_summary
## # A tibble: 6 × 2
##   fruit_shape max_fruit_width_cm
##   <chr>                    <dbl>
## 1 ellipsoid                 1.58
## 2 elongate                  2.36
## 3 fusiform                  0.7 
## 4 globose                   2.36
## 5 ovoid                     2.02
## 6 pyramidal                 1.34
ggplot(palm_summary, aes(max_fruit_width_cm, fruit_shape)) + geom_point()

ggplot(palm_summary, aes(max_fruit_width_cm, fct_reorder(fruit_shape, max_fruit_width_cm))) + geom_point()

Modify factor levels

Show examples of three functions:

  • fct_recode
palm %>%
    mutate(fruit_shape = fct_recode(fruit_shape,
                                    "elongated"     = "ellipsoid",
                                    "longnarrow"    = "elongate",
                                    "round"         = "rounded",
                                    "spindleshaped" = "fusiform",
                                    "spherical"     = "globose",
                                    "eggshaped"     = "ovoid",
                                    "pyramid"       = "pyramidal",)) %>%
    count(fruit_shape)
## # A tibble: 8 × 2
##   fruit_shape       n
##   <fct>         <int>
## 1 elongated        53
## 2 longnarrow      110
## 3 spindleshaped     3
## 4 spherical       800
## 5 eggshaped       813
## 6 pyramid          12
## 7 round             1
## 8 <NA>            765
  • fct_collapse
palm %>%
    mutate(fruit_shape = fct_collapse(fruit_shape,
                                      Regular     =  c("globose", "rounded", "ellipsoid", "ovoid"),
                                      Irregular  =  c("fusiform", "elongate", "pyramidal"))) %>%
    count(fruit_shape)
## # A tibble: 3 × 2
##   fruit_shape     n
##   <fct>       <int>
## 1 Regular      1667
## 2 Irregular     125
## 3 <NA>          765
  • fct_lump
palm %>%
    mutate(fruit_shape = fct_lump(fruit_shape)) %>%
    count(fruit_shape)
## # A tibble: 4 × 2
##   fruit_shape     n
##   <fct>       <int>
## 1 globose       800
## 2 ovoid         813
## 3 Other         179
## 4 <NA>          765

Chapter 16

No need to do anything here.