小编DHR*_*DHR的帖子

ggplot 热图网格线格式 geom_tile 和 geom_rect

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

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

我曾尝试从另一篇文章中运行类似的代码,但无法正常运行。我认为因为我的 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)%>% …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

6
推荐指数
1
解决办法
1126
查看次数

标签 统计

ggplot2 ×1

r ×1