rggplot2naniar

Plot state of boolean variables as bar over time


I am trying to do a visualisation on the state of multiple boolean variables. I managed to get a hacky solution using naniar:

library(tidyverse)
library(naniar)
a <- c(TRUE, FALSE, TRUE,FALSE)
b <- c(FALSE, FALSE, TRUE,FALSE)
c <- c(FALSE, TRUE, TRUE,FALSE)

data.frame(a,b,c) %>%
  mutate_all(~ if_else(., NA, FALSE)) %>%
  vis_miss()

missing value visualisation using naniar

Obviously that is abusing this library and doesn't have the right labels. How could I achieve the same thing in ggplot (say with a colour variable of enabled TRUE or FALSE)?


Solution

  • You could first transform your data to a longer format using pivot_longer. You could use geom_tile to create a heatmap like this:

    library(tidyverse)
    data.frame(a,b,c) %>%
      mutate(ID = row_number()) %>%
      pivot_longer(cols = a:c) %>%
      ggplot(aes(x = name, y = factor(ID), fill = value)) +
      geom_tile() +
      labs(fill = "Enabled", y = 'ID', x = "Name")
    

    Created on 2023-03-29 with reprex v2.0.2