Hadley Wickham創(chuàng)建的可視化包ggplot2可以流暢地進(jìn)行優(yōu)美的可視化戴而,但是如果要通過(guò)ggplot2定制一套圖形换途,尤其是適用于雜志期刊等出版物的圖形,對(duì)于那些沒(méi)有深入了解ggplot2的人來(lái)說(shuō)就有點(diǎn)困難了毅访,ggplot2的部分語(yǔ)法是很晦澀的竟痰。為此Alboukadel Kassambara創(chuàng)建了基于ggplot2的可視化包ggpubr用于繪制符合出版物要求的圖形。
安裝及加載ggpubr包
安裝方式有兩種:
- 直接從CRAN安裝:
install.packages("ggpubr")
- 從GitHub上安裝最新版本:
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
安裝完之后直接加載就行:
library(ggpubr)
ggpubr可繪制圖形:
ggpubr可繪制大部分我們常用的圖形硬霍,下面一一介紹帜慢。
分布圖(Distribution)
#構(gòu)建數(shù)據(jù)集
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"))
箱線圖與小提琴圖
#加載數(shù)據(jù)集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點(diǎn),點(diǎn)shape由dose映射p
增加不同組間的p-value值唯卖,可以自定義需要標(biāo)注的組間比較
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)
內(nèi)有箱線圖的小提琴圖
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這里表示選擇顯著性標(biāo)記(星號(hào))
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)
按組進(jìn)行排序
ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",
palette = "jco",#雜志jco的配色
sort.val = "asc",#上升排序,區(qū)別于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")])
繪制排序過(guò)的條形圖
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")
坐標(biāo)軸變換
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())
點(diǎn)圖(Dot charts)
棒棒糖圖(Lollipop chart)
棒棒圖可以代替條形圖展示數(shù)據(jù)
ggdotchart(df2, x="name", y="mpg", color = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "ascending",
add = "segments", ggtheme = theme_pubr())
可以自設(shè)置各種參數(shù)
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點(diǎn)圖
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