ropensci / visdat

Preliminary Exploratory Visualisation of Data

Home Page:https://docs.ropensci.org/visdat/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

implement vis_binary

njtierney opened this issue · comments

vis_value works for continuous values, but for cases where there are only binary values, this might be useful.

library(visdat)
library(tidyverse)

assign_na <- function(x, size){
  x[sample(length(x), size = size)] <- NA
  x
}

dat <- tibble(x = sample(x = c(0L, 1L), size = 100, replace = TRUE),
              y = sample(x = c(0L, 1L), size = 100, replace = TRUE),
              z = sample(x = c(0L, 1L), size = 100, replace = TRUE)) %>% 
  mutate_all(assign_na, 10)

vis_dat(dat)

vis_miss(dat)

vis_value(dat)

vis_value(dat, viridis_option = "E", na_colour = "grey")

# or 
scale_01 <- function(x) {
  (x - min(x, na.rm = TRUE)) / diff(range(x, na.rm = TRUE))
}

vis_binary <- function(data) {
  purrr::map_dfr(data, scale_01) %>%
    visdat:::vis_gather_() %>%
    dplyr::mutate(value = visdat:::vis_extract_value_(data)) %>%
    mutate(valueType = as.factor(valueType),
           value = as.factor(value)) %>% 
    visdat:::vis_create_() +
    # change the limits etc.
    ggplot2::guides(fill = ggplot2::guide_legend(title = "Value")) +
    # add info about the axes
    ggplot2::scale_x_discrete(position = "top") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(hjust = 0)) +
    ggplot2::scale_fill_manual(values = c("salmon", 
                                          "steelblue2"))
}
vis_miss(dat)

vis_binary(dat)
#> Warning: Removed 30 rows containing missing values (geom_raster).

dat %>% 
  arrange(x) %>% 
  vis_binary()
#> Warning: Removed 30 rows containing missing values (geom_raster).

dat %>% 
  arrange(x,y,z) %>% 
  vis_binary()
#> Warning: Removed 30 rows containing missing values (geom_raster).

Created on 2019-12-06 by the reprex package (v0.3.0)

in the future I might consider vis_integer as well.