Home > Software design >  Visualize presence/absence hourly data with ggplot
Visualize presence/absence hourly data with ggplot

Time:07-27

I have a health signal cell data that the server must receive every hour, after data manipulation and pivoting I would like to create a presence-absence data chart from it to be able to find spotty connections per id/date for troubleshooting later.

Here is my sample data after pivot_wider, if the server got a signal in each hour there is 1 and if no signal is received there is NA.

df <- tibble::tribble(
  ~id,        ~date,     ~n,   ~s, ~hour_1, ~hour_2, ~hour_3, ~hour_4, ~hour_5, ~hour_6, ~hour_7, ~hour_8, ~hour_9, ~hour_10, ~hour_11, ~hour_12, ~hour_13, ~hour_14, ~hour_15, ~hour_16, ~hour_17, ~hour_18, ~hour_19, ~hour_20, ~hour_21, ~hour_22, ~hour_23, ~hour_24,
   1L, "2022-07-26", "CELL", TRUE,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,
   2L, "2022-07-26", "CELL", TRUE,      1L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      1L,       1L,       1L,       1L,       1L,       NA,       NA,       1L,       1L,       1L,       1L,       NA,       NA,       1L,       1L,       NA,
   3L, "2022-07-26", "CELL", TRUE,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,
   4L, "2022-07-26", "CELL", TRUE,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,
   5L, "2022-07-26", "CELL", TRUE,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,       1L,       NA,       NA,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,       1L,
   6L, "2022-07-26", "CELL", TRUE,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,      1L,       1L,       1L,       1L,       1L,       1L,       NA,       NA,       NA,       NA,       NA,       1L,       1L,       1L,       1L,       1L
  )

The chart I have in my mind is something like this or similar to this, assume we filtered for specific date, id as a row and presence-absence for every hour with color/no color. Thanks for taking a look

enter image description here

CodePudding user response:

This solution is for the whole dataset: In case you could filter:

The trick is to use data_color function from gt package and Setting the domain of scales::col_numeric(). See here Section examples enter image description here

CodePudding user response:

In ggplot you could do:

library(tidyverse)

df %>%
  select(-(2:4)) %>%
  pivot_longer(-1, names_to = "hour", values_to = "on_off") %>%
  mutate(hour = factor(as.numeric(sub("hour_", "", hour))),
         on_off = factor(on_off),
         id = factor(id)) %>%
  ggplot(aes(hour, id, fill = on_off))  
  geom_tile(color = "gray30")  
  coord_equal()  
  scale_fill_manual(values = "#7AE063", na.value = "white", guide = "none")  
  theme_minimal(base_size = 16)

enter image description here

  • Related