1

I have longitudinal categorical data that I’d like to visualize as a heatmap-like figure (see heatmap-like plot, but for categorical variables).

The study has three grouping levels: study participant, visit session, & calendar recall diary. A predominant category for each visit was established from the calendar data. For example, a participant might have recorded Category A on 28 of 30 days and Category B on 2 of 30 days; they would be categorized as predominantly Category A at the visit-level).

Occasionally, there are ties in the calendar data. I would like the heatmap to reflect these ties.

Here is some sample data to work with:

dat <- data.frame(person=factor(paste0("id#", 1:5),
                                levels =rev(paste0("id#", 1:5))),
                  matrix(sample(LETTERS[1:3], 15, T), ncol = 3))

dat <- dat %>% mutate(X2 = case_when(person=="id#3" ~ "A;B;C", TRUE ~ X2))

I would like my heatmap to reflect this similar to this example output from excel:

final heatmap

M--
  • 25,431
  • 8
  • 61
  • 93

1 Answers1

0
library(tidyverse)

dat %>% 
  pivot_longer(-person) %>% 
  mutate(val = 1/(str_count(value, ";")+1),
         var = str_split(value, ";")) %>% 
  unnest(var) %>% 
  group_by(person, name, var) %>% 
  summarise(val = sum(val)) %>% 
 ggplot(.) +
  geom_bar(aes(person, val, fill = var), stat = "identity", color = "black", width=1) +
  facet_grid(rows = ~name) +
  coord_flip() +
  theme(rect = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        line = element_blank(),
        panel.spacing = unit(-1, "lines"))

M--
  • 25,431
  • 8
  • 61
  • 93