I'm trying to make a plot (heatmap) using geom_tile. Since I have several variables, I'd like to "split" each column/cell in two (or number of variables). I arranged the data, and it looks like:

 head(mydata)

            date  v1 v2 year
       1990-04-01 NA NA 1990
       1990-04-02 NA NA 1990
        ...................
       1990-04-30 13 NA 1990
       1990-05-01 13 NA 1990
       1990-05-02 13  3 1990
       1990-05-03 13  3 1990

So, I like to see for one column (e.g. one year), both v1 and v2 to compare the evolution during the dates.

Below, I provide a small sample with a similar structure I'm working with.

dput(sample)

structure(list(date = structure(c(7395, 7396, 7397, 7398, 7399, 
7400, 7401, 7402, 7403, 7404, 7405, 7406, 7407, 7408, 7409, 7410, 
7411, 7412, 7413, 7414, 7415, 7416, 7417, 7418, 7419, 7420, 7421, 
7422, 7423, 7424, 7425, 7426, 7427, 7428, 7429, 7430, 7431, 7432, 
7433, 7434, 7435, 7436, 7437, 7438, 7439, 7440, 7441, 7442, 7443, 
7444, 7445, 7446, 7447, 7448, 7449, 7450, 7451, 7452, 7453, 7454, 
7455, 7760, 7761, 7762, 7763, 7764, 7765, 7766, 7767, 7768, 7769, 
7770, 7771, 7772, 7773, 7774, 7775, 7776, 7777, 7778, 7779, 7780, 
7781, 7782, 7783, 7784, 7785, 7786, 7787, 7788, 7789, 7790, 7791, 
7792, 7793, 7794, 7795, 7796, 7797, 7798, 7799, 7800, 7801, 7802, 
7803, 7804, 7805, 7806, 7807, 7808, 7809, 7810, 7811, 7812, 7813, 
7814, 7815, 7816, 7817, 7818, 7819, 7820), class = "Date"), v1 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA, NA, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, NA, 
1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 22L, 22L, 22L, 22L, 
22L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L), 
    v2 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 3L, 3L, 3L, 3L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), year = c("1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991")), class = "data.frame", row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 
55L, 56L, 57L, 58L, 59L, 60L, 61L, 184L, 185L, 186L, 187L, 188L, 
189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 
200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 
211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L, 
222L, 223L, 224L, 225L, 226L, 227L, 228L, 229L, 230L, 231L, 232L, 
233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 
244L))

So far, I've been trying different approaches but it's not working..I couldn't find any solution that works. My tries:

ggplot2::ggplot(sample, aes(x=year,y=format(date, "%m-%d"))) + 
geom_tile(aes(fill=v1)) + geom_tile(aes(fill=v2)) + ylab("")

I also tried re-arranging the data and melting in one column with variable (v1, v2), but it's not working either.

Any idea? Maybe geom_tile is not the best approach? any suggestion?

0 Answers