论文
A high-quality genome compendium of the human gut microbiome of Inner Mongolians
论文中大部分作图数据都有,争取把论文中的图都复现一下
今天的推文我们试着复现一下论文中的Figure2c
上半部分的部分示例数据截图
读取数据
library(readxl)
fig2c1<-read_excel("data/20230305/41564_2022_1270_MOESM5_ESM.xlsx",
sheet = "Fig2c top")
head(fig2c1)
Wilcoxon秩和检验
wilcox.test(N_contigs~DB,data=fig2c1)
wilcox.test(`N50 length`~DB,data=fig2c1)
作图代码
fig2c1 %>%
mutate(DB=factor(DB,levels = c("UHGG","IMGG"))) -> fig2c1
ggplot(data=fig2c1,aes(x=DB,y=N_contigs))+
geom_boxplot(aes(fill=DB),show.legend = FALSE,
width=0.4)+
scale_fill_manual(values = c("#d19999","#b5cde1"))+
theme_bw()+
theme(panel.grid = element_blank())+
labs(x=NULL,y="Number of contigs")+
annotate(geom = "text",x=1.5,y=600,label=expression(paste(italic(P)," < 2",''%*%'',"10",''^-'')^16),parse=T) -> p1
ggplot(data=fig2c1,aes(x=DB,y=`N50 length`/1000000))+
geom_boxplot(aes(fill=DB),show.legend = FALSE,
width=0.4)+
scale_fill_manual(values = c("#d19999","#b5cde1"))+
theme_bw()+
theme(panel.grid = element_blank())+
labs(x=NULL,y="N50 length (Mbp")+
annotate(geom = "text",x=1.5,y=6,label=expression(paste(italic(P)," < 2",''%*%'',"10",''^-'')^16),parse=T) -> p2
library(patchwork)
p1+p2+
plot_layout(ncol = 2)+
plot_annotation(title = "All genomes",
theme = theme(plot.title = element_text(hjust=0.5,size=30)))
这里怎么给标题再添加一个灰色背景,暂时想不到办法了
这里出的图和论文中的对不上,暂时不知道问题出在哪里了
这里新学到的知识点,使用annotate()函数搭配expression的写法
如果是普通的文本需要用双引号引起来,需要expression函数解析的文本用两个单引号引起来
paste内部上标用加减号是可以的,但是用数字就不行,暂时不知道为啥
比如
ggplot(data=fig2c1,aes(x=DB,y=`N50 length`/1000000))+
geom_boxplot(aes(fill=DB),show.legend = FALSE,
width=0.4)+
scale_fill_manual(values = c("#d19999","#b5cde1"))+
theme_bw()+
theme(panel.grid = element_blank())+
labs(x=NULL,y="N50 length (Mbp")+
annotate(geom = "text",x=1.5,y=6,label=expression(paste(italic(P)," < 2",''%*%'',"10",''^-+'')),parse=T)
这个代码出结果
ggplot(data=fig2c1,aes(x=DB,y=`N50 length`/1000000))+
geom_boxplot(aes(fill=DB),show.legend = FALSE,
width=0.4)+
scale_fill_manual(values = c("#d19999","#b5cde1"))+
theme_bw()+
theme(panel.grid = element_blank())+
labs(x=NULL,y="N50 length (Mbp")+
annotate(geom = "text",x=1.5,y=6,label=expression(paste(italic(P)," < 2",''%*%'',"10",''^-1'')),parse=T)
这个代码就报错
需要把数字写到paste()函数的外面
ggplot(data=fig2c1,aes(x=DB,y=`N50 length`/1000000))+
geom_boxplot(aes(fill=DB),show.legend = FALSE,
width=0.4)+
scale_fill_manual(values = c("#d19999","#b5cde1"))+
theme_bw()+
theme(panel.grid = element_blank())+
labs(x=NULL,y="N50 length (Mbp")+
annotate(geom = "text",x=1.5,y=6,
label=expression(paste(italic(P)," < 2",''%*%'',"10",''^-'')^123456),parse=T)
那如果我还想继续往后写文本,暂时不知道怎么搞了
突然想到了,原来可以用波浪线来连接
ggplot(data=fig2c1,aes(x=DB,y=`N50 length`/1000000))+
geom_boxplot(aes(fill=DB),show.legend = FALSE,
width=0.4)+
scale_fill_manual(values = c("#d19999","#b5cde1"))+
theme_bw()+
theme(panel.grid = element_blank())+
labs(x=NULL,y="N50 length (Mbp")+
annotate(geom = "text",x=1.5,y=6,
label=expression(paste(italic(P)," < 2",''%*%'',"10",''^-'')^123456~"AAA"[123]),parse=T)
推文记录的是自己的学习笔记,内容可能会存在错误,请大家批判着看,欢迎大家指出其中的错误
示例数据和代码可以给推文打赏1元获取
声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/156073.html