溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2022-01-04 09:43:50 來源:億速云 閱讀:3272 作者:小新 欄目:大數(shù)據(jù)

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

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

r語言中g(shù)gpubr包的ggarrange()函數(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)
 

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

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

還有一個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ù)怎么用    

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

參考資料的鏈接是

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

但是我按照這個方法操作我自己的數(shù)據(jù)的時候一直遇到報錯

代碼是

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

報錯內(nèi)容是

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

暫時還沒有搞懂是什么原

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

如果將ggplot2的圖例設(shè)置為頂部,默認結(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ù)怎么用    

如果要改成一行的話再加一個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)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI