tabyl_pivot_longer.Rd
Title
tabyl_pivot_longer(tabyl, col_var = NULL, list_var = NULL)
tabyl |
---|
datasets::Titanic %>% data.frame() %>% janitor::clean_names() %>% tidyr::uncount(weights = freq) %>% janitor::tabyl(survived, sex) %>% janitor::adorn_totals(c("row", "col")) %>% janitor::adorn_percentages(denominator = "col") %>% janitor::adorn_pct_formatting() %>% janitor::adorn_ns(position = "front") %>% tabyl_pivot_longer(col_var = "sex")#> Warning: `as_data_frame()` was deprecated in tibble 2.0.0. #> Please use `as_tibble()` instead. #> The signature and semantics have changed, see `?as_tibble`.#> # A tibble: 9 × 3 #> survived sex value #> <fct> <fct> <chr> #> 1 No Male "1364 (78.8%)" #> 2 No Female "126 (26.8%)" #> 3 No Total "1490 (67.7%)" #> 4 Yes Male " 367 (21.2%)" #> 5 Yes Female "344 (73.2%)" #> 6 Yes Total " 711 (32.3%)" #> 7 Total Male "1731 (100.0%)" #> 8 Total Female "470 (100.0%)" #> 9 Total Total "2201 (100.0%)"datasets::Titanic %>% data.frame() %>% janitor::clean_names() %>% tidyr::uncount(weights = freq) %>% janitor::tabyl(survived, sex,class) %>% janitor::adorn_totals(c("row", "col")) %>% janitor::adorn_percentages(denominator = "col") %>% janitor::adorn_pct_formatting() %>% janitor::adorn_ns(position = "front") %>% tabyl_pivot_longer(col_var = "sex", list_var = "class")#> # A tibble: 36 × 4 #> class survived sex value #> <fct> <fct> <fct> <chr> #> 1 1st No Male "118 (65.6%)" #> 2 1st No Female " 4 (2.8%)" #> 3 1st No Total "122 (37.5%)" #> 4 1st Yes Male " 62 (34.4%)" #> 5 1st Yes Female "141 (97.2%)" #> 6 1st Yes Total "203 (62.5%)" #> 7 1st Total Male "180 (100.0%)" #> 8 1st Total Female "145 (100.0%)" #> 9 1st Total Total "325 (100.0%)" #> 10 2nd No Male "154 (86.0%)" #> # … with 26 more rowslibrary(ggplot2)#> Warning: package ‘ggplot2’ was built under R version 3.6.2datasets::Titanic %>% data.frame() %>% janitor::clean_names() %>% tidyr::uncount(weights = freq) %>% janitor::tabyl(survived, sex, class) %>% tabyl_pivot_longer(col_var = "sex", list_var = "class") %>% ggplot() + aes(x = sex, y = survived) + facet_wrap(~class) + geom_tile() + aes(fill = value) + geom_text(aes(label = value), color = "white")datasets::Titanic %>% data.frame() %>% janitor::clean_names() %>% tidyr::uncount(weights = freq) %>% janitor::tabyl(survived, sex,class) %>% janitor::adorn_totals(c("row", "col")) %>% janitor::adorn_percentages(denominator = "col") -> percentages_prep percentages_prep %>% janitor::adorn_pct_formatting() %>% janitor::adorn_ns(position = "front") %>% tabyl_pivot_longer(col_var = "sex", list_var = "class") %>% ggplot() + aes(x = sex, y = survived %>% forcats::fct_rev()) + facet_wrap( ~ class) + geom_tile(data = percentages_prep %>% tabyl_pivot_longer(col_var = "sex", list_var = "class"), aes(fill = value)) + geom_text(aes(label = value), color = "white") + scale_fill_viridis_c(limits = 0:1)