Skip to contents

Facet app inputs

Usage

pull_facet_cols(df)

Arguments

df

a data.frame or tibble

Value

a named character vector or column from with < 5 unique levels

Variables to use for facets

This function is designed to quickly determine which variables have an appropriate number of categorical levels for using ggplot2::facet_wrap() or ggplot2::facet_grid()

Examples

require(dplyr)
require(NHANES)
pull_facet_cols(df = dplyr::starwars)
#>   sex 
#> "sex" 
pull_facet_cols(df = NHANES::NHANES)
#>            HomeOwn               Work   BMICatUnder20yrs            BMI_WHO 
#>          "HomeOwn"             "Work" "BMICatUnder20yrs"          "BMI_WHO" 
#>     LittleInterest          Depressed     SexOrientation        PregnantNow 
#>   "LittleInterest"        "Depressed"   "SexOrientation"      "PregnantNow" 

str(dplyr::select(
  dplyr::starwars,
  dplyr::all_of(pull_facet_cols(df = dplyr::starwars))
))
#> tibble [87 × 1] (S3: tbl_df/tbl/data.frame)
#>  $ sex: chr [1:87] "male" "none" "none" "male" ...
str(dplyr::select(
  NHANES::NHANES,
  dplyr::all_of(pull_facet_cols(df = NHANES::NHANES))
))
#> tibble [10,000 × 8] (S3: tbl_df/tbl/data.frame)
#>  $ HomeOwn         : Factor w/ 3 levels "Own","Rent","Other": 1 1 1 1 2 2 1 1 1 1 ...
#>  $ Work            : Factor w/ 3 levels "Looking","NotWorking",..: 2 2 2 NA 2 NA NA 3 3 3 ...
#>  $ BMICatUnder20yrs: Factor w/ 4 levels "UnderWeight",..: NA NA NA NA NA NA NA NA NA NA ...
#>  $ BMI_WHO         : Factor w/ 4 levels "12.0_18.5","18.5_to_24.9",..: 4 4 4 1 4 1 2 3 3 3 ...
#>  $ LittleInterest  : Factor w/ 3 levels "None","Several",..: 3 3 3 NA 2 NA NA 1 1 1 ...
#>  $ Depressed       : Factor w/ 3 levels "None","Several",..: 2 2 2 NA 2 NA NA 1 1 1 ...
#>  $ SexOrientation  : Factor w/ 3 levels "Bisexual","Heterosexual",..: 2 2 2 NA 2 NA NA 1 1 1 ...
#>  $ PregnantNow     : Factor w/ 3 levels "Yes","No","Unknown": NA NA NA NA NA NA NA NA NA NA ...