几天来,我一直致力于创建热图,但我无法让网格线的最终格式正常工作。请参阅下面的代码和附图。我要做的是使用 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)