几天来,我一直致力于创建热图,但无法使网格线的最终格式正常工作。请参阅下面的代码和附图。我想要做的是使用 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)%>%
ggplot()+
geom_raster(aes(x=x,y=fct_reorder(y,n,sum),fill=n),hjust=0,vjust=0)
Run Code Online (Sandbox Code Playgroud)
这有点像黑客。我的方法将分类变量转换为数字,从而在绘图中添加与图块对齐的小网格线。为了摆脱主要的网格线,我只需使用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())
Run Code Online (Sandbox Code Playgroud)

由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)
Run Code Online (Sandbox Code Playgroud)

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