Import your data

read_csv("canada_births_1991_2022.csv")
## Rows: 384 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): year, month, births
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 384 × 3
##     year month births
##    <dbl> <dbl>  <dbl>
##  1  1991     1  32213
##  2  1991     2  30345
##  3  1991     3  34869
##  4  1991     4  35398
##  5  1991     5  36371
##  6  1991     6  34378
##  7  1991     7  35436
##  8  1991     8  34421
##  9  1991     9  34410
## 10  1991    10  33092
## # ℹ 374 more rows

Chapter 15

Create a factor

x <- factor(c("single", "married", "married", "single"))
print(x)
## [1] single  married married single 
## Levels: married single

Modify factor order

Make two bar charts here - one before ordering another after

# Create data
data <- data.frame(
  name=c("A","B","C","D","E") ,  
  value=c(3,12,5,18,45)
  )

# Barplot
ggplot(data, aes(x=name, y=value)) + 
  geom_bar(stat = "identity")

Modify factor levels

Show examples of three functions:

  • fct_recode
  • fct_collapse
  • fct_lump

Chapter 16

No need to do anything here.