Help with replicating forcats example from r4ds book

Not getting correct results for this example from r4ds book section on forcats. Tried to use this in class and got the following. Any ideas? Probably something simple i am doing wrong.
by_age <- gss_cat %>%
filter(!is.na(age)) %>%
group_by(age, marital) %>%
count() %>%
mutate(prop = n / sum(n))

ggplot(by_age, aes(age, prop, colour = marital)) +
geom_line(na.rm = TRUE)

by_age

A tibble: 351 x 4

Groups: age, marital [351]

 age       marital     n  prop


1 18 Never married 89 1
2 18 Married 2 1
3 19 Never married 234 1
4 19 Divorced 3 1
5 19 Widowed 1 1
6 19 Married 11 1
7 20 Never married 227 1
8 20 Separated 1 1
9 20 Divorced 2 1
10 20 Married 21 1

... with 341 more rows

You could use

summarise(n = n())

instead of count. This will reduce the groups to "age". And your plot will show something more informative.

1 Like