论文
Graph pangenome captures missing heritability and empowers tomato breeding
https://www.nature.com/articles/s41586-022-04808-9#MOESM8
没有找到论文里的作图的代码,但是找到了部门组图数据,我们可以用论文中提供的原始数据模仿出论文中的图
本日的推文重复一下论文中的Figure2a
重要知识点
- 怎样在山脊图上添加辅助线
- 别的一个知识点是怎样把图例放到整个图的左下角
部门示例数据截图
读取数据
library(readxl)dat.fig2a<-read_excel("data/20220711/41586_2022_4808_MOESM6_ESM.xlsx", sheet = "Fig2a", skip = 1)数据转换为长格式
library(tidyverse)library(stringr)reshape2::melt(dat.fig2a) %>% select(variable,value) %>% mutate(new_col01 = str_split_fixed(variable,'_',2)[,1], new_col02 = str_split_fixed(variable,'_',2)[,2]) -> new.df这里另有一个知识点是 指定分隔符拆分字符串函数 str_split_fixed()可以指定拆分成多少个
赋予因子水平
new.df$new_col02<-factor(new.df$new_col02, levels = c("snps","indels","svs", "snps_indels","snps_indels_svs"))根本作图代码
ggplot(data=new.df,aes(x=value,y=new_col02))+ geom_density_ridges(aes(fill=new_col01,color=new_col01), alpha=0.4, bandwidth=0.04, quantile_lines=TRUE, quantile_fun=function(x,...)mean(x), #linetype="dashed", scale=1, vline_linetype="dashed")+ scale_fill_manual(values = c("graph"="#ca612d", "linear"="#2772a7"))+ scale_color_manual(values = c("graph"="#ca612d", "linear"="#2772a7"))+ theme_classic() + guides(fill="none",color="none") -> p1p1这里有一个标题是辅助线的位置是在匀称值,这里通过一个求匀称值的函数实现,假如是任意数值应该怎么做暂时想不到方法
添加文本
new.df %>% group_by(new_col01,new_col02) %>% summarise(mean_value=mean(value)) %>% ungroup() %>% mutate(new_col02 = fct_relevel(new_col02, c("snps","indels","svs", "snps_indels","snps_indels_svs"))) %>% mutate(new_col03=as.numeric(new_col02)) -> new.df01p1+ scale_y_discrete(labels=c("SNP","Indel","SV","SNP + Indel","SNP + Indel + SV")) geom_text(data=new.df01 %>% filter(new_col01=="graph"), aes(y=new_col03+0.1,x=mean_value, label=round(mean_value,2)), hjust=-0.5,color="#ca612d")+ geom_text(data=new.df01 %>% filter(new_col01=="linear"), aes(y=new_col03+0.1,x=mean_value, label=round(mean_value,2)), hjust=1.5,color="#2772a7")绘制图例
ggplot(data=new.df,aes(x=value,y=new_col02))+ geom_density_ridges(aes(fill=new_col01,color=new_col01), alpha=0.4)+ scale_fill_manual(values = c("graph"="#ca612d", "linear"="#2772a7"), name="", label=c("TGG1.1-332","SL5.0-332"))+ guides(color="none")-> p2library(ggpubr)as_ggplot(get_legend(p2)) -> p3将图例和图组合到一起
library(latex2exp)pdf(file = "Rplot13.pdf", width=9.4,height = 4)p1+ scale_y_discrete(labels=c("SNP","Indel","SV","SNP + Indel", "SNP + Indel + SV"))+ geom_text(data=new.df01 %>% filter(new_col01=="graph"), aes(y=new_col03+0.1,x=mean_value, label=round(mean_value,2)), hjust=-0.5,color="#ca612d")+ geom_text(data=new.df01 %>% filter(new_col01=="linear"), aes(y=new_col03+0.1,x=mean_value, label=round(mean_value,2)), hjust=1.5,color="#2772a7")+ labs(x=TeX(r"(\textit{h}$^2$)"),y="")+ annotation_custom(grob=ggplotGrob(p3), xmin=-0.35,xmax = -0.35,ymin = 1,ymax = 1)+ coord_cartesian(clip="on")+ theme(plot.margin = unit(c(0.1,0.1,0.1,1),'cm'))+ annotate(geom = "text", x=0.8,y=1.5, label=TeX(r"(\textit{P} = 1.70 \times 10$^{-217}$)"), vjust=-0.5)dev.off()
示例数据和代码可以本身到论文中获取,大概给本篇推文点赞,点击在看,然后留言获取
接待各人关注我的公众号
小明的数据分析条记本
小明的数据分析条记本 公众号 重要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相干转录组学、基因组学、群体遗传学文献阅读条记;3、生物信息学入门学习资料及本身的学习条记!
|