如何将值添加到热图中

时间:2018-01-04 16:54:42

标签: r ggplot2 heatmap

我的数据看起来像这样

df<- structure(list(time = c(1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L), grp = c("A", "B", "C", "D", "E", "F", "A", "B", "C", "D", "E", "F"), `02` = c(36034L, 63763L, 51432L, 65100L, 61444L, 71012L, 266610L, 389787L, 47659L, 63156L, 84593L, 84331L), `03` = c(45632L,66505L, 60360L, 36685L, 107551L, 53360L, 323952L, 344944L, 69601L, 51268L, 130665L, 59704L), `04` = c(59025L, 52837L, 68571L, 35788L, 75262L, 66601L, 424683L, 340948L, 79487L, 42809L, 95607L, 81739L ), `05` = c(74767L, 48210L, 70972L, 67705L, 85576L, 89265L, 393380L, 306633L, 77816L, 73611L, 106317L, 116890L), `06` = c(50846L, 
    37970L, 63896L, 78296L, 81216L, 62308L, 62613L, 21770L, 80955L, 
    88832L, 97586L, 68345L), `07` = c(26688L, 27830L, 17010L, 54074L, 
    26727L, 31109L, 24448L, 38701L, 17378L, 46327L, 25324L, 25325L
    ), `08` = c(16498L, 26604L, 41201L, 38417L, 43709L, 33217L, 69943L, 
    80638L, 37444L, 31701L, 46781L, 31152L), `09` = c(16272L, 24485L, 
    14546L, 74756L, 28193L, 770L, 72238L, 78418L, 9161L, 48618L, 
    26466L, 1078L), `10` = c(20612L, 713L, 18114L, 57872L, 25684L, 
    27985L, 73618L, 1770L, 11953L, 33347L, 25824L, 25860L), `11` = c(23549L, 
    856L, 32854L, 42906L, 33385L, 26218L, 88509L, 62103L, 23377L, 
    29738L, 33504L, 26642L)), .Names = c("time", "grp", "02", "03", 
    "04", "05", "06", "07", "08", "09", "10", "11"), row.names = c(NA, 
    12L), class = "data.frame")

我试图以这种方式绘制它,并在那里添加值

require(ggplot2) 
require(reshape2) 
myt <- melt(df, id = c("time", "grp"), 
            variable.name = "Participant", value.name = "Score") 
ggplot() + geom_tile(data = myt, aes(x = grp, y = Participant, fill = Score))+
  facet_grid(. ~ time)

我试图通过执行此操作在热图中添加值

ggplot() + geom_tile(data = myt, aes(x = grp, y = Participant, fill = Score))+ geom_text(aes(label = round(Score, 1)))+
  facet_grid(. ~ time)

但我收到此错误

  

if(empty(data)){:缺少值需要TRUE / FALSE

时出错

1 个答案:

答案 0 :(得分:1)

通过将数据调用ggplot命令解决问题来重新组织代码。

ggplot(data = myt, aes(x = grp, y = Participant, fill = Score)) + 
  geom_tile()+ 
  geom_text(aes(label = round(Score, 1)))+
  facet_grid(. ~ time)

enter image description here