penguins <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-04-15/penguins.csv')
## Rows: 344 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): species, island, sex
## dbl (5): bill_len, bill_dep, flipper_len, body_mass, year
##
## ℹ 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.
penguins %>% count(year)
## # A tibble: 3 × 2
## year n
## <dbl> <int>
## 1 2007 110
## 2 2008 114
## 3 2009 120
year_order <- c("2009", "2008", "2007")
data_rev <- penguins %>%
mutate(year = year %>% factor(levels = year_order))
Make two bar charts here - one before ordering another after
data_summary <- penguins %>%
group_by(sex) %>%
summarise(
body_mass = mean(body_mass, na.rm = TRUE)
)
data_summary
## # A tibble: 3 × 2
## sex body_mass
## <chr> <dbl>
## 1 female 3862.
## 2 male 4546.
## 3 <NA> 4006.
ggplot(data_summary, aes(body_mass, sex)) + geom_point()
ggplot(data_summary, aes(body_mass, fct_reorder(sex, body_mass))) + geom_point()
Show examples of three functions:
penguins %>%
mutate(sex = fct_recode(sex,
"female" = "ladies",
"male" = "men")) %>%
count(sex)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `sex = fct_recode(sex, female = "ladies", male = "men")`.
## Caused by warning:
## ! Unknown levels in `f`: ladies, men
## # A tibble: 3 × 2
## sex n
## <fct> <int>
## 1 female 165
## 2 male 168
## 3 <NA> 11
penguins %>%
mutate(island = fct_collapse(island,
main_islan = "Biscoe",
other = "Dream", "Torgersen
",
)) %>%
count(island)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `island = fct_collapse(...)`.
## Caused by warning:
## ! Unknown levels in `f`: Torgersen
## # A tibble: 3 × 2
## island n
## <fct> <int>
## 1 main_islan 168
## 2 other 124
## 3 Torgersen 52
penguins %>%
mutate(island = fct_lump(island)) %>%
count(island)
## # A tibble: 3 × 2
## island n
## <fct> <int>
## 1 Biscoe 168
## 2 Dream 124
## 3 Other 52
No need to do anything here.