美文网首页
跟着Nature学作图:R语言ggplot2三角热图按照指定的角

跟着Nature学作图:R语言ggplot2三角热图按照指定的角

作者: 小明的数据分析笔记本 | 来源:发表于2023-03-27 14:49 被阅读0次

论文

Whole-genome doubling drives oncogenic loss of chromatin segregation

https://www.nature.com/articles/s41586-023-05794-2#MOESM10

作图数据都有,论文中的图也很好看,抽时间复现

今天的推文复现一下论文中的Figure 1e 三角热图

ggplot2能够做这种三角热图,但是怎么让热图的尖朝上,之前还没有尝试过,基本思路就是可以让整个图进行旋转,查了一下怎么让ggplot2整体旋转,很多都是借助grid包的语法来实现,但是grid的作图我还不是很理解,找了好长时间看有没有ggplot2的扩展包可以做,找到了一个参考链接

https://www.jianshu.com/p/802bdefa8feb

这里借助cowplot和ggplotify两个R包

一个简单的小例子

library(ggplot2)

pp<-ggplot()+
  geom_point(aes(x=1,y=1))+
  coord_cartesian(clip = "off")

pp
cowplot::ggdraw()+
  cowplot::draw_plot(ggplotify::as.ggplot(pp,angle = -45),
            width = 0.5,height = 0.5,
            hjust = -0.2,
            vjust = -0.5)
image.png

然后用推文中的示例数据

image.png

其中一个小图的部分示例数据

读取数据

library(readxl)
#install.packages("rlang")
dat<-read_excel("data/20230327/Figure1d.xlsx")
dat

普通三角热图

library(ggplot2)
ggplot(data=dat,aes(x=x,y=y))+
  geom_tile(aes(fill=log2(value)))+
  coord_equal(clip = "off") -> p

p
image.png

添加文本标签

library(tidyverse)
dat %>% pull(x) %>% unique()
p+
  geom_text(data=data.frame(x=1:8,y=0:7,label=dat %>% pull(x) %>% unique()),
            aes(x=x,y=y,label=label),angle=90)+
  theme_void() -> p1
p1
image.png

在指定的地方添加线段

p1+
  geom_path(data=data.frame(x=c(0.5,0.5,4.5,4.5,3.5,3.5,2.5,2.5,1.5,1.5,0.5),
                            y=c(0.5,4.5,4.5,3.5,3.5,2.5,2.5,1.5,1.5,0.5,0.5)),
            aes(x=x,y=y),
            color="black",size=1)+
  geom_path(data=data.frame(x=c(0.5,0.5,4.5,4.5,3.5,3.5,2.5,2.5,1.5,1.5,0.5)+4,
                            y=c(0.5,4.5,4.5,3.5,3.5,2.5,2.5,1.5,1.5,0.5,0.5)+4),
            aes(x=x,y=y),
            color="black",size=1) -> p2
p2

image.png

更改配色

p2+
  scale_fill_gradient2(low = scales::muted("red"),
                       mid = "white",
                       high = scales::muted("blue"),
                       midpoint = 0)+
  theme(legend.position = "none") -> p3
p3
image.png

添加一些额外的注释

p3+
  annotate(geom = "segment",x=0.4,xend=0.4,y=0.5,yend=4.5,
           size=2,color="red")+
  annotate(geom = "text",x=0.2,y=2.5,label="A",angle=45,size=5)+
  annotate(geom = "segment",x=4.5,xend=8.5,y=8.6,yend=8.6,
           size=2,color="blue")+
  annotate(geom = "text",x=6.5,y=8.8,label="B",angle=45,size=5) -> p4
p4
image.png

最后顺时针旋转45度

library(cowplot)
ggdraw()+
  draw_plot(ggplotify::as.ggplot(p4,angle = -45),
            width = 0.7,height = 0.7,
            hjust = -0.2,
            vjust = -0.1)+
  annotate(geom = "text",x=0.5,y=0.2,label="Chromatin subcompartments") -> p5

p5
image.png

最后再给添加一个图例

p2+
  scale_fill_gradient2(low = scales::muted("red"),
                       mid = "white",
                       high = scales::muted("blue"),
                       midpoint = 0)+
  theme(legend.position = "bottom")+
  guides(fill=guide_colorbar(title.position = "top",
                             title.hjust = 0.5,barwidth = 20)) -> p3.1


p5+
  annotation_custom(grob = ggpubr::get_legend(p3.1),
                    xmin = 0.1,xmax=0.9,
                    ymin=0.1,ymax=0.2)
image.png

一直会有提示信息

image.png

还可以拼图

library(patchwork)

p6+p6
image.png

示例数据和代码可以给推文点赞,然后点击在看,最后留言获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

微信公众号好像又有改动,如果没有将这个公众号设为星标的话,会经常错过公众号的推文,个人建议将 小明的数据分析笔记本 公众号添加星标,添加方法是

点开公众号的页面,右上角有三个点

image.png

点击三个点,会跳出界面

image.png

直接点击 设为星标 就可以了

相关文章

网友评论

      本文标题:跟着Nature学作图:R语言ggplot2三角热图按照指定的角

      本文链接:https://www.haomeiwen.com/subject/fdhrrdtx.html