跟着Nature Communications学作图:R语言ggplot2箱线图和小提琴展示结构变异的长度分布

论文

Chromosome-level assemblies of multiple Arabidopsis genomes reveal hotspots of rearrangements with altered evolutionary dynamics

https://www.nature.com/articles/s41467-020-14779-y

拟南芥NC_panGenome.pdf

分析代码的github主页

https://github.com/schneebergerlab/AMPRIL-genomes

论文中组装了7个拟南芥的基因组,做了一些泛基因组相关的分析,数据和大部分代码都公开了,我们试着复现一下其中的图和一些分析过程,今天的推文复现一下论文中的figure2b箱线图和小提琴图展示结构变异的长度分布

示例数据

图片
image.png

读取数据

library(tidyverse)
dat<-read_delim("D:/R_4_1_0_working_directory/env001/data/20230318/Source_Data.Figure2/Fig2b.txt",
                delim = "t")
dat

最基本的箱线图和小提琴图

library(ggplot2)

ggplot(data = dat,aes(x=`SV-type`,y=length))+
  geom_violin()+
  geom_boxplot()
图片
image.png

这里因为结构变异的长度分布范围非常大,所以出图不太好看,论文里的处理方式是对长度的数值取log10,这样图看起来就好看很多,这个也是一个数据可视化的小技巧

ggplot(data = dat,aes(x=`SV-type`,y=log10(length)))+
  geom_violin()+
  geom_boxplot()
图片
image.png

接下来对整个图进行美化

dat %>% 
  mutate(`SV-type`=factor(`SV-type`,
                          levels = c("INS","DEL","DUP","TL","INV"))) -> dat
ggplot()+
  geom_rect(aes(xmin=-Inf,xmax=2.5,ymin=-Inf,ymax=Inf),
            alpha=0.5)+
  geom_violin(data = dat,
              aes(x=`SV-type`,
                  y=log10(length),
                  color=`SV-type`),
              linewidth=1)+
  geom_boxplot(data = dat,
               aes(x=`SV-type`,
                   y=log10(length),
                   color=`SV-type`),
               width=0.1,outlier.alpha = 0,
               linewidth=1)+
  labs(x=NULL,y="Length of SVs (bp)")+
  scale_y_continuous(breaks = c(2:6),
                     labels = c(expression(10^2),expression(10^3),
                                expression(10^4),expression(10^5),
                                expression(10^6)))+
  theme_bw()+
  theme(legend.position = "none",
        panel.grid = element_blank(),
        panel.background = element_rect(fill="grey"))
图片
image.png

这里遇到一个问题是

geom_rect(aes(xmin=-Inf,xmax=2.5,ymin=-Inf,ymax=Inf),
            alpha=0.5)

添加背景的时候如果添加一层是没有问题的,但是如果再继续叠加一层就会报错,暂时搞不清楚问题出在哪里

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

欢迎大家关注我的公众号

小明的数据分析笔记本

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/156153.html

(0)
联系我们
联系我们
分享本页
返回顶部