更好的閱讀體驗-->>
ggplot2 (Hadley Wickham開發(fā))是目前R語言數(shù)據(jù)可視化的主流绰垂。與R的基礎(chǔ)繪圖系統(tǒng)相比,基于grid繪圖系統(tǒng)的ggplot2已經(jīng)在語法理解性上已經(jīng)進步很多私沮,但是通過ggplot2繪制用于學(xué)術(shù)雜志的圖形三娩,仍然需要較多的繪圖函數(shù)(或者加載一些寫好的模板代碼)携添。為此Alboukadel Kassambara基于ggplot2巩梢、ggsci包開發(fā)了ggpubr用于繪制符合出版物要求的圖形。該包封裝了很多ggplot2的繪圖函數(shù)知染,并且內(nèi)嵌了ggsci中很多優(yōu)秀的學(xué)術(shù)期刊配色方案肋僧,值得學(xué)習(xí)使用。
ggpubr包括一些關(guān)鍵的特性:
- 能幫助研究人員快速創(chuàng)建易于發(fā)表的圖形;
- 能夠?qū)值和顯著性水平自動添加到圖形上而無需二次編輯嫌吠;
- 使圖形注釋和排版變得容易止潘;
- 使更改圖形參數(shù)(例如顏色和標簽)變得容易。
安裝
從CRAN安裝:
install.packages("ggpubr")
或者也可以從Github安裝最新版本:
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
加載ggpubr包:
library("ggpubr")
ggpubr可繪制的圖形
加載數(shù)據(jù)
library(ggpubr)
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata, 4)
## sex weight
## 1 F 53.8
## 2 F 55.3
## 3 F 56.1
## 4 F 52.7
密度圖
# 帶有平均值線和邊際地毯的密度圖
ggdensity(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
直方圖
# 帶有平均值線和邊際地毯的直方圖
gghistogram(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
箱線圖和小提琴圖
# 加載數(shù)據(jù)
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)
## 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
# 帶有抖動點圖的箱線圖
p <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
p
# 添加P值
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+ # 添加每兩組間的P值
stat_compare_means(label.y = 50) # 添加全局P值
# 帶有箱線圖的小提琴圖
ggviolin(df, 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")+ # 添加顯著性水平
stat_compare_means(label.y = 50) # 添加全局P值
條形圖
數(shù)據(jù)加載
data("mtcars")
dfm <- mtcars
dfm$cyl <- as.factor(dfm$cyl)
dfm$name <- rownames(dfm)
head(dfm[, c("name", "wt", "mpg", "cyl")])
## name wt mpg cyl
## Mazda RX4 Mazda RX4 2.62 21.0 6
## Mazda RX4 Wag Mazda RX4 Wag 2.88 21.0 6
## Datsun 710 Datsun 710 2.32 22.8 4
## Hornet 4 Drive Hornet 4 Drive 3.21 21.4 6
## Hornet Sportabout Hornet Sportabout 3.44 18.7 8
## Valiant Valiant 3.46 18.1 6
有序條形圖
通過cyl
更改填充色辫诅,并且對全部數(shù)據(jù)進行排序, 而非分組排序凭戴。
ggbarplot(dfm, x = "name", y = "mpg",
fill = "cyl",
color = "white",
palette = "jco",
sort.val = "desc",
sort.by.groups = FALSE,
x.text.angle = 90
)
對每組內(nèi)的數(shù)據(jù)進行排序,可設(shè)置sort.by.groups = TRUE炕矮。
ggbarplot(dfm, x = "name", y = "mpg",
fill = "cyl",
color = "white",
palette = "jco",
sort.val = "asc",
sort.by.groups = TRUE,
x.text.angle = 90
)
偏差圖
偏差圖一般用以展示變量與參考值之間的偏差程度么夫。下面將以mtcars數(shù)據(jù)集中的mpg z-score來繪制偏差圖。
計算mpg數(shù)據(jù)的z-score:
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"),
levels = c("low", "high"))
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
## name wt mpg mpg_z mpg_grp cyl
## Mazda RX4 Mazda RX4 2.62 21.0 0.151 high 6
## Mazda RX4 Wag Mazda RX4 Wag 2.88 21.0 0.151 high 6
## Datsun 710 Datsun 710 2.32 22.8 0.450 high 4
## Hornet 4 Drive Hornet 4 Drive 3.21 21.4 0.217 high 6
## Hornet Sportabout Hornet Sportabout 3.44 18.7 -0.231 low 8
## Valiant Valiant 3.46 18.1 -0.330 low 6
繪制分組排序的條形圖:
ggbarplot(dfm, x = "name", y = "mpg_z",
fill = "mpg_grp",
color = "white",
palette = "jco",
sort.val = "asc",
sort.by.groups = FALSE,
x.text.angle = 90,
ylab = "MPG z-score",
xlab = FALSE,
legend.title = "MPG Group"
)
旋轉(zhuǎn)圖形:
ggbarplot(dfm, 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",
legend.title = "MPG Group",
rotate = TRUE,
ggtheme = theme_minimal()
)
點圖
棒棒糖圖
當你有大量數(shù)據(jù)來展示時肤视,棒棒糖圖與上面所說的條形圖的效果是類似的档痪。
棒棒糖圖的顏色可以根據(jù)分組變量“cyl”確定:
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
sorting = "ascending",
add = "segments",
ggtheme = theme_pubr()
)
旋轉(zhuǎn)并更改點大小:
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
sorting = "descending",
add = "segments",
rotate = TRUE,
group = "cyl",
dot.size = 6,
label = round(dfm$mpg),
font.label = list(color = "white", size = 9,
vjust = 0.5),
ggtheme = theme_pubr()
)
偏差圖
ggdotchart(dfm, x = "name", y = "mpg_z",
color = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
sorting = "descending",
add = "segments",
add.params = list(color = "lightgray", size = 2),
group = "cyl",
dot.size = 6,
label = round(dfm$mpg_z,1),
font.label = list(color = "white", size = 9,
vjust = 0.5),
ggtheme = theme_pubr()
)+
geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
Cleveland點圖
ggdotchart(dfm, 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()
運行環(huán)境
sessionInfo()
R version 3.6.2 (2019-12-12)
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.4 magrittr_1.5 ggplot2_3.2.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 rstudioapi_0.10 tidyselect_0.2.5 munsell_0.5.0
[5] colorspace_1.4-1 R6_2.4.1 rlang_0.4.3 dplyr_0.8.3
[9] tools_3.6.2 grid_3.6.2 gtable_0.3.0 withr_2.1.2
[13] lazyeval_0.2.2 assertthat_0.2.1 digest_0.6.23 tibble_2.1.3
[17] lifecycle_0.1.0 ggsignif_0.6.0 crayon_1.3.4 ggsci_2.9
[21] purrr_0.3.3 farver_2.0.3 glue_1.3.1 labeling_0.3
[25] compiler_3.6.2 pillar_1.4.3 scales_1.1.0 pkgconfig_2.0.3