Reputation: 125
I have a data frame looking like this:
structure(list(time = 1:36, X2018 = c("1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0"), X2019 = c("0", "0", "1", "1",
"1", "1", "1", "1", "1", "1", "0", "0", "1", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0"), X2020 = c("1", "1", "1", "1",
"1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0")), row.names = c(NA, -36L), class = "data.frame")
Column "time" is the number of the decade (= 10 days) of a year. Column "X2018" has the data entries for the year 2018. Data entries are either 1 (there is data) or 0 (there is no data/NA).
I want to create a plot looking something like this, that shows the decades of different years that have data entries:
I tried generating a plot using ggplot2. I think barplots aren't the way to go here, as they visualize a total count starting at 0.
I looked into gantt charts, but I feel this is too complicated and I was wondering if someone could give me an hint on how to solve this or what packages I could look into.
Upvotes: 1
Views: 271
Reputation: 56179
Reshape data wide-to-long then use geom_tiles:
library(ggplot2)
library(data.table)
setDT(dt)
#reshape
plotDat <- melt(df1, id.vars = "time", variable.name = "year")
#pretty xlimits
xlimits <- range(plotDat$time)
# plot tiles
ggplot(plotDat[ value == 1, ], aes(time, year)) +
geom_tile(fill = "lightblue") +
scale_x_continuous(limits = xlimits, breaks = seq(xlimits[1], xlimits[2]))
Upvotes: 2