最近在看论文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication
今天的笔记记录的是论文中Figure2图b中的饼图的画法
ggplot2画拼图我个人认为相对是比较麻烦的,而且要实现上图这种各个部分能够分开的好像不太好实现。之前找饼状图的资料的时候发现了
ggforce
这个包,用他来做饼图相对方便很多,今天的内容主要用这个包里的函数来实现
第一步还是准备数据
数据只需要两列就可以了
第一次用ggforce这个包还是需要先安装
install.packages("ggforce")
最基本的饼图代码如下
df1<-read.csv("pieplot/AA.csv",header = T)
df1
ggplot()+
geom_arc_bar(data=df1,
stat = "pie",
aes(x0=0,y0=0,r0=0,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)),
)
关键还可以把中间给弄成空心的
ggplot()+
geom_arc_bar(data=df1,
stat = "pie",
aes(x0=0,y0=0,r0=1,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)),
)
做饼状图用到的函数是
geom_arc_bar()
,这个函数里参数的作用之前录制过视频大家可以参考一下
接下来是简单的美化,最后拼图就好了
library(ggforce)
df1<-read.csv("pieplot/AA.csv",header = T)
df1
df2<-edit(df1)
p1<-ggplot()+
geom_arc_bar(data=df1,
stat = "pie",
aes(x0=0,y0=0,r0=0,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)),
)+
scale_fill_manual(values = c("#80c97f","#a68dc8",
"#ffc000","#c00000"))+
annotate("text",x=0,y=-2.2,label="32.72%")+
annotate("text",x=-1.6,y=1.5,label="36.76%",angle=50)+
annotate("text",x=1.6,y=1.5,label="30.52%",angle=-50)+
theme_void()+
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5,face="italic"))+
labs(title = "GDDH13")
p2<-ggplot()+
geom_arc_bar(data=df1,
stat = "pie",
aes(x0=0,y0=0,r0=0,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)),
)+
scale_fill_manual(values = c("#80c97f","#a68dc8",
"#ffc000","#c00000"))+
annotate("text",x=0,y=-2.2,label="32.72%")+
annotate("text",x=-1.6,y=1.5,label="36.76%",angle=50)+
annotate("text",x=1.6,y=1.5,label="30.52%",angle=-50)+
theme_void()+
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5,face="italic"))+
labs(title = "GDDH13")
p3<-ggplot()+
geom_arc_bar(data=df2,
stat = "pie",
aes(x0=0,y0=0,r0=0,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0.05)),
)+
scale_fill_manual(values = c("#80c97f","#a68dc8",
"#ffc000","#c00000"))+
annotate("text",x=0.5,y=-2.2,label="32.72%")+
annotate("text",x=-1.6,y=1.5,label="16.76%",angle=50)+
annotate("text",x=-2.2,y=0.5,label="30%",angle=80)+
annotate("text",x=1.6,y=1.5,label="30.52%",angle=-50)+
theme_void()+
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5,face="italic"))+
labs(title = "GDDH13")
p3
library(cowplot)
plot_grid(p1,p2,p3,ncol = 2,nrow=2)
这样是没有图例的,我想到的办法是重新做一个图,拼接到右下角作为图例
df4<-data.frame(x=1,y=c(1,2,3,4),label=c("A","B","C","D"))
p4<-ggplot(df4,aes(x=x,y=y))+
geom_text(aes(x=x+0.2,y=y,label=label))+
geom_point(aes(color=label),show.legend = F,size=4)+
theme_void()+
ylim(-8,8)+xlim(-1,3)+
scale_color_manual(values = c("#80c97f","#a68dc8",
"#ffc000","#c00000"))
plot_grid(p1,p2,p3,p4,ncol = 2,nrow=2)
最终的效果如下
欢迎大家关注我的公众号
小明的数据分析笔记本