成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用

這篇文章主要介紹r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)懷寧,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

當(dāng)使用ggplot2作圖的時(shí)候如果多個(gè)圖拼接到一起,圖例互相之間有一樣的時(shí)候,比如如下的情況(我們用R語言內(nèi)置的鳶尾花的數(shù)據(jù)集做三個(gè)散點(diǎn)圖)

r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用    

實(shí)現(xiàn)上面圖片的代碼是

library(ggplot2)
library(ggpubr)
df<-iris
colnames(df)<-paste0("V",1:5)
p1<-ggplot(df,aes(x=V1,y=V2))+
  geom_point(aes(color=V5))+
  theme_bw()
p2<-ggplot(df,aes(x=V1,y=V3))+
  geom_point(aes(color=V5))+
  theme_bw()
p3<-ggplot(df,aes(x=V1,y=V4))+
  geom_point(aes(color=V5))+
  theme_bw()
ggarrange(p1,p2,p3,ncol = 3)
 

因?yàn)槿齻€(gè)圖的圖例是一樣的,我們完全可以只顯示一個(gè)圖例就夠了。這里拼圖使用的函數(shù)是ggpubr這個(gè)包里的ggarrange()函數(shù),這個(gè)函數(shù)里有一個(gè)參數(shù)是common.legend,默認(rèn)好像是FALSE,我們直接設(shè)置成TRUE就好了,代碼如下

ggarrange(p1,p2,p3,ncol = 3,
          common.legend = T)
 
r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用   

還有一個(gè)legend參數(shù)用來控制圖例的位置

ggarrange(p1,p2,p3,ncol = 3,
          common.legend = T,
          legend = "right")
 
r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用    

還有一種情況是分組過多如何調(diào)整圖例的布局,比如

代碼

df$V6<-sample(LETTERS[1:6],150,replace = T)
p4<-ggplot(df,aes(x=V1,y=V2))+
  geom_point(aes(color=V6))+
  theme_bw()
p5<-ggplot(df,aes(x=V1,y=V3))+
  geom_point(aes(color=V6))+
  theme_bw()
p6<-ggplot(df,aes(x=V1,y=V4))+
  geom_point(aes(color=V6))+
  theme_bw()
ggarrange(p4,p5,p6,ncol = 3,
          common.legend = T)
 

結(jié)果是

r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用    

圖例放到頂部,默認(rèn)的布局是3行3列,如果要改成1行6列呢?ggplot2里應(yīng)該有對(duì)應(yīng)的參數(shù)可以修改吧?但是目前還不知道如何使用ggplot2自帶的函數(shù)來操作,查資料的時(shí)候發(fā)現(xiàn)了一個(gè)R包lemon里有一個(gè)reposition_legend()函數(shù)

參考資料的鏈接是

https://cran.r-project.org/web/packages/lemon/vignettes/legends.html

但是我按照這個(gè)方法操作我自己的數(shù)據(jù)的時(shí)候一直遇到報(bào)錯(cuò)

代碼是

#install.packages("lemon")
library(lemon)
reposition_legend(p4,panel = c('panel-1-5'),position = "top")
 

報(bào)錯(cuò)內(nèi)容是

Error in reposition_legend(p4, panel = c("panel-1-5"), position = "top") : 
  Could not find panel named `panel-1-5`.
 

暫時(shí)還沒有搞懂是什么原

我去查了一下ggplo2里關(guān)于圖例的布局,找到了解決辦法 參考鏈接是 https://ggplot2.tidyverse.org/reference/guide_legend.html

如果將ggplot2的圖例設(shè)置為頂部,默認(rèn)結(jié)果如下

p4<-ggplot(df,aes(x=V1,y=V2))+
  geom_point(aes(color=V6))+
  theme_bw()+
  theme(legend.position = "top")
p4
 
r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用

圖例兩行,順序是從上到下依次排,那如果要改成2行從左到右依次排列呢?

p4<-ggplot(df,aes(x=V1,y=V2))+
  geom_point(aes(color=V6))+
  theme_bw()+
  theme(legend.position = "top")+
  scale_color_discrete(
    guide=guide_legend(byrow = T)
  )
p4
 
r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用    

如果要改成一行的話再加一個(gè)nrow參數(shù)就好了

p4<-ggplot(df,aes(x=V1,y=V2))+
  geom_point(aes(color=V6))+
  theme_bw()+
  theme(legend.position = "top")+
  scale_color_discrete(
    guide=guide_legend(byrow = T,nrow = 1)
  )
p4
 
r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用

以上是“r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前文章:r語言中g(shù)gpubr包的ggarrange()函數(shù)怎么用
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article8/pdcsip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、做網(wǎng)站全網(wǎng)營銷推廣、網(wǎng)站導(dǎo)航、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
莒南县| 惠安县| 巴塘县| 抚顺县| 玉山县| 桑植县| 平昌县| 黄石市| 桑植县| 永丰县| 柳河县| 库尔勒市| 大英县| 永吉县| 蒙自县| 驻马店市| 阿克| 新兴县| 萝北县| 永州市| 涟水县| 泰和县| 清新县| 天柱县| 山阳县| 体育| 泸溪县| 保山市| 崇州市| 西和县| 横山县| 海淀区| 巢湖市| 闽清县| 康保县| 高碑店市| 阿鲁科尔沁旗| 千阳县| 长沙市| 垣曲县| 临洮县|