artists <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-09-27/artists.csv')
## Rows: 3380 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): state, race, type
## dbl (4): all_workers_n, artists_n, artists_share, location_quotient
##
## ℹ 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.
race <- c("Hispanic", "White", "African-American", "Asian", "Other")
race
## [1] "Hispanic" "White" "African-American" "Asian"
## [5] "Other"
race_factor <- factor(race, levels = race)
race_factor
## [1] Hispanic White African-American Asian
## [5] Other
## Levels: Hispanic White African-American Asian Other
Make two bar charts here - one before ordering another after
Unordered factor levels
# Transform data: group by race, average artists by race
avg_artists <- artists %>%
group_by(race) %>%
summarise(avg_artists = mean(artists_n, na.rm = TRUE))
# Plot
avg_artists %>%
ggplot(aes(x = avg_artists, y = race)) +
geom_point()
Ordered factor levels
avg_artists %>%
ggplot(aes(x = avg_artists, y = fct_reorder(.f = race, .x = avg_artists))) +
geom_point() +
labs(y = NULL, x = "Average Artists by Race")
Show examples of three functions:
artists %>% distinct(race)
## # A tibble: 5 × 1
## race
## <chr>
## 1 Hispanic
## 2 White
## 3 African-American
## 4 Asian
## 5 Other
# Recode function
artists %>%
#Rename levels
mutate(race_rev = fct_recode(race, "Caucasian" = "White")) %>%
select(race, race_rev) %>%
filter(race == "White")
## # A tibble: 676 × 2
## race race_rev
## <chr> <fct>
## 1 White Caucasian
## 2 White Caucasian
## 3 White Caucasian
## 4 White Caucasian
## 5 White Caucasian
## 6 White Caucasian
## 7 White Caucasian
## 8 White Caucasian
## 9 White Caucasian
## 10 White Caucasian
## # ℹ 666 more rows
# Collapse multiple levels into one
artists %>%
mutate(race_col = fct_collapse(race, "Minority" = c("African-American", "Other"))) %>%
select(race, race_col) %>%
filter(race != "White") %>%
filter(race != "Hispanic") %>%
sample_n(10)
## # A tibble: 10 × 2
## race race_col
## <chr> <fct>
## 1 Other Minority
## 2 Asian Asian
## 3 African-American Minority
## 4 Other Minority
## 5 Other Minority
## 6 African-American Minority
## 7 African-American Minority
## 8 African-American Minority
## 9 Asian Asian
## 10 Asian Asian
# Lump small levels into other levels
artists %>% count(race)
## # A tibble: 5 × 2
## race n
## <chr> <int>
## 1 African-American 676
## 2 Asian 676
## 3 Hispanic 676
## 4 Other 676
## 5 White 676
artists %>%
mutate(race_lump = fct_lump(race, n = 3)) %>%
count(race_lump, sort = TRUE)
## # A tibble: 5 × 2
## race_lump n
## <fct> <int>
## 1 African-American 676
## 2 Asian 676
## 3 Hispanic 676
## 4 Other 676
## 5 White 676
# Because there are no minimum, maximum or unique values for each of the race levels, the race_lump function does not lump.
No need to do anything here.