在使用ggplot2初步繪制(ggplot2|詳解八大基本繪圖要素)出需要展示的圖形后,還需要對(duì)標(biāo)題,坐標(biāo)軸(ggplot2|theme主題設(shè)置,詳解繪圖優(yōu)化-“精雕細(xì)琢”)和legend(ggplot2 |legend參數(shù)設(shè)置,圖形精雕細(xì)琢)上的對(duì)象進(jìn)行一系列的設(shè)置,包括但不限于名稱(chēng)更改,顏色,大小,位置和角度的調(diào)整。 本文針對(duì)性的介紹下如何對(duì)標(biāo)題,坐標(biāo)軸和legend進(jìn)行修改和設(shè)置,算是之前幾篇推文的一些補(bǔ)充。 一 載入R包 數(shù)據(jù) 為方便展示,使用ggplot2內(nèi)置的iris數(shù)據(jù)集 library(ggplot2) p <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(color=Species)) p 本文會(huì)分別介紹圖中紅色標(biāo)記部分的修改和設(shè)置方法。 二 標(biāo)題設(shè)置 可以通過(guò)labs函數(shù)添加圖片標(biāo)題 以及 subtitle ,caption ;而后通過(guò)theme來(lái)設(shè)置字體的大小,顏色,位置和角度等。 注意theme中對(duì)應(yīng)的更改plot.title ,plot.caption 和 plot.subtitle p1 <- p + labs(title="學(xué)習(xí)ggplot2可視化", subtitle = "熟能生巧", caption = "生信補(bǔ)給站") + theme(plot.title=element_text(face="bold.italic", #字體 color="steelblue", #顏色 size=24, #大小 hjust=0.5, #位置 vjust=0.5, angle=360), # 角度 plot.caption=element_text(face="bold",color="blue",size=20)) p1 三 坐標(biāo)軸設(shè)置 3.1 設(shè)置坐標(biāo)軸 使用labs函數(shù) ,其中x y 即為對(duì)應(yīng)的坐標(biāo)名字; p2 <- p1 + labs(x="X軸",y = "這是Y軸",title = "生信補(bǔ)給站") p2 3.2 設(shè)置坐標(biāo)大小,顏色根據(jù)實(shí)際情況設(shè)置大小,顏色和傾斜角度可以更清晰的展示結(jié)果 p2 + theme(axis.title.x=element_text(vjust=1, size=20), # X axis title axis.title.y=element_text(size=10, color = "blue"), # Y axis title axis.text.x=element_text(size=10, angle = 45, color = "red", vjust=.5), # X axis text axis.text.y=element_text(size=10)) # Y axis text 四 圖例設(shè)置 legend 可以使用guide函數(shù)或者scale函數(shù)進(jìn)行修改,這里分別進(jìn)行一下介紹。 4.1 根據(jù)guide修改p3 <- p2 + guides(color=guide_legend(title = "shape change Legend")) p3 注意這里使用color=guide_legend ,和aes對(duì)應(yīng) 。 ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(shape=Species))+ guides(shape=guide_legend(title = "shape change legend")) 4.2 根據(jù)scale修改p4 <- p2 + scale_colour_discrete(name="scale change Legend") p4 #綜合一下 ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(color=Species,shape=Species)) + scale_colour_discrete(name="color legend") + guides(shape=guide_legend(title = "shape legend")) 4.3 更改標(biāo)簽名稱(chēng)4.3.1 使用scale 函數(shù)對(duì)標(biāo)簽名稱(chēng)進(jìn)行更改 p5 <- p2 + scale_color_discrete(name="scale change \n Legend", breaks=c("setosa" ,"versicolor","virginica"), labels=c("Species 1", "Species 2", "Species 3") ) p5 4.3.2 關(guān)于legend的其他設(shè)置 p5 + theme(legend.title = element_text(size=15, color = "firebrick"), legend.text = element_text(size=10), legend.key=element_rect(fill='blue')) 關(guān)于scale函數(shù)的一個(gè)簡(jiǎn)單的“總結(jié)”,ggplot2|詳解八大基本繪圖要素也有簡(jiǎn)單的介紹。ggplot2的scale系列函數(shù)有很多,命名和用法是有一定規(guī)律的。一般使用三個(gè)單詞用_連接 ,scale_xxx_yyy形式: 其中第二部分的xxx可選為: colour: 點(diǎn) 線 或者其他圖形的框線顏色 fill: 填充顏色 (注意個(gè)colour區(qū)分) linetype :線型, 實(shí)線 虛線 點(diǎn)線 shape ,size ,alpha : 分別為形狀, 大小 和 透明度(某些場(chǎng)景有妙用) 其中第三部分的 yyy 可選為: manual: 手動(dòng)設(shè)置 discrete: 離散數(shù)據(jù) continuous :連續(xù)數(shù)據(jù) gradient: 顏色梯度 grey: 設(shè)置灰度值 更多請(qǐng)參考: https://ggplot2./ https:/// |
|
來(lái)自: 生信補(bǔ)給站 > 《待分類(lèi)》