亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

R語言ggplot2之圖例的設(shè)置

 更新時間:2021年04月22日 11:57:29   作者:zx403413599  
這篇文章主要介紹了R語言ggplot2之圖例的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

引言

圖例的設(shè)置包括移除圖例、改變圖例的位置、改變標(biāo)簽的順序、改變圖例的標(biāo)題等。

移除圖例

有時候你想移除圖例,使用 guides()。

library(ggplot2)
p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p + guides(fill=FALSE)

改變圖例的位置

我們可以用theme(legend.position=…)將圖例移到圖表的上方、下方、左邊和右邊。

p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
scale_fill_brewer(palette="Pastel2")
#上方 
p + theme(legend.position="top")#左邊left,右邊 right, 底部bottom

改變圖例標(biāo)簽的順序

我們可以設(shè)置圖例的指定順序,也可以逆轉(zhuǎn)圖例的位置。

p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
#使用limit參數(shù)設(shè)置圖例位置
p + scale_fill_discrete(limits=c("trt1", "trt2", "ctrl"))
#使用guides(fill=guide_legend(reverse=TRUE))逆轉(zhuǎn)圖例
p + guides(fill=guide_legend(reverse=TRUE))

設(shè)置圖例的標(biāo)題

我們可以改變圖例的標(biāo)題,也可以改變標(biāo)題的主題格式,還可以刪除圖例標(biāo)題。

#改變標(biāo)題名字 用labs()
p + labs(fill="Condition")
# 設(shè)置圖例的標(biāo)題的字體、顏色、大小用theme(legend.title=element_text())
p + theme(legend.title=element_text(face="italic", family="Times", colour="red",
size=14))
#移除圖例標(biāo)題
#增加 guides(fill=guide_legend(title=NULL))函數(shù)即可移除圖例標(biāo)題
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
guides(fill=guide_legend(title=NULL))

設(shè)置圖例的標(biāo)簽

library(gcookbook)
#改變標(biāo)簽的名字
p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p + scale_fill_discrete(labels=c("Control", "Treatment 1", "Treatment 2"))
#改變標(biāo)簽的主題
p + theme(legend.text=element_text(face="italic", family="Times", colour="red",
size=14))
#多行圖例標(biāo)簽的展示
#有時候標(biāo)簽名字較長,一行展示不夠美觀,需要多行呈現(xiàn) 加一個\n
p + scale_fill_discrete(labels=c("Control", "Type 1\ntreatment",
"Type 2\ntreatment"))

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論