cis-ds / Discussion

Public discussion
10 stars 15 forks source link

question on factor #204

Closed BaichenTan closed 2 years ago

BaichenTan commented 2 years ago

In the notes, why it is often suggested to use mutate() to reorder the factors first instead of directly applying them in ggplot() by setting the aes(fct_infreq())? @bensoltoff e.g., why in the solution, the code

intent_levels <- c("Accidental", "Homicide", "Suicide", "Undetermined")

gun_deaths %>% drop_na(intent) %>% mutate(intent = parse_factor(intent, levels = intent_levels) %>% fct_infreq() %>% fct_rev()) %>% ggplot(mapping = aes(x = intent)) + geom_bar()

is better than

gun_deaths %>% ggplot(mapping = aes(intent %>% fct_infreq())) + geom_bar()

@bensoltoff

bensoltoff commented 2 years ago

It isn't inherently better. I did it that way to distinguish the data transformation operations from the data visualization functions. But it will work either way.

BaichenTan commented 2 years ago

Gotcha! thank you!