Skip to content

Consider adding x and y tick labels columns to layer_data() #5004

Closed

Description

@jooyoungseo

layer_data() provides useful information for people with and without visual impairments to better understand current ggplot object in a table form.

However, this data does not contain x/y labels so it is challenging to associate each x/y values with their corresponding labels.

For example, in the following bar plot, the x labels ("2seater", "compact", "midsize", "minivan", "pickup", "subcompact", "suv") are not contained in layer_data() data frame.

                 Of course, it is possible to retrieve them by defining a custom function just like what {BrailleR} does like below; however, being able to see all the labels along with their corresponding values in one data frame would not only be more desirable, but also be more stable and consistent.

Reprex

library(ggplot2)

g <- ggplot(mpg, aes(class)) + geom_bar()

g

layer_data(g)
#>    y count prop x flipped_aes PANEL group ymin ymax xmin xmax colour   fill
#> 1  5     5    1 1       FALSE     1     1    0    5 0.55 1.45     NA grey35
#> 2 47    47    1 2       FALSE     1     2    0   47 1.55 2.45     NA grey35
#> 3 41    41    1 3       FALSE     1     3    0   41 2.55 3.45     NA grey35
#> 4 11    11    1 4       FALSE     1     4    0   11 3.55 4.45     NA grey35
#> 5 33    33    1 5       FALSE     1     5    0   33 4.55 5.45     NA grey35
#> 6 35    35    1 6       FALSE     1     6    0   35 5.55 6.45     NA grey35
#> 7 62    62    1 7       FALSE     1     7    0   62 6.55 7.45     NA grey35
#>   size linetype alpha
#> 1  0.5        1    NA
#> 2  0.5        1    NA
#> 3  0.5        1    NA
#> 4  0.5        1    NA
#> 5  0.5        1    NA
#> 6  0.5        1    NA
#> 7  0.5        1    NA

# Use custom function
.getGGXTicks <- function(x, xbuild, layer) {
  # The location of this item is changing in an upcoming ggplot version
  if ("panel_ranges" %in% names(xbuild$layout)) {
    return(xbuild$layout$panel_ranges[[layer]]$x.labels) # ggplot 2.2.1
  } else {
    xlabs <- xbuild$layout$panel_params[[1]]$x$get_labels()
    return(xlabs[!is.na(xlabs)])
  }
}

.getGGXTicks(g, ggplot_build(g), 1)
#> [1] "2seater"    "compact"    "midsize"    "minivan"    "pickup"    
#> [6] "subcompact" "suv"

Created on 2022-10-02 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.1 (2022-06-23 ucrt)
#>  os       Windows 10 x64 (build 22621)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/Chicago
#>  date     2022-10-02
#>  pandoc   2.19.2 @ C:/PROGRA~3/CHOCOL~1/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.2.0)
#>  cli           3.4.1      2022-09-23 [1] CRAN (R 4.2.1)
#>  colorspace    2.0-3      2022-02-21 [1] CRAN (R 4.2.0)
#>  curl          4.3.2      2021-06-23 [1] CRAN (R 4.2.0)
#>  DBI           1.1.3      2022-06-18 [1] CRAN (R 4.2.0)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.2.0)
#>  dplyr         1.0.10     2022-09-01 [1] CRAN (R 4.2.1)
#>  evaluate      0.16       2022-08-09 [1] CRAN (R 4.2.1)
#>  fansi         1.0.3      2022-03-24 [1] CRAN (R 4.2.0)
#>  farver        2.1.1      2022-07-06 [1] CRAN (R 4.2.1)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.2.0)
#>  fs            1.5.2      2021-12-08 [1] CRAN (R 4.2.0)
#>  generics      0.1.3      2022-07-05 [1] CRAN (R 4.2.1)
#>  ggplot2     * 3.3.6      2022-05-03 [1] CRAN (R 4.2.0)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
#>  gtable        0.3.1      2022-09-01 [1] CRAN (R 4.2.1)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.3      2022-07-18 [1] CRAN (R 4.2.1)
#>  httr          1.4.4      2022-08-17 [1] CRAN (R 4.2.1)
#>  knitr         1.40       2022-08-24 [1] CRAN (R 4.2.1)
#>  labeling      0.4.2      2020-10-20 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.2      2022-09-09 [1] CRAN (R 4.2.1)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.2.0)
#>  mime          0.12       2021-09-28 [1] CRAN (R 4.2.0)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.2.0)
#>  pillar        1.8.1      2022-08-19 [1] CRAN (R 4.2.1)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.2.0)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.2.0)
#>  R.cache       0.16.0     2022-07-21 [1] CRAN (R 4.2.1)
#>  R.methodsS3   1.8.2      2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0     2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.0     2022-06-28 [1] CRAN (R 4.2.1)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.2.0)
#>  reprex        2.0.2      2022-08-17 [1] CRAN (R 4.2.1)
#>  rlang         1.0.6      2022-09-24 [1] CRAN (R 4.2.1)
#>  rmarkdown     2.16       2022-08-24 [1] CRAN (R 4.2.1)
#>  scales        1.2.1      2022-08-20 [1] CRAN (R 4.2.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.8      2022-07-11 [1] CRAN (R 4.2.1)
#>  stringr       1.4.1      2022-08-20 [1] CRAN (R 4.2.1)
#>  styler        1.7.0.9003 2022-10-03 [1] Github (r-lib/styler@b41f8ac)
#>  tibble        3.1.8      2022-07-22 [1] CRAN (R 4.2.1)
#>  tidyselect    1.1.2      2022-02-21 [1] CRAN (R 4.2.0)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.2.0)
#>  vctrs         0.4.2      2022-09-29 [1] CRAN (R 4.2.1)
#>  withr         2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.33       2022-09-12 [1] CRAN (R 4.2.1)
#>  xml2          1.3.3      2021-11-30 [1] CRAN (R 4.2.0)
#>  yaml          2.3.5      2022-02-21 [1] CRAN (R 4.2.0)
#> 
#>  [1] C:/Program Files/R/R-4.2.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Activity

  1. thomasp85 commented on Oct 3, 2022

    @thomasp85
    Member

    Hi JooYoung

    layer_data() has always ever been intended for testing purposes and I'm a bit apprehensive about tacking on more information than the actual layer data for fear that it turns into some sort of catch-all function.

    However, I do think we should think about providing a suite of functions that can extract relevant information about the final state of a plot for use with textual descriptions etc. We will soon have some more ressources for this so hopefully we can begin to build this out in the release following the one I'm currently preparing.

    best
    Thomas

  2. teunbrand commented on Dec 1, 2022

    @teunbrand
    Collaborator

    I think it might be useful to build something like layer_data(), but for guide breaks, positions and labels. I've noticed people trying to access these values (i.e. #5029, #5063), and I think accessor functions, as mentioned here, are a great idea that leaves maintainers free to change internal structures as long as the accessors continue to work. The current internal structure for guides are poised to change again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions