如何用R语言ggforce包画饼状图

发布时间:2021-11-22 15:28:32 作者:柒染
来源:亿速云 阅读:335

这篇文章将为大家详细讲解有关如何用R语言ggforce包画饼状图,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

如何用R语言ggforce包画饼状图  
image.png

最近在看论文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication 今天的笔记记录的是论文中Figure2图b中的饼图的画法

如何用R语言ggforce包画饼状图  
image.png

ggplot2画拼图我个人认为相对是比较麻烦的,而且要实现上图这种各个部分能够分开的好像不太好实现。之前找饼状图的资料的时候发现了ggforce这个包,用他来做饼图相对方便很多,今天的内容主要用这个包里的函数来实现

 第一步还是准备数据

数据只需要两列就可以了如何用R语言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)),
               )
 
如何用R语言ggforce包画饼状图  
image.png

关键还可以把中间给弄成空心的

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)),
               )
 
如何用R语言ggforce包画饼状图  
image.png

做饼状图用到的函数是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)
 
如何用R语言ggforce包画饼状图  
image.png

这样是没有图例的,我想到的办法是重新做一个图,拼接到右下角作为图例

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)
 

最终的效果如下如何用R语言ggforce包画饼状图


关于如何用R语言ggforce包画饼状图就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 使用R语言怎么绘制一个饼状图
  2. 如何用Div画条龙

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

r语言 ggforce

上一篇:C语言面试题有哪些

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》