ProjectMOSAIC / ggformula

Provides a formula interface to 'ggplot2' graphics.
Other
38 stars 11 forks source link

Add support for plot relabeling. #141

Closed rpruim closed 4 years ago

rpruim commented 4 years ago

Some packages like {expss} and {Hmisc} support labels that are different from variable names. This gives the best of both worlds: short syntactic names for coding and longer, expressive names for plotting and other purposes.

{expss} does this by adding a label attribute to the vectors in a data frame. These labels can be extracted and used in place of the variable names as default labels.

By extending the class of a ggplot produced with ggformula, we can have these extended labels used by default when a plot is printed.

Plan:

rpruim commented 4 years ago

Proof of concept:

suppressPackageStartupMessages(library(ggformula))

KF <-
  mosaicData::KidsFeet %>%
  set_labels(
    list(
      length      = 'foot length (cm)',
      width       = 'foot width (cm)',
      birthmonth  = 'birth month',
      birthyear   = 'birth year',
      biggerfoot  = 'bigger foot',
      domhand     = 'dominant hand'
    )
  )

KF %>%
  gf_point(length ~ width, color = ~ domhand)

get_labels(KF)
#> $name
#> NULL
#> 
#> $birthmonth
#> [1] "birth month"
#> 
#> $birthyear
#> [1] "birth year"
#> 
#> $length
#> [1] "foot length (cm)"
#> 
#> $width
#> [1] "foot width (cm)"
#> 
#> $sex
#> NULL
#> 
#> $biggerfoot
#> [1] "bigger foot"
#> 
#> $domhand
#> [1] "dominant hand"

mosaicData::KidsFeet %>% gf_point(width ~ length) %>% 
  gf_relabel(list(width = "width of foot", length = "length of foot"))

Created on 2020-07-08 by the reprex package (v0.3.0)

rpruim commented 4 years ago

This has been refactored to reexport some functions from the {labelled} package instead of rolling my own.