ggplot2繪圖系統——圖例:guide函數、標度函數、overrides.aes參數
圖例調整函數guide_legend
也屬於標度函數,但不能單獨作為對象使用,即不能如p+guide_legend()
使用。
1. guides及guides_legend函數
guide_legend函數參數:
guide_legend(title = , #圖例標題
title.position = ,#top/bottom/right/left
title.theme = , #圖例風格
title.hjust = , #標題水平調整
title.vjust = ,
label = TRUE, #是否顯示標簽
label.position = , #標簽位置,同上title
label.theme = ,
label.hjust = ,
label.vjust = ,
keywidth = , #圖標寬度
keyheight = ,
direction = , #圖標方向,horizontal/vetical
default.unit = 'line',
override.aes = list(), #忽略aes中設置
nrow = , #幾行
ncol = , #幾列
byrow = FALSE, #是否按行
reverse = FALSE, #圖例是否翻轉
order = 0,...)
guide_legend
結合guides
函數調整圖例。
圖例的四種形式:fill, color, shape, size
,使用guides函數時,使用相應參數即可。
df <- data.frame(x=1:20,y=1:20,color=letters[1:20])
p <- ggplot(df,aes(x,y))+geom_point(aes(color=color))
p+guide_legend(title='legend',nrow = 4,ncol = 5) #error
p+guides(color=guide_legend('legend',nrow=4,ncol=5,label.position='left'))
不同的圖例可以同時調整。
dat <- data.frame(x=1:5,y=1:5,p=1:5,q=factor(1:5),r=factor(1:5))
#生成3種圖例
pp <- ggplot(dat,aes(x,y,color=p,size=q,shape=r))+
geom_point()
#只使用guides函數
b=pp+guides(color='colorbar', #顏色條
size='none', #不顯示
shape='legend') #普通圖例
grid.arrange(pp,b,ncol=2)
#結合guide_*函數
c=pp+guides(color=guide_colorbar('color'), #顏色條用相應函數colorbar
shape=guide_legend('shape',ncol=5))
#將3個圖例整合成一個
d=pp+guides(color=guide_legend('title'),
size=guide_legend('title'),
shape=guide_legend('title'))
grid.arrange(c,d,ncol=2)
order參數:
不同圖例的排列順序。
ggplot(mpg,aes(displ,cty))+
geom_point(aes(size=hwy,color=cyl,shape=drv))+
guides(color=guide_colorbar(order = 2),
shape=guide_legend(order=3),
size=guide_legend(order=1)) #第一位
2. 標度函數調整圖例
在scale_*中使用guide參數。標度函數中,有嚴格的連續型和離散型變量之分。colorbar針對連續型變量,legend針對離散型變量。
#只用guide參數
a=pp+scale_color_continuous(guide='colorbar')+
scale_shape(guide='legend')+
scale_size_discrete(guide='legend')
#結合guide_*函數(即自定義圖例)
b=pp+scale_color_continuous(guide=guide_colorbar('color'))+
scale_shape(guide=guide_legend('shape',ncol=5))+
scale_size_discrete(guide=guide_legend('size',ncol=5))
grid.arrange(a,b,ncol=2)
3. 結合theme函數調整圖例
theme函數修飾圖例有關參數:
示例。
pt <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+
geom_point()
a=pt+scale_color_discrete(name='cyl')+
theme(legend.title = element_text(color='blue'),
legend.background = element_rect(color = 'red',
linetype = 2))
b=pt+scale_color_discrete(name='cyl')+
theme(legend.position = 'bottom',
legend.text = element_text(color='red',size=13,angle = 45),
legend.key = element_rect(color='black',fill = 'orange'),
legend.key.height = unit(1,'cm'),
legend.key.width = unit(1,'cm'))
grid.arrange(a,b,ncol=2)
自定義圖例到圖形區域中。
pt <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+
geom_point()
pt+scale_color_discrete(name='cyl')+
theme(legend.position = c(0.9,0.8), #相對位置比例
legend.title = element_text(color='blue'),
legend.background = element_rect(color='red',
linetype = 2))
4. overrides.aes參數的技巧
該參數接受list函數,可對圖例樣式進行修改而不受映射函數的影響。
示例比較。
p=ggplot(diamonds,aes(carat,price))+
geom_point(aes(color=color),alpha=1/100)
b=p+guides(color=guide_legend(override.aes = list(alpha=1)))
grid.arrange(p,b,ncol=2)
雖然圖上的點模糊,但圖例上的點很清晰(將alpha還原為1),這就是overrides.aes參數作用。
對圖例進行其他設置,而不受映射函數干擾,如圖例中圖標大小。
df <- data.frame(id=rep(c('hq','lq'),each=5),
values=rnorm(n=10,mean=1,sd=0.5)+c(1:10),
period=rep(c(1:5),2))
ggplot(df,aes(x=period,y=values,group=id,shape=id,fill=id))+
geom_line(color='gray40')+
geom_point(size=2)+
guides(shape=guide_legend(override.aes = list(size=5)))+
scale_fill_manual(values=c('lightskyblue1','lightpink'),
labels=c('HQ','LQ'))+
scale_shape_manual(values = c(22,24),labels=c('HQ',"LQ"))
為突出圖例中的點,圖例中的點設置比圖中點大。
fill和shape的labels設置相同,合並了兩種圖例。