我正在尝试使用创建热图ggplots geom_tile。目前,网格线以每个网格线的中间为中心geom_tile。我想要的是网格线与每个图块的开始/结束对齐。我看过一些相关的帖子(此处,此处,但所有帖子都在处理连续尺度。就我而言,两个尺度都是离散/因子。有什么想法吗?非常感谢!
library(tidyverse)
my_iris <- iris %>%
mutate(sepal_interval=cut(Sepal.Length, 4)) %>%
group_by(sepal_interval, Species)
my_iris %>%
summarise(n_obs=n()) %>%
ggplot()+
geom_tile(aes(y=Species,
x=sepal_interval,
fill=n_obs))+
theme_bw()+
theme(panel.grid = element_line(color="black"))
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-01-28 创建
小智 6
我已经成功geom_rect地使用ggplot2. 虽然前端有更多的数据处理,但我发现它更灵活且更容易定制。
my_iris <- iris %>%
mutate(sepal_interval=cut(Sepal.Length, 4)) %>%
group_by(sepal_interval, Species) %>%
summarise(n_obs=n()) %>%
mutate(sepal.id = as.numeric(as.factor(sepal_interval)),
species.id = as.numeric(as.factor(Species)))
species.key <- levels(factor(my_iris$Species))
sepal.int.key <- levels(factor(my_iris$sepal_interval))
my_iris %>%
ggplot() +
geom_rect(aes(xmin = sepal.id, xmax = sepal.id+1, ymin = species.id, ymax = species.id+1, fill = n_obs)) +
theme_bw() +
scale_x_continuous(breaks = seq(1.5, length(sepal.int.key)+0.5, 1), labels = sepal.int.key, expand = c(0,0)) +
scale_y_continuous(breaks = seq(1.5, length(species.key) + 0.5, 1), labels = species.key, expand = c(0,0)) +
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.ontop = TRUE,
panel.background = element_rect(fill = "transparent"))
Run Code Online (Sandbox Code Playgroud)