ggplot繪圖(氣泡圖)(一)

你永遠(yuǎn)不知道你的小伙伴會(huì)有什么奇奇怪怪的要求,而這些要求能把作為一個(gè)菜鳥的你折騰得半死(/ o \)~~~~
這篇文章就是Poesie無數(shù)次嘔血后的干貨總結(jié),給自己留條后路(誰(shuí)也不知道電腦什么時(shí)候會(huì)崩潰 / (#_#) /))

本次干貨小提綱~~

1.ggplot \color{red}{氣泡圖}的基本繪圖與語(yǔ)法(套用公式)

2.如何快速更換圖中\color{red}{文字字體樣式}

3.ggplot中如何改變\color{red}{主題的字體、顏色、大小、位置(居中等)}

4.如何\color{red}{鎖定}橫軸坐標(biāo)的\color{red}{標(biāo)簽順序}

5.ggplot中如何改變 \color{red}{背景顏色,網(wǎng)格等}

一、數(shù)據(jù)

這個(gè)是我作圖的一個(gè)數(shù)據(jù)表格


我作圖的一個(gè)數(shù)據(jù)表格

二、將數(shù)據(jù)讀入R中,同時(shí)打開繪圖所需的包

library(ggplot2) #主要繪圖包
# install.packages(readxl) 
#我的文件是xlsx格式的,電腦要下載R包readxl ,如果電腦已經(jīng)下載過了,這個(gè)代碼就可以直接忽略~~
# 讀取數(shù)據(jù)
AA=readxl::read_xlsx("GO(3).xlsx",sheet = 1)
# 設(shè)置圖形中的字體格式
windowsFonts(myFont = windowsFont("Times New Roman")) #將字體格式改成Times New Roman格式, 環(huán)境存在myFont 格式
  1. 如果電腦已經(jīng)下載過了R包readxl,代碼install.packages(readxl) 就可直接忽略
  2. 讀取數(shù)據(jù)中sheet = 1是可以去掉的,這個(gè)參數(shù)是如果你一個(gè)excel表格里面好幾個(gè)Sheet,可以通過修改數(shù)字,決定你讀取第幾個(gè)Sheet,一般默認(rèn)讀取第一個(gè)
    \color{red}{例如:AA=readxl::read_xlsx("GO(3).xlsx",sheet = 3)}\
    將sheet = 1改成sheet = 3,就是將讀取sheet = 1改成讀取sheet = 3
  3. 設(shè)置圖形中的字體格式,只要是電腦有的格式,都可以通過修改括號(hào)中的Times New Roman,修改字體格式

三、繪圖

1. 氣泡圖總匯代碼

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) + # 鎖定數(shù)據(jù)框,X軸Y軸數(shù)據(jù)
 geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ #繪制氣泡圖
  coord_flip()+ # 將X軸與Y軸調(diào)換
  scale_color_gradient(low="yellow",high ="red")+ #顏色范圍
  theme(legend.title = element_text(size = 15, face = 2))+ #圖例標(biāo)題大小
  theme(legend.key.size=unit(1,'cm'))+ #圖例標(biāo)題大小
  theme(legend.text = element_text( size = 15,face = 'bold'))+ #圖例圖形里文字大小
  labs(x="Term",y = "RichFactor",title = "GO")+ # X軸、 Y軸 、圖的biao
  geom_line() +
  theme_bw()+
  theme(plot.background = element_rect(fill = "White"))+ #圖片背景顏色設(shè)置
  theme(panel.grid.major=element_line(colour="grey")) +
  scale_fill_gradient(low = "pink", high = "red")+
  theme(axis.title.x = element_text(size = 15))+
  theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
  theme(axis.text.y = element_text(size = 12, family = "myFont", color =c("red","red","red","red","red","red","red","red","red","red","red","red","red","red","red","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue"),face = "bold"))+
  theme(plot.title = element_text(size = 15,face = 4, hjust =0.5))

2. 分步解讀

(1) ggplot繪圖相當(dāng)于層層疊加(加代碼加圖層)
ggplot()
繪圖打底的頁(yè)面,背景層
(2)鎖定數(shù)據(jù)框,X、Y軸

初步鎖定了X軸與Y軸,由于Term的名字太長(zhǎng)了,導(dǎo)致X軸名字密密麻麻擠到了一起,
后面可以通過代碼調(diào)整

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)
(3)繪制氣泡圖
Ⅰ.可見這個(gè)圖十分簡(jiǎn)略,接下來就是通過添加代碼修飾
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)+
geom_point()
Ⅱ.通過修改參數(shù)size(大小)color(顏色)來修改圖形
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)+
geom_point(size=5,color="red",shape =0)
Ⅲ、通過修改參數(shù)shape (形狀)來修改圖形
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
     geom_point(size=5,color="red",shape = 10)

shape=X ,X是數(shù)字1~25,可以任意更改以滿足自己的需求


Ⅳ、參數(shù)size(大小)color(顏色)shape (形狀)進(jìn)階

由于我想將氣泡圖的大小形狀顏色均勻表格中數(shù)據(jù)聯(lián)系在一起,鎖定aes()

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)+
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))

修飾:①size=10*GeneNumber --用點(diǎn)的大小表示GeneNumber 數(shù)值大小,但是由于GeneNumber數(shù)值有點(diǎn)過小,圖形差異不明顯,用10 *,將其擴(kuò)大10倍
②color=-1 * log10(PValue)----給點(diǎn)上顏色,變量鎖定PValue,-1 * log10也是數(shù)學(xué)運(yùn)算

③shape = factor(Group)----根據(jù)Group列來改變其形狀的不同,注意要將分組的哪一列factor()因子化
Ⅴ. 將X軸與Y軸倒過來
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip() #將X軸與Y軸倒過來
Ⅵ.修改變化中的顏色 ---+ scale_color_gradient()
high,low是顏色變化的上下限,通過自己喜好修改顏色
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
 geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip() +#將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")
(4)圖例修改

Ⅰ.大小

  1. legend.title 圖例的標(biāo)題 --參數(shù)size(大?。?face參數(shù)可用于使字體變?yōu)榇煮w或斜體,補(bǔ):可以加參數(shù)color顏色的,用法和上面一樣
    *face取值:plain普通,bold加粗,italic斜體,bold.italic斜體加粗
  2. legend.key.size=unit(1,'cm')) 圖例里面圖形的大小--修改數(shù)字調(diào)整
  3. legend.text--- 圖例里面圖形的文字--參數(shù) size 與 face 應(yīng)用如上
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))

legend.title
legend.key.size
legend.text

Ⅱ.圖例
c9bc03a6a62f45031ca93f9a701fc00.png

位置

通過+ theme(legend.position = "bottom")

“top” “right” “bottom” “l(fā)eft”
“頂部” “右側(cè)” “底部” “左側(cè)”
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+
  theme(legend.position = "left")


Ⅲ. 若想關(guān)閉圖例

  1. 關(guān)閉全部圖例---+theme(legend.position = "none")
  2. 若想關(guān)閉圖例標(biāo)題---+ theme(legend.title = element_blank())
    element_blank()----空白的意思
  3. 關(guān)閉對(duì)color產(chǎn)生的圖例去掉標(biāo)題+guides(color=guide_legend(title=NULL))
    *同理可換成size
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.position = "none")
關(guān)閉全部圖例
(5)標(biāo)題

Ⅰ. + labs()函數(shù)
x--X軸標(biāo)題,y-Y軸標(biāo)題,title --總標(biāo)題
由圖可見我添加了總標(biāo)題GO,將Y軸坐標(biāo)由GeneNumber改成了RichFactor,但由于函數(shù) coord_flip()+ #將X軸與Y軸倒過來,所以RichFactor在橫坐標(biāo)位置

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title = "GO")

標(biāo)題

Ⅱ.添加副標(biāo)題
+ggtitle("GO","副標(biāo)題")

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title ="GO")+
  ggtitle("GO","副標(biāo)題") 

Ⅲ. 對(duì)標(biāo)題大小及位置調(diào)整

函數(shù)/參數(shù) 含義 size family hjust face vjust
plot.title 標(biāo)題
axis.title.x X軸標(biāo)題
axis.title.y Y軸標(biāo)題
axis.text.x X軸坐標(biāo)的標(biāo)題
axis.text.y Y軸坐標(biāo)的標(biāo)題 細(xì)

①參數(shù)size與face,用法與上面一樣
family = "myFont"----字體格式用myFont的格式,myFont在最開始設(shè)定好了windowsFonts(myFont = windowsFont("Times New Roman"))#將字體格式改成Times New Roman格式
hjust=0.5 以為這字體居中--GO標(biāo)題明顯居中
hjust=1
hjust=0
④vjust=一個(gè)數(shù)值,通過數(shù)值調(diào)整高低,可正可負(fù),但是一般沒有需求就不調(diào)整
axis.title

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title = "GO")+
  theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))


axis.title+axis.text

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title = "GO")+
  theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
  theme(axis.text.y = element_text(size = 12, family = "myFont", color ="black", face = "bold"))


補(bǔ)充:vjust

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title ="GO")+
  theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold',vjust=-6))
下調(diào)標(biāo)題
(6)鎖定軸坐標(biāo)順序

細(xì)心的朋友可能會(huì)發(fā)現(xiàn)作圖表格中有時(shí)候Term的順序與圖中的順序?qū)?yīng)不上,ggplot作圖時(shí),會(huì)按照字母排序,所以對(duì)這方面有需求的小伙伴們可以試一下下面的方法鎖定橫軸坐標(biāo)順序

非常抱歉,那個(gè)對(duì)應(yīng)不上的數(shù)據(jù)誤刪了,這張圖就是個(gè)表意

①先將Term整列因子化,再用levels鎖定順序(即表格中的順序)

AA$Term<-levels(factor(AA$Term))

繪圖代碼不變
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title = "GO")+
  theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
  theme(axis.text.y = element_text(size = 12, family = "myFont", color ="black", face = "bold"))

如果還有其他順序的需求可以用代碼
圖形將會(huì)按照c("A","B""C",....)排序顯示出來
c("A","B""C",....)里面長(zhǎng)長(zhǎng)的綠色的都是名字,可按需修改,對(duì)應(yīng)上就好

AA$Term= factor(AA$Term,levels = c( "cytosol" ,                                                      
                                    "nucleus"  ,                                                     
                                    "nucleoplasm" ,                                                  
                                    "perinuclear region of cytoplasm"   ,                            
                                    "extracellular region"   ,                                       
                                    "extracellular space"       ,                                    
                                    "cyclin A2-CDK2 complex"     ,                                   
                                    "membrane raft"              ,                                   
                                    "caveola"                     ,                                  
                                    "endosome"                ,                                      
                                    "proteinaceous extracellular matrix"  ,                                          
                                       ......... (太多了,就補(bǔ)一一列舉了)
                                    "protein homodimerization activity"             ,                
                                    "type II transforming growth factor beta receptor binding" ), ordered=TRUE)
比較簡(jiǎn)單的邏輯
(6)關(guān)于背景和網(wǎng)格線

①去掉面板背景顏色
+ theme_bw()
前面代碼不變

ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
  geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ 
  coord_flip()+ #將X軸與Y軸倒過來
  scale_color_gradient(low="yellow",high ="red")+
  theme(legend.title = element_text(size = 15, face = 2))+
  theme(legend.key.size=unit(1,'cm'))+
  theme(legend.text = element_text( size = 15,face = 'bold'))+ 
  labs(x="Term",y = "RichFactor",title = "GO")+
  theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
  theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
  theme(axis.text.y = element_text(size = 12, family = "myFont", color ="black", face = "bold"))+
  theme_bw()

②改變面板顏色
+theme(panel.background = element_rect(fill = "blue"))*
前面的代碼不變

+theme(panel.background = element_rect(fill = "blue"))

③網(wǎng)格線
網(wǎng)格線有兩種類型:主要網(wǎng)格線指示刻度線,主要網(wǎng)格線之間的次要網(wǎng)格線。

主要網(wǎng)格線 + theme(panel.grid.major=element_line(colour="red"))
前面的代碼不變

+  theme(panel.grid.major=element_line(colour="red"))

紅色

次要網(wǎng)格線 + theme(panel.grid.minor = element_line(color = "green", size = 0.25))
前面的代碼不變

  + theme(panel.grid.minor = element_line(color = "green", size =1))
image.png

③填充旁邊的顏色
+theme(plot.background = element_rect(fill = "pink"))
前面的代碼不變

+theme(plot.background = element_rect(fill = "pink"))
將旁邊變成粉色

④邊距
參數(shù)plot.margin可以處理各種不同的單位(厘米,英寸等)
+ theme(plot.margin = unit(c(1, 2, 1, 5), "cm"))
邊距邊的順序是上,右,下,左
前面的代碼不變

+ theme(plot.margin = unit(c(1, 5, 1, 5), "cm"))
邊距明顯變化

以上是Poesie關(guān)于ggplot繪圖的一小部分總結(jié),不足之處也希望大家指出

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容