版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第R語(yǔ)言ggplot2設(shè)置圖例(legend)的操作大全目錄基本箱線(xiàn)圖(帶有圖例)移除圖例修改圖例的內(nèi)容顛倒圖例的順序隱藏圖例標(biāo)題修改圖例中的標(biāo)簽修改data.frame的factor修改標(biāo)題和標(biāo)簽的顯示修改圖例的框架設(shè)置圖例的位置隱藏斜線(xiàn)總結(jié)本文在/Graphs/Scatterplots_(ggplot2)/的基礎(chǔ)上加入了自己的理解
圖例用來(lái)解釋圖中的各種含義,比如顏色,形狀,大小等等,在ggplot2中aes中的參數(shù)(x,y除外)基本都會(huì)生成圖例來(lái)解釋圖形,比如fill,colour,linetype,shape.
基本箱線(xiàn)圖(帶有圖例)
library(ggplot2)
bp-ggplot(data=PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
bp
移除圖例
Useguides(fill=FALSE),replacingfillwiththedesiredaesthetic.使用guides(fill=FALSE)移除由ase中匹配的fill生成的圖例,也可以使用themeYoucanalsoremoveallthelegendsinagraph,usingtheme.
bp+guides(fill=FALSE)
#也可以這也
bp+scale_fill_discrete(guide=FALSE)
#移除所有圖例
bp+theme(legend.position="none")
修改圖例的內(nèi)容
改變圖例的順序?yàn)閠rt1,ctrl,trt2:
bp+scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))
根據(jù)不同的分類(lèi),可以使用scale_fill_manual,scale_colour_hue,scale_colour_manual,scale_shape_discrete,scale_linetype_discrete等等.
顛倒圖例的順序
#多種方法
bp+guides(fill=guide_legend(reverse=TRUE))
#也可以
bp+scale_fill_discrete(guide=guide_legend(reverse=TRUE))
#還可以這也
bp+scale_fill_discrete(breaks=rev(levels(PlantGrowth$group)))
隱藏圖例標(biāo)題
#Removetitleforfilllegend
bp+guides(fill=guide_legend(title=NULL))
#Removetitleforalllegends
bp+theme(legend.title=element_blank())
修改圖例中的標(biāo)簽
兩種方法一種是直接修改標(biāo)簽,另一種是修改data.frame
Usingscales
圖例可以根據(jù)fill,colour,linetype,shape等繪制,我們以fill為例,scale_fill_xxx,xxx表示處理數(shù)據(jù)的一種方法,可以是hue(對(duì)顏色的定量操作),continuous(連續(xù)型數(shù)據(jù)處理),discete(離散型數(shù)據(jù)處理)等等.
#設(shè)置圖例名稱(chēng)
bp+scale_fill_discrete(name="Experimental\nCondition")
#設(shè)置圖例的名稱(chēng),重新定義新的標(biāo)簽名稱(chēng)
bp+scale_fill_discrete(name="Experimental\nCondition",
breaks=c("ctrl","trt1","trt2"),
labels=c("Control","Treatment1","Treatment2"))
#自定義fill的顏色
bp+scale_fill_manual(values=c("#999999","#E69F00","#56B4E9"),
name="Experimental\nCondition",
breaks=c("ctrl","trt1","trt2"),
labels=c("Control","Treatment1","Treatment2"))
注意這里并不能修改x軸的標(biāo)簽,如果需要改變x軸的標(biāo)簽,可以參照/tanzuozhev/article/details/51107583
#Adifferentdataset
df1-data.frame(
sex=factor(c("Female","Female","Male","Male")),
time=factor(c("Lunch","Dinner","Lunch","Dinner"),levels=c("Lunch","Dinner")),
total_bill=c(13.53,16.81,16.24,17.42)
#Abasicgraph
lp-ggplot(data=df1,aes(x=time,y=total_bill,group=sex,shape=sex))+geom_line()+geom_point()
lp
#修改圖例
lp+scale_shape_discrete(name="Payer",
breaks=c("Female","Male"),
labels=c("Woman","Man"))
Ifyouusebothcolourandshape,theybothneedtobegivenscalespecifications.Otherwisetherewillbetwotwoseparatelegends.如果同時(shí)使用color和shape,那么必須都進(jìn)行scale_xx_xxx的定義,否則color和shape的圖例就會(huì)合并到一起,如果scale_xx_xxx中的name相同,那么他們也會(huì)合并到一起.
#Specifycolourandshape
lp1-ggplot(data=df1,aes(x=time,y=total_bill,group=sex,shape=sex,colour=sex))+geom_line()+geom_point()
lp1
#Here'swhathappensifyoujustspecifycolour
lp1+scale_colour_discrete(name="Payer",
breaks=c("Female","Male"),
labels=c("Woman","Man"))
#Specifybothcolourandshape
lp1+scale_colour_discrete(name="Payer",
breaks=c("Female","Male"),
labels=c("Woman","Man"))+
scale_shape_discrete(name="Payer",
breaks=c("Female","Male"),
labels=c("Woman","Man"))
scale_xxx_yyy:
xxx的分類(lèi)colour:點(diǎn)線(xiàn)或者其他圖形的框線(xiàn)顏色fill:填充顏色linetype:線(xiàn)型,實(shí)線(xiàn)虛線(xiàn)點(diǎn)線(xiàn)shape:點(diǎn)的性狀,超級(jí)多,可以自己搜索一下size:點(diǎn)的大小alpha:透明度
yyy的分離hue:設(shè)置色調(diào)范圍(h)、飽和度(c)和亮度(l)獲取顏色manual:手動(dòng)設(shè)置gradient:顏色梯度grey:設(shè)置灰度值discrete:離散數(shù)據(jù)(e.g.,colors,pointshapes,linetypes,pointsizes)continuous連續(xù)行數(shù)據(jù)(e.g.,alpha,colors,pointsizes)
修改data.frame的factor
pg-PlantGrowth#Copydataintonewdataframe
#Renamethecolumnandthevaluesinthefactor
levels(pg$group)[levels(pg$group)=="ctrl"]-"Control"
levels(pg$group)[levels(pg$group)=="trt1"]-"Treatment1"
levels(pg$group)[levels(pg$group)=="trt2"]-"Treatment2"
names(pg)[names(pg)=="group"]-"ExperimentalCondition"
#Viewafewrowsfromtheendproduct
head(pg)
##weightExperimentalCondition
##14.17Control
##25.58Control
##35.18Control
##46.11Control
##54.50Control
##64.61Control
#Maketheplot
ggplot(data=pg,aes(x=`ExperimentalCondition`,y=weight,fill=`ExperimentalCondition`))+
geom_boxplot()
修改標(biāo)題和標(biāo)簽的顯示
#標(biāo)題
bp+theme(legend.title=element_text(colour="blue",size=16,face="bold"))
#標(biāo)簽
bp+theme(legend.text=element_text(colour="blue",size=16,face="bold"))
修改圖例的框架
bp+theme(legend.background=element_rect())
bp+theme(legend.background=element_rect(fill="gray90",size=.5,linetype="dotted"))
設(shè)置圖例的位置
圖例的位置(left/right/top/bottom):
bp+theme(legend.position="top")
也可以根據(jù)坐標(biāo)來(lái)設(shè)置圖例的位置,左下角為(0,0),右上角為(1,1)
#Positionlegendingraph,wherex,yis0,0(bottomleft)to1,1(topright)
bp+theme(legend.position=c(.5,.5))
#Setthe"anchoringpoint"ofthelegend(bottom-leftis0,0;top-rightis1,1)
#Putbottom-leftcorneroflegendboxinbottom-leftcornerofgraph
bp+theme(legend.justification=c(0,0),#這個(gè)參數(shù)設(shè)置很關(guān)鍵
legend.position=c(0,0))
#Putbottom-rightcorneroflegendboxinbottom-rightcornerofgraph
bp+theme(legend.justification=c(1,0),legend.position=c(1,0))
隱藏斜線(xiàn)
#Nooutline
ggplot(data=PlantGrowth,aes(x=group,fill=group))+
geom_bar()
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 生產(chǎn)調(diào)度員倒班制度規(guī)定
- 生產(chǎn)車(chē)間安全標(biāo)準(zhǔn)化制度
- 化工生產(chǎn)車(chē)間衛(wèi)生制度
- 定點(diǎn)屠宰場(chǎng)安全生產(chǎn)制度
- 服務(wù)窗口安全生產(chǎn)制度
- 潔凈生產(chǎn)區(qū)衛(wèi)生管理制度
- 2026用戶(hù)交互(UX)設(shè)計(jì)師招聘面試題及答案
- 2026年金融投資知識(shí)題庫(kù)解析
- 2026版物流管理與供應(yīng)鏈優(yōu)化題
- 2026年計(jì)算機(jī)編程語(yǔ)言編程模擬題
- 培訓(xùn)學(xué)校老師入股協(xié)議書(shū)
- 2025廣西百礦超元發(fā)電有限公司社會(huì)招聘81人筆試參考題庫(kù)附答案解析
- 2025年國(guó)防科工局機(jī)關(guān)公開(kāi)遴選公務(wù)員筆試模擬題及答案
- 2025職業(yè)健康培訓(xùn)測(cè)試題(+答案)
- 供貨流程管控方案
- 章節(jié)復(fù)習(xí):平行四邊形(5個(gè)知識(shí)點(diǎn)+12大??碱}型)解析版-2024-2025學(xué)年八年級(jí)數(shù)學(xué)下冊(cè)(北師大版)
- 中試基地運(yùn)營(yíng)管理制度
- 老年病康復(fù)訓(xùn)練治療講課件
- 2024中考會(huì)考模擬地理(福建)(含答案或解析)
- CJ/T 164-2014節(jié)水型生活用水器具
- 購(gòu)銷(xiāo)合同范本(塘渣)8篇
評(píng)論
0/150
提交評(píng)論