0

By having the following dataframe:

df = data.frame(Date=1:5,
           Cat1=c(1,0,1,0,0),
           Cat2=c(1,1,1,0,0),
           Cat3=c(0,1,1,0,1),
           Cat4=c(0,0,1,1,0))

By using ggplot2, how would be possible to achieve this plot?

enter image description here

PeCaDe
  • 277
  • 1
  • 8
  • 33
  • This is really more a comment than a solution, but, why wouldn't you use any package for table editing, e.g., `gt`, `formattable`, `DT`. By creating a table, you can fill it later with data, so it is handier for potential future actions. – Bloxx Oct 29 '21 at 11:15
  • 1
    It is more a matter of achieving a way to plot presence(1)/absence(0) values along time, rather than other issue. – PeCaDe Oct 29 '21 at 11:19

1 Answers1

4

You need to pivot the data into long format, then convert the 0 and 1 values into factors. You can then plot with geom_tile, using the values as fill colours.

library(ggplot2)

ggplot(tidyr::pivot_longer(df, -1),
         aes(x = Date, y = factor(name, levels = rev(unique(name))), 
             fill = as.factor(value))) +
  geom_tile(color = "black") +
  scale_fill_manual(values = c("white", "grey50")) +
  labs(y = "") +
  theme_void() +
  theme(legend.position = "none",
        axis.text = element_text(size = 15),
        axis.title.x = element_text(size = 15),
        plot.margin = margin(20, 20, 20, 20))

enter image description here

And of course, you have lots of options with how the final plot looks. For example:

ggplot(tidyr::pivot_longer(df, -1),
         aes(x = Date, y = factor(name, levels = rev(unique(name))), 
             fill = as.factor(value))) +
  geom_tile(color = "black", size = 1) +
  scale_fill_manual(values = c("gold", "deepskyblue4")) +
  coord_equal() +
  labs(y = "") +
  theme_void() +
  theme(legend.position = "none",
        axis.text = element_text(size = 15),
        axis.title.x = element_text(size = 15),
        plot.margin = margin(20, 20, 20, 20))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87