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

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

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

本文在 http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ 的基礎(chǔ)上加入了自己的理解

圖例用來解釋圖中的各種含義,比如顏色,形狀,大小等等, 在ggplot2中aes中的參數(shù)(x, y 除外)基本都會生成圖例來解釋圖形, 比如 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)容

改變圖例的順序為 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)))

隱藏圖例標題

# Remove title for fill legend
bp + guides(fill=guide_legend(title=NULL))

# Remove title for all legends
bp + theme(legend.title=element_blank())

修改圖例中的標簽

兩種方法一種是直接修改標簽, 另一種是修改data.frame

Using scales

圖例可以根據(jù) fill, colour, linetype, shape 等繪制, 我們以 fill 為例, scale_fill_xxxxxx 表示處理數(shù)據(jù)的一種方法, 可以是 hue(對顏色的定量操作), continuous(連續(xù)型數(shù)據(jù)處理), discete(離散型數(shù)據(jù)處理)等等.

# 設(shè)置圖例名稱
bp + scale_fill_discrete(name="Experimental\nCondition")

# 設(shè)置圖例的名稱, 重新定義新的標簽名稱
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軸 的標簽,如果需要改變x軸的標簽,可以參照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. 如果同時使用 colorshape,那么必須都進行scale_xx_xxx的定義,否則colorshape的圖例就會合并到一起, 如果 scale_xx_xxx 中的name相同,那么他們也會合并到一起.

# 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: 點 線 或者其他圖形的框線顏色 fill: 填充顏色 linetype :線型, 實線 虛線 點線 shape: 點的性狀,超級多,可以自己搜索一下 size: 點的大小 alpha: 透明度

yyy 的分離 hue: 設(shè)置色調(diào)范圍(h)、飽和度(c)和亮度(l)獲取顏色 manual: 手動設(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()

修改標題和標簽的顯示

# 標題
bp + theme(legend.title = element_text(colour="blue", size=16, face="bold"))

# 標簽
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ù)坐標來設(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), # 這個參數(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è)置了顏色, 那么圖例中就會出現(xiàn) 黑色斜線
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
    geom_bar(colour="black")

# 黑魔法: 可以先設(shè)置geom_bar, 然后再來一個沒有 圖例 的 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • R語言中Fisher判別的使用方法

    R語言中Fisher判別的使用方法

    這篇文章主要介紹了R語言中Fisher判別的使用方法,文中
    2021-03-03
  • R語言繪圖-點圖dot plot

    R語言繪圖-點圖dot plot

    這篇文章主要介紹了R語言繪圖-點圖dot plot案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 利用R語言合并數(shù)據(jù)框的行與列實例代碼

    利用R語言合并數(shù)據(jù)框的行與列實例代碼

    實際操作中我們經(jīng)常需要引入其他表中的列,即將其他表中列加入到表中,需要把兩個或者更多的表合并成一個,下面這篇文章主要給大家介紹了關(guān)于利用R語言合并數(shù)據(jù)框的行與列的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • R語言-修改(替換)因子變量的元素操作

    R語言-修改(替換)因子變量的元素操作

    這篇文章主要介紹了R語言-修改(替換)因子變量的元素操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 使用R語言將表格導(dǎo)出為CSV文件簡單示例

    使用R語言將表格導(dǎo)出為CSV文件簡單示例

    這篇文章主要給大家介紹了關(guān)于使用R語言將表格導(dǎo)出為CSV文件的相關(guān)資料,CSV(逗號分隔值)是一種常見的文本文件格式,廣泛用于數(shù)據(jù)交換,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • R語言的Dataframe常用操作使用

    R語言的Dataframe常用操作使用

    本文將結(jié)合實例代碼,介紹R語言的Dataframe常用操作使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • R語言ggplot2圖例修改超詳細介紹

    R語言ggplot2圖例修改超詳細介紹

    ggplot2是R語言最流行的畫圖包,基于圖層化語法的思想設(shè)計和創(chuàng)建美觀優(yōu)雅的圖形,下面這篇文章主要給大家介紹了關(guān)于R語言ggplot2圖例修改的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • R語言繪圖時輸出希臘字符上下標及數(shù)學(xué)公式實現(xiàn)方法

    R語言繪圖時輸出希臘字符上下標及數(shù)學(xué)公式實現(xiàn)方法

    這篇文章主要為大家介紹了R語言進行繪圖時輸出希臘字符上標,下標及數(shù)學(xué)公式的實現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2021-11-11
  • 詳解R語言caret包trainControl函數(shù)

    詳解R語言caret包trainControl函數(shù)

    這篇文章主要介紹了R語言caret包trainControl函數(shù)詳解,本文通過源碼分析給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • R語言中cbind、rbind和merge函數(shù)的使用與區(qū)別

    R語言中cbind、rbind和merge函數(shù)的使用與區(qū)別

    這篇文章主要介紹了R語言中cbind、rbind和merge函數(shù)的使用與區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論