最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

R語言ggplot2設(shè)置圖例(legend)的操作大全

 更新時(shí)間:2022年07月21日 10:41:40   作者:探索者v  
ggplot2是一個(gè)繪制可視化圖形的R包,汲取了R語言基礎(chǔ)繪圖系統(tǒng)(graphics)和l?attice包的優(yōu)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于R語言ggplot2設(shè)置圖例(legend)的操作大全,需要的朋友可以參考下

本文在 http://www.cookbook-r.com/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

移除圖例

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_manualscale_colour_hue,scale_colour_manualscale_shape_discretescale_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_xxxxxx 表示處理數(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í)使用 colorshape,那么必須都進(jìn)行scale_xx_xxx的定義,否則colorshape的圖例就會(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)來設(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, 然后再來一個(gè)沒有 圖例 的 geom_bar
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
    geom_bar() +
    geom_bar(colour="black", show_guide=FALSE)

總結(jié) 

到此這篇關(guān)于R語言ggplot2設(shè)置圖例(legend)的文章就介紹到這了,更多相關(guān)R語言ggplot2圖例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • R語言glmnet包lasso回歸中分類變量的處理圖文詳解

    R語言glmnet包lasso回歸中分類變量的處理圖文詳解

    Lasso回歸又稱為套索回歸,是Robert?Tibshirani于1996年提出的一種新的變量選擇技術(shù),下面這篇文章主要給大家介紹了關(guān)于R語言glmnet包lasso回歸中分類變量的處理的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • R語言-如何讀寫帶分隔符的文件

    R語言-如何讀寫帶分隔符的文件

    這篇文章主要介紹了R語言讀寫帶分隔符的文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言入門教程之刪除指定數(shù)據(jù)的方法

    R語言入門教程之刪除指定數(shù)據(jù)的方法

    這篇文章主要介紹了R語言入門教程之刪除指定數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • R語言基本運(yùn)算的示例代碼

    R語言基本運(yùn)算的示例代碼

    這篇文章主要介紹了R語言基本運(yùn)算的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • R ggplot2 修改默認(rèn)顏色的操作

    R ggplot2 修改默認(rèn)顏色的操作

    這篇文章主要介紹了R ggplot2 修改默認(rèn)顏色的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言繪制line?plot線圖示例詳解

    R語言繪制line?plot線圖示例詳解

    這篇文章主要為大家介紹了R語言繪制line?plot線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-02-02
  • Rstudio更換默認(rèn)下載鏡像(下載源)的操作教程

    Rstudio更換默認(rèn)下載鏡像(下載源)的操作教程

    這篇文章主要介紹了Rstudio更換默認(rèn)下載鏡像(下載源)的操作教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言數(shù)據(jù)類型和對(duì)象深入講解

    R語言數(shù)據(jù)類型和對(duì)象深入講解

    這篇文章主要介紹了R語言數(shù)據(jù)類型和對(duì)象深入講解,文中列舉的實(shí)例講解的很清楚,有感興趣的同學(xué)可以學(xué)習(xí)下
    2021-03-03
  • R語言實(shí)現(xiàn)漂亮的ROC圖效果

    R語言實(shí)現(xiàn)漂亮的ROC圖效果

    pROC是一個(gè)專門用來計(jì)算和繪制ROC曲線的R包,目前已被CRAN收錄,因此安裝也非常簡(jiǎn)單,同時(shí)該包也兼容ggplot2函數(shù)繪圖,本次就教大家怎么用pROC來快速畫出ROC圖,感興趣的朋友一起看看吧
    2022-06-06
  • R語言實(shí)現(xiàn)導(dǎo)出矩陣

    R語言實(shí)現(xiàn)導(dǎo)出矩陣

    這篇文章主要介紹了R語言實(shí)現(xiàn)導(dǎo)出矩陣,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評(píng)論

鄯善县| 忻城县| 奎屯市| 余江县| 白水县| 齐齐哈尔市| 辰溪县| 大新县| 陇川县| 呼图壁县| 泰来县| 大城县| 鄂温| 承德县| 永仁县| 沂水县| 宁河县| 大埔县| 乡宁县| 富民县| 确山县| 天水市| 大田县| 读书| 桐城市| 石棉县| 西和县| 泰宁县| 得荣县| 独山县| 手机| 林州市| 遂溪县| 枣强县| 集安市| 福建省| 靖宇县| 古交市| 岑巩县| 商洛市| 承德市|