如何在 R 中生成带注释的每月热图?

How to produce annotated monthly heatmap in R?

我有一个类似如下的数据集:

df <- structure(list(Topic = c("Topic_A", "Topic_A", 
                              "Topic_A", "Topic_A", "Topic_A", 
                              "Topic_A", "Topic_A", "Topic_A", 
                              "Topic_A", "Topic_A", "Topic_A", 
                              "Topic_B", "Topic_B", "Topic_B", 
                              "Topic_B", "Topic_B", "Topic_B", 
                              "Topic_B", "Topic_B", "Topic_B", 
                              "Topic_B", "Topic_B"), 
                    month = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
                                        11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("Jan", 
                                        "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
                                        "Nov", "Dec"), class = c("ordered", "factor")), N = c(439L, 
                                        291L, 592L, 527L, 336L, 254L, 141L, 137L, 68L, 182L, 125L, 
                                        48L, 111L, 74L, 127L, 55L, 80L, 105L, 129L, 63L, 72L, 82L
                                        )), row.names = c(NA, 22L), class = "data.frame")

我使用以下代码生成月度热图,X 轴为月份名称,Y 轴为主题名称:

ggplot(df, aes( month,Topic)) + geom_tile(aes(fill = N),colour = "white") +
  #scale_fill_gradient(low = col1, high = col2) +  
  scale_fill_viridis(name="Hrly Temps C",option ="C")+
  guides(fill=guide_legend(title="Frequency")) +
  labs(title = "Topic Heatmap",
       x = "Month", y = "Topic") +
  theme_bw() + theme_minimal() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

由于在我的原始数据集中,几乎每个月的主题名称都不同,我想用主题名称对其对应月份的热图的每个框进行注释,在这种情况下删除 Y 轴,并在其中添加文本每个月的热图框。

任何指导我该怎么做?

您可以添加 geom_text() 为每个面板添加文本,并使用主题中的 element_blank() 删除 y 轴值

ggplot(df, aes( month,Topic)) + geom_tile(aes(fill = N),colour = "white") +
  #scale_fill_gradient(low = col1, high = col2) +  
  scale_fill_viridis_c(name="Hrly Temps C")+
  guides(fill=guide_legend(title="Frequency")) +
  labs(title = "Topic Heatmap",
       x = "Month", y = "Topic") +
  theme_minimal() + 
  geom_text( aes(x = month, y = Topic, label = Topic), angle = 90, color = "white") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text.y = element_blank())