ggplot:将网格线与geom_tile的末端对齐 [英] ggplot: align grid lines with end of geom_tile

查看:49
本文介绍了ggplot:将网格线与geom_tile的末端对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ggplots geom_tile 创建一个热图.就目前而言,网格线位于每个 geom_tile 的中间.我想要的是将网格线与每个图块的开始/结尾对齐.我看过一些相关的文章((v0.3.0)创建于2020-01-28

解决方案

我已经成功地使用 geom_rect ggplot2 中制作了我的热图.尽管前端还有更多数据精打细算,但我发现它更加灵活且易于自定义.

  my_iris<-虹膜%&%;%mutate(sepal_interval = cut(Sepal.Length,4))%&%group_by(sepal_interval,种)%>%summarise(n_obs = n())%&%mutate(sepal.id = as.numeric(as.factor(sepal_interval)),species.id = as.numeric(as.factor(Species)))species.key<-级别(factor(my_iris $ Species))sepal.int.key<-级别(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,填充= n_obs))+theme_bw()+scale_x_continuous(breaks = seq(1.5,length(sepal.int.key)+0.5,1),标签= sepal.int.key,expand = c(0,0))+scale_y_continuous(breaks = seq(1.5,length(species.key)+ 0.5,1),labels = species.key,展开= c(0,0))+主题(panel.grid.major.x = element_blank(),panel.grid.major.y = element_blank(),panel.ontop = TRUE,panel.background = element_rect(fill ="transparent")) 

输出:

I am trying to create a heatmap with ggplots geom_tile. As it stands the grid lines are centered at the middle of each geom_tile. What I want is the gridlines to be aligned with the start/end of each tile. I have seen a few related posts (here, here, but all are dealing with continuous scales. In my case, both scales are discrete/factors. Any idea? Many thanks!

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"))

Created on 2020-01-28 by the reprex package (v0.3.0)

解决方案

I've had a lot of success using geom_rect to make my heatmaps in ggplot2. While there's a bit more data finessing on the front-end, I've found that it's more flexible and easily customizable.

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"))

With the output:

这篇关于ggplot:将网格线与geom_tile的末端对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆