R語(yǔ)言可視化(七):箱線圖繪制

07.箱線圖繪制


清除當(dāng)前環(huán)境中的變量

rm(list=ls())

設(shè)置工作目錄

setwd("C:/Users/Dell/Desktop/R_Plots/07boxplot/")

基礎(chǔ)boxplot函數(shù)繪制箱線圖

## boxplot on a formula:
# 查看內(nèi)置數(shù)據(jù)集
head(InsectSprays)
##   count spray
## 1    10     A
## 2     7     A
## 3    20     A
## 4    14     A
## 5    14     A
## 6    12     A

boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
image.png
boxplot(count ~ spray, data = InsectSprays,
        notch = TRUE, col = "blue")
## Warning in bxp(list(stats = structure(c(7, 11, 14, 18.5, 23, 7, 12, 16.5, :
## some notches went outside hinges ('box'): maybe set notch=FALSE
image.png
## boxplot on a matrix:
mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
             `5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
head(mat)
##           Uni05       Norm          5T      Gam2
## [1,] 0.04761905  0.7106628  1.36924396 5.6044293
## [2,] 0.09523810  1.0806786  0.55538157 0.9160426
## [3,] 0.14285714  0.8803233 -1.14302098 3.7818738
## [4,] 0.19047619 -0.3892679  0.96060571 1.8471195
## [5,] 0.23809524  0.6940575 -0.03855087 1.2075029
## [6,] 0.28571429 -1.1620767  4.36417197 3.5148279

boxplot(mat) # directly, calling boxplot.matrix()
image.png
## boxplot on a data frame:
df <- as.data.frame(mat)
par(las = 1) # all axis labels horizontal
boxplot(df, main = "boxplot(*, horizontal = TRUE)", 
        col = "red", notch = T, horizontal = TRUE)
image.png
## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :
head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5

boxplot(len ~ dose, data = ToothGrowth,
        subset = supp == "VC", 
        at = 1:3 - 0.2,
        boxwex = 0.25, 
        col = "yellow",
        main = "Guinea Pigs' Tooth Growth",
        xlab = "Vitamin C dose mg",
        ylab = "tooth length",
        xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
boxplot(len ~ dose, data = ToothGrowth, 
        add = TRUE,
        subset = supp == "OJ", 
        at = 1:3 + 0.2,
        boxwex = 0.25,
        col = "orange")
legend("topleft", c("Ascorbic acid", "Orange juice"),
       fill = c("yellow", "orange"))
image.png
## With less effort (slightly different) using factor *interaction*:
boxplot(len ~ dose:supp, data = ToothGrowth,
        boxwex = 0.5, col = c("orange", "yellow"),
        main = "Guinea Pigs' Tooth Growth",
        xlab = "Vitamin C dose mg", ylab = "tooth length",
        sep = ":", lex.order = TRUE, ylim = c(0, 35), yaxs = "i")
image.png

ggplot2包繪制箱線圖

library(ggplot2)

data <- read.table("demo1_boxplot.txt",header = T)
head(data)
##   BRCA1 sample_type
## 1  4.53       Tumor
## 2  6.28       Tumor
## 3  6.38       Tumor
## 4  6.45       Tumor
## 5  6.67       Tumor
## 6  6.84       Tumor

ggplot(data,aes(sample_type,BRCA1,fill=sample_type)) + 
        geom_boxplot()
image.png
# 添加擾動(dòng)點(diǎn)嘹叫,更改離群點(diǎn)的顏色和蚪,形狀和大小
ggplot(data,aes(sample_type,BRCA1,fill=sample_type)) + 
        geom_boxplot(width=0.5,outlier.color = "red",outlier.shape = 2,outlier.size = 3) + 
        geom_jitter(shape=16, position=position_jitter(0.2))
image.png
# 添加notch吝沫,更改顏色
ggplot(data,aes(sample_type,BRCA1,fill=sample_type)) + 
        geom_boxplot(notch = T,width=0.5,alpha=0.8) + 
        scale_fill_brewer(palette="Set1")
image.png
# 添加均值點(diǎn)
ggplot(data,aes(sample_type,BRCA1,fill=sample_type)) + 
        geom_boxplot(notch = T,width=0.5,alpha=0.8) + 
        scale_fill_brewer(palette="Set1") + 
        stat_summary(fun.y="mean",geom="point",shape=23,
                     size=4,fill="white")
image.png
# 添加誤差棒
ggplot(data,aes(sample_type,BRCA1,fill=sample_type)) + 
        geom_boxplot(notch = T,width=0.5,alpha=0.8) + 
        stat_boxplot(geom = "errorbar",width=0.1) +
        scale_fill_brewer(palette="Set1") + 
        stat_summary(fun.y="mean",geom="point",shape=23,
                     size=4,fill="white")
image.png
# 更換主題背景,旋轉(zhuǎn)坐標(biāo)軸
ggplot(data,aes(sample_type,BRCA1,fill=sample_type)) + 
        stat_boxplot(geom = "errorbar",width=0.1) +
        geom_boxplot(notch = T,width=0.5,alpha=0.8) + 
        scale_fill_brewer(palette="Set1") + 
        stat_summary(fun.y="mean",geom="point",
                     shape=23,size=4,fill="white") +
        theme_bw() + coord_flip()

image.png

ggpubr包繪制箱線圖

library(ggpubr)

data <- read.table("demo1_boxplot.txt",header = T)
head(data)
##   BRCA1 sample_type
## 1  4.53       Tumor
## 2  6.28       Tumor
## 3  6.38       Tumor
## 4  6.45       Tumor
## 5  6.67       Tumor
## 6  6.84       Tumor

ggboxplot(data,x="sample_type",y="BRCA1",
          width = 0.6,fill="sample_type")
image.png
# 添加notch滥崩,擾動(dòng)點(diǎn),更改顏色
ggboxplot(data,x="sample_type",y="BRCA1",
          width = 0.6,fill="sample_type",
          notch = T,palette = c("#00AFBB", "#E7B800"),
          add = "jitter",shape="sample_type")
image.png
# 添加誤差棒和均值
ggboxplot(data,x="sample_type",y="BRCA1",
          width = 0.6,fill="sample_type",
          bxp.errorbar = T, bxp.errorbar.width = 0.2,
          add = "mean",add.params = list(size=1,color="white"))
image.png
# 旋轉(zhuǎn)坐標(biāo)軸
ggboxplot(data,x="sample_type",y="BRCA1",
          width = 0.6,fill="sample_type",
          add = "mean",add.params = list(size=1,color="white"),
          notch = T,orientation = "horizontal")
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggpubr_0.2.1  magrittr_1.5  ggplot2_3.2.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.1         knitr_1.23         tidyselect_0.2.5  
##  [4] munsell_0.5.0      colorspace_1.4-1   R6_2.4.0          
##  [7] rlang_0.4.0        stringr_1.4.0      dplyr_0.8.3       
## [10] tools_3.6.0        grid_3.6.0         gtable_0.3.0      
## [13] xfun_0.8           withr_2.1.2        htmltools_0.3.6   
## [16] yaml_2.2.0         lazyeval_0.2.2     digest_0.6.20     
## [19] assertthat_0.2.1   tibble_2.1.3       ggsignif_0.5.0    
## [22] crayon_1.3.4       RColorBrewer_1.1-2 purrr_0.3.2       
## [25] glue_1.3.1         evaluate_0.14      rmarkdown_1.13    
## [28] labeling_0.3       stringi_1.4.3      compiler_3.6.0    
## [31] pillar_1.4.2       scales_1.0.0       pkgconfig_2.0.2
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末讹语,一起剝皮案震驚了整個(gè)濱河市钙皮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌顽决,老刑警劉巖短条,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異才菠,居然都是意外死亡茸时,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門赋访,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)可都,“玉大人,你說(shuō)我怎么就攤上這事蚓耽∏” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵步悠,是天一觀的道長(zhǎng)签杈。 經(jīng)常有香客問(wèn)我,道長(zhǎng)鼎兽,這世上最難降的妖魔是什么答姥? 我笑而不...
    開封第一講書人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮接奈,結(jié)果婚禮上踢涌,老公的妹妹穿的比我還像新娘。我一直安慰自己序宦,他們只是感情好睁壁,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般潘明。 火紅的嫁衣襯著肌膚如雪行剂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評(píng)論 1 291
  • 那天钳降,我揣著相機(jī)與錄音厚宰,去河邊找鬼。 笑死遂填,一個(gè)胖子當(dāng)著我的面吹牛铲觉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吓坚,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼撵幽,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了礁击?” 一聲冷哼從身側(cè)響起盐杂,我...
    開封第一講書人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哆窿,沒(méi)想到半個(gè)月后链烈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挚躯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年强衡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片秧均。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡食侮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出目胡,到底是詐尸還是另有隱情锯七,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布誉己,位于F島的核電站眉尸,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏巨双。R本人自食惡果不足惜噪猾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望筑累。 院中可真熱鬧袱蜡,春花似錦、人聲如沸慢宗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至敏晤,卻和暖如春贱田,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嘴脾。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工男摧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人译打。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓耗拓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親扶平。 傳聞我的和親對(duì)象是個(gè)殘疾皇子帆离,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350