這篇文章將為大家詳細(xì)講解有關(guān)如何用R語(yǔ)言ggforce包畫(huà)餅狀圖,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、達(dá)坂城網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
最近在看論文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication 今天的筆記記錄的是論文中Figure2圖b中的餅圖的畫(huà)法
ggplot2畫(huà)拼圖我個(gè)人認(rèn)為相對(duì)是比較麻煩的,而且要實(shí)現(xiàn)上圖這種各個(gè)部分能夠分開(kāi)的好像不太好實(shí)現(xiàn)。之前找餅狀圖的資料的時(shí)候發(fā)現(xiàn)了
ggforce
這個(gè)包,用他來(lái)做餅圖相對(duì)方便很多,今天的內(nèi)容主要用這個(gè)包里的函數(shù)來(lái)實(shí)現(xiàn)
數(shù)據(jù)只需要兩列就可以了
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)),
)
關(guān)鍵還可以把中間給弄成空心的
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)),
)
做餅狀圖用到的函數(shù)是
geom_arc_bar()
,這個(gè)函數(shù)里參數(shù)的作用之前錄制過(guò)視頻大家可以參考一下
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)
這樣是沒(méi)有圖例的,我想到的辦法是重新做一個(gè)圖,拼接到右下角作為圖例
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)
最終的效果如下
關(guān)于如何用R語(yǔ)言ggforce包畫(huà)餅狀圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。