在热图的每一列中可视化多个变量

时间:2019-04-29 10:08:35

标签: r

我正在尝试使用geom_tile绘制图(热图)。由于我有多个变量,因此我想将每列/单元格“拆分”为两个(或变量数量)。 我整理了数据,看起来像这样:

 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

所以,我喜欢在第一列(例如一年)中看到v1和v2来比较日期期间的变化。

下面,我提供了一个与我使用的结构相似的小样本。

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))

到目前为止,我一直在尝试不同的方法,但是它没有用..我找不到任何有效的解决方案。我的尝试:

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

我还尝试重新排列数据并将其合并到具有变量(v1,v2)的一列中,但是它也不起作用。

有什么主意吗?也许geom_tile不是最好的方法?有什么建议吗?

0 个答案:

没有答案