Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - Rotate vis_miss figure #170

Closed
JonathanYe3 opened this issue Nov 30, 2024 · 2 comments
Closed

Feature request - Rotate vis_miss figure #170

JonathanYe3 opened this issue Nov 30, 2024 · 2 comments

Comments

@JonathanYe3
Copy link

Would it be possible to add an option for the vis_miss function to output figures with observations on the x-axis and variables on the y-axis? Simply transposing the ggplot itself does not work, as demonstrated below. It simply switches the labels on the axes, rather than changing the plot itself

library(ggplot2)
library(visdat)

data <- data.frame(
  var1 = c(1, NA, 3, 4, 5),
  var2 = c(NA, 2, 3, NA, 5),
  var3 = c(1, 2, NA, 4, 5)
)

visdat::vis_miss(data)

#attempt to transpose the ggplot (doesn't work as expected)
p <- visdat::vis_miss(data) +
  coord_flip()

print(p)
@njtierney
Copy link
Collaborator

Hi there,

Thanks for your question!

I won't be able to support this unfortunately - it will add complexity to the code, and I'll need some option like "rotate = TRUE". Unfortunately due to the way the plots are created, it isn't as simple as adding "coord_flip()", sorry! Ultimately I would prefer to keep all of the visdat plots the same style.

While I won't add this feature, I did want to show you how to create a similar plot that would get your most of the way there, although it doesn't have the missingness text summaries, using a recent version of visdat allows you to get the data out that is used in the plots.

Hopefully this helps you!

library(ggplot2)
library(visdat)

data <- data.frame(
  var1 = c(1, NA, 3, 4, 5),
  var2 = c(NA, 2, 3, NA, 5),
  var3 = c(1, 2, NA, 4, 5)
)

visdat::vis_miss(data)

p <- visdat::vis_miss(data)
p

p + coord_flip()

vis_miss_data <- visdat::data_vis_miss(data)
vis_miss_data
#> # A tibble: 15 × 4
#>     rows variable valueType value
#>    <int> <chr>    <chr>     <chr>
#>  1     1 var1     FALSE     FALSE
#>  2     1 var2     TRUE      TRUE 
#>  3     1 var3     FALSE     FALSE
#>  4     2 var1     TRUE      TRUE 
#>  5     2 var2     FALSE     FALSE
#>  6     2 var3     FALSE     FALSE
#>  7     3 var1     FALSE     FALSE
#>  8     3 var2     FALSE     FALSE
#>  9     3 var3     TRUE      TRUE 
#> 10     4 var1     FALSE     FALSE
#> 11     4 var2     TRUE      TRUE 
#> 12     4 var3     FALSE     FALSE
#> 13     5 var1     FALSE     FALSE
#> 14     5 var2     FALSE     FALSE
#> 15     5 var3     FALSE     FALSE

# based off of `visdat:::vis_dat_create_`
ggplot(data = vis_miss_data, 
       aes(
         x = variable,
         y = rows
       )) + 
  geom_raster(aes(
    fill = valueType
  )) +
  theme_minimal() + 
  theme(
    axis.text.x = element_text(
      angle = 45,
      vjust = 1,
      hjust = 1
    )
  ) + 
  labs(
    x = "",
    y = "Observations"
  ) + 
  coord_flip() +
  # scale_y_reverse() + 
  theme(
    axis.text.x = element_text(hjust = 0.5)
  )  +
  guides(
    colour = "none"
  ) + 
  scale_fill_manual(name = "", 
                    values = c("grey80", "grey20"),
                    labels = c("Present", 
                               "Missing")) + 
  guides(fill = guide_legend(reverse = TRUE)) 

Created on 2024-12-02 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       macOS Sequoia 15.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Australia/Hobart
#>  date     2024-12-02
#>  pandoc   3.2.1 @ /opt/homebrew/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.3   2024-06-21 [1] CRAN (R 4.4.0)
#>  colorspace    2.1-1   2024-07-26 [1] CRAN (R 4.4.0)
#>  curl          6.0.1   2024-11-14 [1] CRAN (R 4.4.1)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.4.1)
#>  dplyr         1.1.4   2023-11-17 [1] CRAN (R 4.4.0)
#>  evaluate      1.0.1   2024-10-10 [1] CRAN (R 4.4.1)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.4.0)
#>  farver        2.1.2   2024-05-13 [1] CRAN (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
#>  fs            1.6.5   2024-10-30 [1] CRAN (R 4.4.1)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.4.0)
#>  ggplot2     * 3.5.1   2024-04-23 [1] CRAN (R 4.4.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.4.1)
#>  gtable        0.3.6   2024-10-25 [1] CRAN (R 4.4.1)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  knitr         1.49    2024-11-08 [1] CRAN (R 4.4.1)
#>  labeling      0.4.3   2023-08-29 [1] CRAN (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
#>  munsell       0.5.1   2024-04-01 [1] CRAN (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.4.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.4.1)
#>  rstudioapi    0.17.1  2024-10-22 [1] CRAN (R 4.4.1)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
#>  tidyr         1.3.1   2024-01-24 [1] CRAN (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
#>  visdat      * 0.6.0   2023-02-02 [1] CRAN (R 4.4.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.4.1)
#>  xfun          0.49    2024-10-31 [1] CRAN (R 4.4.1)
#>  xml2          1.3.6   2023-12-04 [1] CRAN (R 4.4.0)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Users/nick/Library/R/arm64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@JonathanYe3
Copy link
Author

Sounds good, thank you for the demo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants