R語言可視化學習筆記之ggpubr包

作者簡介Introduction

taoyan:偽碼農,R語言愛好者,愛開源。

個人博客: ytlogos.github.io/

往期回顧

R語言學習筆記之聚類分析

Hadley Wickham創建的可視化包ggplot2可以流暢地進行優美的可視化,但是如果要通過ggplot2定製一套圖形,尤其是適用於雜誌期刊等出版物的圖形,對於那些沒有深入了解ggplot2的人來說就有點困難了,ggplot2的部分語法是很晦澀的。為此Alboukadel Kassambara創建了基於ggplot2的可視化包ggpubr用於繪製符合出版物要求的圖形。

安裝及載入ggpubr包

安裝方式有兩種:

  • 直接從CRAN安裝:

install.packages("ggpubr")

  • 從GitHub上安裝最新版本:

if(!require(devtools)) install.packages("devtools") devtools::install_github("kassambara/ggpubr")

安裝完之後直接載入就行:

library(ggpubr)

ggpubr可繪製圖形

ggpubr可繪製大部分我們常用的圖形,下面一一介紹。

分布圖(Distribution)

#構建數據集set.seed(1234)

df <- data.frame( sex=factor(rep(c("f", "M"), each=200)), weight=c(rnorm(200, 55), rnorm(200, 58)))

head(df)

## sex weight

## 1 f 53.79293

## 2 f 55.27743

## 3 f 56.08444

## 4 f 52.65430

## 5 f 55.42912

## 6 f 55.50606

密度分布圖以及邊際地毯線並添加平均值線

ggdensity(df, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))

帶有均值線和邊際地毯線的直方圖

gghistogram(df, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))

箱線圖與小提琴圖

#載入數據集ToothGrowth

data("ToothGrowth")

df1 <- ToothGrowth head(df1)

## 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

p <- ggboxplot(df1, x="dose", y="len", color = "dose",

palette = c("#00AFBB", "#E7B800", "#FC4E07"),

add = "jitter", shape="dose")#增加了jitter點,點shape由dose映射p

增加不同組間的p-value值,可以自定義需要標註的組間比較

my_comparisons <- list(c("0.5", "1"), c("1", "2"), c("0.5", "2")) p+stat_compare_means(comparisons = my_comparisons)+#不同組間的比較stat_compare_means(label.y=50)

內有箱線圖的小提琴圖

ggviolin(df1, x="dose", y="len", fill = "dose",

palette = c("#00AFBB", "#E7B800", "#FC4E07"),

add = "boxplot", add.params = list(fill="white"))+

stat_compare_means(comparisons = my_comparisons, label =

"p.signif")+#label這裡表示選擇顯著性標記(星號)

stat_compare_means(label.y = 50)

條形圖

data("mtcars")

df2 <- mtcars

df2$cyl <- factor(df2$cyl)

df2$name <- rownames(df2)#添加一行name

head(df2[, c("name", "wt", "mpg", "cyl")])

按從小到大順序繪製條形圖(不分組排序)

ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",

palette = "jco",#雜誌jco的配色

sort.val = "desc",#下降排序

sort.by.groups=FALSE,#不按組排序

x.text.angle=60)

按組進行排序

ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",

palette = "jco",#雜誌jco的配色

sort.val = "asc",#上升排序,區別於desc,具體看圖演示

sort.by.groups=TRUE,#按組排序

x.text.angle=90)

偏差圖

偏差圖展示了與參考值之間的偏差

df2$mpg_z <- (df2$mpg-mean(df2$mpg))/sd(df2$mpg)

df2$mpg_grp <- factor(ifelse(df2$mpg_z<0, "low", "high"), levels = c("low", "high"))

head(df2[, c("name", "wt", "mpg", "mpg_grp", "cyl")])

繪製排序過的條形圖

ggbarplot(df2, x="name", y="mpg_z", fill = "mpg_grp", color = "white",

palette = "jco", sort.val = "asc", sort.by.groups = FALSE, x.text.angle=60, ylab = "MPG z-score", xlab = FALSE, legend.title="MPG Group")

坐標軸變換

ggbarplot(df2, x="name", y="mpg_z", fill = "mpg_grp", color = "white",

palette = "jco", sort.val = "desc", sort.by.groups = FALSE,

x.text.angle=90, ylab = "MPG z-score", xlab = FALSE,

legend.title="MPG Group", rotate=TRUE, ggtheme = theme_minimal())

點圖(Dot charts)

棒棒糖圖(Lollipop chart)

棒棒圖可以代替條形圖展示數據

ggdotchart(df2, x="name", y="mpg", color = "cyl",

palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "ascending",

add = "segments", ggtheme = theme_pubr())

可以自設置各種參數

ggdotchart(df2, x="name", y="mpg", color = "cyl",

palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending",

add = "segments", rotate = TRUE, group = "cyl", dot.size = 6,

label = round(df2$mpg), font.label = list(color="white", size=9, vjust=0.5),

ggtheme = theme_pubr())

偏差圖

ggdotchart(df2, x="name", y="mpg_z", color = "cyl",

palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending",

add = "segment", add.params = list(color="lightgray", size=2),

group = "cyl", dot.size = 6, label = round(df2$mpg_z, 1),

font.label = list(color="white", size=9, vjust=0.5),

ggtheme = theme_pubr())+ geom_line(yintercept=0, linetype=2, color="lightgray")

Cleveland點圖

ggdotchart(df2, x="name", y="mpg", color = "cyl",

palette = c("#00AFBB", "#E7B800", "#FC4E07"),

sorting = "descending", rotate = TRUE, dot.size = 2, y.text.col=TRUE,

ggtheme = theme_pubr())+ theme_cleveland()

SessionInfo

sessionInfo()

## R version 3.4.0 (2017-04-21)

## Platform: x86_64-w64-mingw32/x64 (64-bit)

## Running under: Windows 8.1 x64 (build 9600)

##

## 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.1.3 magrittr_1.5 ggplot2_2.2.1

##

## loaded via a namespace (and not attached):

## [1] Rcpp_0.12.11 knitr_1.16 munsell_0.4.3 colorspace_1.3-2

## [5] R6_2.2.1 rlang_0.1.1 stringr_1.2.0 plyr_1.8.4

## [9] dplyr_0.5.0 tools_3.4.0 grid_3.4.0 gtable_0.2.0

## [13] DBI_0.6-1 htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0

## [17] rprojroot_1.2 digest_0.6.12 assertthat_0.2.0 tibble_1.3.3

## [21] ggsignif_0.2.0 ggsci_2.4 purrr_0.2.2.2 evaluate_0.10

## [25] rmarkdown_1.5 labeling_0.3 stringi_1.1.5 compiler_3.4.0

## [29] scales_0.4.1 backports_1.1.0

往期精彩內容整理合集

2017年R語言發展報告(國內)

R語言中文社區歷史文章整理(作者篇)

R語言中文社區歷史文章整理(類型篇)


推薦閱讀:

R-ggridges包的改進
仿經濟學人——矩陣氣泡圖
Excel圖表的基本類型與選擇
你會怎樣衡量你的產品? —— 一點產品數據分析的經驗分享
ECharts+Python 給你的數據做「美顏」

TAG:R编程语言 | 数据可视化 |