6

几天来,我一直致力于创建热图,但我无法让网格线的最终格式正常工作。请参阅下面的代码和附图。我要做的是使用 geom_tile() 沿着热图的图块对齐网格线,以便每个图块以框的方式填充网格的内部。我能够使用 geom_raster() 对齐网格线,但是 y 轴标签在瓷砖的顶部或底部打勾,但我需要它在中心打勾(见红色突出显示),我也无法让 geom_raster 换行瓷砖周围的白线边框,因此颜色块在我的原始数据集中看起来有点杂乱无章。对于格式化代码的任何帮助将不胜感激。非常感谢!

#The data set in long format 


y<- c("A","A","A","A","B","B","B","B","B","C","C","C","D","D","D")
    x<- c("2020-03-01","2020-03-15","2020-03-18","2020-03-18","2020-03-01","2020-03-01","2020-03-01","2020-03-01","2020-03-05","2020-03-06","2020-03-05","2020-03-05","2020-03-20","2020-03-20","2020-03-21")
    v<-data.frame(y,x)

#approach 1 using geom_tile but gridline does not align with borders of the tiles 
    v%>%
      count(y,x,drop=FALSE)%>%
      arrange(n)%>%
      ggplot(aes(x=x,y=fct_reorder(y,n,sum)))+
      geom_tile(aes(fill=n),color="white", size=0.25)

需要平铺边框与网格线对齐

我曾尝试从另一篇文章运行类似的代码,但我无法让它正常运行。我认为因为我的 x 变量是 y 变量的计数变量,所以不能格式化为因子变量以在 geom_rect() 中指定 xmin 和 xmax

#approach 2 using geom_raster but y-axis label can't tick at the center of tiles and there's no border around the tile to differentiate between tiles. 

v%>%
  count(y,x,drop=FALSE)%>%
  arrange(n)%>%
  ggplot()+
  geom_raster(aes(x=x,y=fct_reorder(y,n,sum),fill=n),hjust=0,vjust=0)

需要 y 轴标签在图块的中心打勾,并且需要在图块周围设置边框

4

2 回答 2

3

我认为保持刻度线和网格线在它们所在的位置是有意义的。为了仍然实现您正在寻找的内容,我建议您扩展您的数据以包含所有可能的组合,并将其设置na.value为中性填充颜色:

# all possible combinations
all <- v %>% expand(y, x)

# join with all, n will be NA for obs. in all that are not present in v
v = v %>% group_by_at(vars(y, x)) %>% 
    summarize(n = n()) %>% right_join(all)

ggplot(data = v, 
       aes(x=x, y=fct_reorder(y,n, function(x) sum(x, na.rm = T))))+ # note that you must account for the NA values now 
geom_tile(aes(fill=n), color="white",
        size=0.25) +
scale_fill_continuous(na.value = 'grey90') +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0))
于 2020-05-23T08:28:24.243 回答
2

这有点骇人听闻。我的方法将分类变量转换为数字,从而将次要网格线添加到与图块对齐的图中。为了摆脱我简单地使用的主要网格线theme()。缺点:必须手动设置中断和标签。

library(ggplot2)
library(dplyr)
library(forcats)

v1 <- v %>%
  count(y,x,drop=FALSE)%>%
  arrange(n) %>%
  mutate(y = fct_reorder(y, n, sum),
         y1 = as.integer(y),
         x = factor(x),
         x1 = as.integer(x))

labels_y <- levels(v1$y)
breaks_y <- seq_along(labels_y)

labels_x <- levels(v1$x)
breaks_x <- seq_along(labels_x)

ggplot(v1, aes(x=x1, y=y1))+
  geom_tile(aes(fill=n), color="white", size=0.25) + 
  scale_y_continuous(breaks = breaks_y, labels = labels_y) +
  scale_x_continuous(breaks = breaks_x, labels = labels_x) +
  theme(panel.grid.major = element_blank())

reprex 包(v0.3.0)于 2020-05-23 创建

编辑:检查长变量名称

y<- c("John Doe","John Doe","John Doe","John Doe","Mary Jane","Mary Jane","Mary Jane","Mary Jane","Mary Jane","C","C","C","D","D","D")
x<- c("2020-03-01","2020-03-15","2020-03-18","2020-03-18","2020-03-01","2020-03-01","2020-03-01","2020-03-01","2020-03-05","2020-03-06","2020-03-05","2020-03-05","2020-03-20","2020-03-20","2020-03-21")
v<-data.frame(y,x)

reprex 包(v0.3.0)于 2020-05-23 创建

于 2020-05-23T08:40:59.467 回答