'How to plot graph like this using R

enter image description here

I don't even know the name of this plot and what kind of data is required to make this, so sorry I don't really have a minimal reproducible sample. with x axis the time and y axis all the variables,in each period each variable can be selected or not selected, indicated by the blue colour.

this plot is from Freyberger, Joachim, Andreas Neuhierl, and Michael Weber. "Dissecting characteristics nonparametrically." The Review of Financial Studies 33.5 (2020): 2326-2377.



Solution 1:[1]

You could use ggplot.

First we'll create some plausible sample data:

set.seed(1)

df <- data.frame(on_off = c(replicate(26, cumsum(rnorm(20)))) > 3,
                 year = rep(2001:2020, 26),
                 factor = rep(LETTERS, each = 20))

And the plotting code is something like this:

library(ggplot2)

ggplot(df, aes(year, factor, fill = on_off)) +
  geom_tile() +
  scale_fill_manual(values = c('TRUE' = 'blue2', 'FALSE' = 'white')) +
  theme_classic() + 
  theme(legend.position = 'none')

enter image description here

Solution 2:[2]

You can build it as a heatmap, where the value can only be 0 or 1. Here's an example of how this can be achieved with ggplot (from here):

ggplot(df1, aes(variable, indv)) + geom_tile(aes(fill = value),
   colour = "white")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron
Solution 2 iod