今天要模仿的图片来自于论文 Core gut microbial communities are maintained by beneficial interactions and strain variability in fish。期刊是 Nature microbiology
今天重复的图片是Figure4中的小a,在一幅图的右上角放一幅图
之前的推文介绍过相关的内容(点击下方蓝字直达图文)
ggplot2:在一幅图中插入另外一幅图
首先是频率分布直方图
第一步是准备数据
频率分布直方图之前的推文有过详细的介绍,点击下方蓝字直达,这里的代码就不再过多介绍
R语言ggplot2包绘制频率分布直方图的简单小例子
代码语言:javascript复制library(ggplot2)
df1<-read.csv("histogram_1.csv",header=T)
ggplot(df1,aes(A))
geom_histogram(binwidth = 0.1,
color="white",fill="#4c72b0")
theme(panel.background = element_blank(),
axis.line = element_line())
labs(x="Average number of strains within a pool",
y="Number of pools of 8 microbes")
ylim(0,14)
接下来是箱线图
准备数据
代码语言:javascript复制df2<-read.csv("boxplot_1.csv",header=T)
df3<-reshape2::melt(df2)
head(df3)
df3$variable<-factor(df3$variable,
levels = c("Non_Core","Core"),
labels = c("Non-core","Core"))
ggplot(df3,aes(x=variable,y=value,group=variable))
geom_boxplot(size=1,outlier.colour = "white")
geom_dotplot(binaxis='y', stackdir='center',
binwidth = 0.1,
aes(fill=variable))
theme_bw()
theme(panel.grid = element_blank(),
plot.title = element_text(hjust=0.5),
legend.position = "none")
scale_fill_manual(values = c("#4c72b0","#dd8452"))
labs(x=NULL,y="Number of strains",
title = "Strain diversity for OTUsnwith >3,000 reads")
annotate("text",x=0.6,y=8,label="P=0.016")
最后是将箱线图放到直方图的右上角
代码语言:javascript复制g1<-ggplotGrob(p2)
p1 annotation_custom(g1,xmin=6,xmax = 8,ymin = 8,ymax=15)