R語(yǔ)言ggplot2設(shè)置圖例(legend)的操作大全
本文在 http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ 的基礎(chǔ)上加入了自己的理解
圖例用來(lái)解釋圖中的各種含義,比如顏色,形狀,大小等等, 在ggplot2中aes
中的參數(shù)(x, y 除外)基本都會(huì)生成圖例來(lái)解釋圖形, 比如 fill, colour, linetype, shape.
基本箱線圖(帶有圖例)
library(ggplot2) bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() bp
移除圖例
Use guides(fill=FALSE), replacing fill with the desired aesthetic. 使用 guides(fill=FALSE)
移除由ase
中 匹配的fill
生成的圖例, 也可以使用theme
You can also remove all the legends in a graph, using theme.
bp + guides(fill=FALSE)
# 也可以這也 bp + scale_fill_discrete(guide=FALSE)
# 移除所有圖例 bp + theme(legend.position="none")
修改圖例的內(nèi)容
改變圖例的順序?yàn)?trt1, 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)題
# Remove title for fill legend bp + guides(fill=guide_legend(title=NULL))
# Remove title for all legends bp + theme(legend.title=element_blank())
修改圖例中的標(biāo)簽
兩種方法一種是直接修改標(biāo)簽, 另一種是修改data.frame
Using scales
圖例可以根據(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", "Treatment 1", "Treatment 2"))
# 自定義fill的顏色 bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"), name="Experimental\nCondition", breaks=c("ctrl", "trt1", "trt2"), labels=c("Control", "Treatment 1", "Treatment 2"))
注意這里并不能修改 x軸 的標(biāo)簽,如果需要改變x軸的標(biāo)簽,可以參照http://blog.csdn.net/tanzuozhev/article/details/51107583
# A different data set 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) ) # A basic graph 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"))
If you use both colour and shape, they both need to be given scale specifications. Otherwise there will be two two separate legends. 如果同時(shí)使用 color
和shape
,那么必須都進(jìn)行scale_xx_xxx的定義,否則color
和shape
的圖例就會(huì)合并到一起, 如果 scale_xx_xxx
中的name
相同,那么他們也會(huì)合并到一起.
# Specify colour and shape lp1 <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line() + geom_point() lp1
# Here's what happens if you just specify colour lp1 + scale_colour_discrete(name ="Payer", breaks=c("Female", "Male"), labels=c("Woman", "Man"))
# Specify both colour and shape 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的種類
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, point shapes, line types, point sizes) continuous
連續(xù)行數(shù)據(jù) (e.g., alpha, colors, point sizes)
修改data.frame的factor
pg <- PlantGrowth # Copy data into new data frame # Rename the column and the values in the factor levels(pg$group)[levels(pg$group)=="ctrl"] <- "Control" levels(pg$group)[levels(pg$group)=="trt1"] <- "Treatment 1" levels(pg$group)[levels(pg$group)=="trt2"] <- "Treatment 2" names(pg)[names(pg)=="group"] <- "Experimental Condition" # View a few rows from the end product head(pg)
## weight Experimental Condition ## 1 4.17 Control ## 2 5.58 Control ## 3 5.18 Control ## 4 6.11 Control ## 5 4.50 Control ## 6 4.61 Control
# Make the plot ggplot(data=pg, aes(x=`Experimental Condition`, y=weight, fill=`Experimental Condition`)) + 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)
# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right) bp + theme(legend.position=c(.5, .5))
# Set the "anchoring point" of the legend (bottom-left is 0,0; top-right is 1,1) # Put bottom-left corner of legend box in bottom-left corner of graph bp + theme(legend.justification=c(0,0), # 這個(gè)參數(shù)設(shè)置很關(guān)鍵 legend.position=c(0,0))
# Put bottom-right corner of legend box in bottom-right corner of graph bp + theme(legend.justification=c(1,0), legend.position=c(1,0))
隱藏斜線
# No outline ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar()
# 如果設(shè)置了顏色, 那么圖例中就會(huì)出現(xiàn) 黑色斜線 ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar(colour="black")
# 黑魔法: 可以先設(shè)置geom_bar, 然后再來(lái)一個(gè)沒(méi)有 圖例 的 geom_bar ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar() + geom_bar(colour="black", show_guide=FALSE)
總結(jié)
到此這篇關(guān)于R語(yǔ)言ggplot2設(shè)置圖例(legend)的文章就介紹到這了,更多相關(guān)R語(yǔ)言ggplot2圖例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- R語(yǔ)言ggplot2圖例修改超詳細(xì)介紹
- R語(yǔ)言中g(shù)gplot2繪制雙坐標(biāo)軸圖
- R語(yǔ)言ggplot2圖例標(biāo)簽、標(biāo)題、順序修改和刪除操作實(shí)例
- R語(yǔ)言ggplot2拼圖包patchwork安裝使用
- R語(yǔ)言包ggplot實(shí)現(xiàn)分面去掉小標(biāo)題的灰色底色小技巧
- R語(yǔ)言學(xué)習(xí)ggplot2繪制統(tǒng)計(jì)圖形包全面詳解
- R語(yǔ)言數(shù)據(jù)可視化ggplot繪制置信區(qū)間與分組繪圖技巧
- R語(yǔ)言使用ggplot繪制畫中畫細(xì)節(jié)放大的方法
- R語(yǔ)言ggplot2之圖例的設(shè)置
- R語(yǔ)言ggplot2包之坐標(biāo)軸詳解
- R語(yǔ)言ggplot在熱圖上標(biāo)注相關(guān)系數(shù)的操作方法
相關(guān)文章
利用R語(yǔ)言合并數(shù)據(jù)框的行與列實(shí)例代碼
實(shí)際操作中我們經(jīng)常需要引入其他表中的列,即將其他表中列加入到表中,需要把兩個(gè)或者更多的表合并成一個(gè),下面這篇文章主要給大家介紹了關(guān)于利用R語(yǔ)言合并數(shù)據(jù)框的行與列的相關(guān)資料,需要的朋友可以參考下2022-07-07使用R語(yǔ)言將表格導(dǎo)出為CSV文件簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于使用R語(yǔ)言將表格導(dǎo)出為CSV文件的相關(guān)資料,CSV(逗號(hào)分隔值)是一種常見(jiàn)的文本文件格式,廣泛用于數(shù)據(jù)交換,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02R語(yǔ)言繪圖時(shí)輸出希臘字符上下標(biāo)及數(shù)學(xué)公式實(shí)現(xiàn)方法
這篇文章主要為大家介紹了R語(yǔ)言進(jìn)行繪圖時(shí)輸出希臘字符上標(biāo),下標(biāo)及數(shù)學(xué)公式的實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11詳解R語(yǔ)言caret包trainControl函數(shù)
這篇文章主要介紹了R語(yǔ)言caret包trainControl函數(shù)詳解,本文通過(guò)源碼分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08R語(yǔ)言中cbind、rbind和merge函數(shù)的使用與區(qū)別
這篇文章主要介紹了R語(yǔ)言中cbind、rbind和merge函數(shù)的使用與區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03