R語言ggplot2設(shè)置圖例(legend)的操作大全_第1頁
R語言ggplot2設(shè)置圖例(legend)的操作大全_第2頁
R語言ggplot2設(shè)置圖例(legend)的操作大全_第3頁
R語言ggplot2設(shè)置圖例(legend)的操作大全_第4頁
R語言ggplot2設(shè)置圖例(legend)的操作大全_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第R語言ggplot2設(shè)置圖例(legend)的操作大全目錄基本箱線圖(帶有圖例)移除圖例修改圖例的內(nèi)容顛倒圖例的順序隱藏圖例標(biāo)題修改圖例中的標(biāo)簽修改data.frame的factor修改標(biāo)題和標(biāo)簽的顯示修改圖例的框架設(shè)置圖例的位置隱藏斜線總結(jié)本文在/Graphs/Scatterplots_(ggplot2)/的基礎(chǔ)上加入了自己的理解

圖例用來解釋圖中的各種含義,比如顏色,形狀,大小等等,在ggplot2中aes中的參數(shù)(x,y除外)基本都會(huì)生成圖例來解釋圖形,比如fill,colour,linetype,shape.

基本箱線圖(帶有圖例)

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ù)不同的分類,可以使用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è)置圖例名稱

bp+scale_fill_discrete(name="Experimental\nCondition")

#設(shè)置圖例的名稱,重新定義新的標(biāo)簽名稱

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的分類colour:點(diǎn)線或者其他圖形的框線顏色fill:填充顏色linetype:線型,實(shí)線虛線點(diǎ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)來設(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))

隱藏斜線

#Nooutline

ggplot(data=PlantGrowth,aes(x=group,fill=group))+

geom_bar()

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論