利用ggpubr包进行R语言可视化

Hadley Wickham创建的可视化包ggplot2可以流畅地进行优美的可视化,但是如果要通过ggplot2定制一套图形,尤其是适用于杂志期刊等出版物的图形,对于那些没有深入了解ggplot2的人来说就有点困难了,ggplot2的部分语法是很晦涩的。为此Alboukadel Kassambara创建了基于ggplot2的可视化包ggpubr用于绘制符合出版物要求的图形。

安装及加载ggpubr包

安装方式有两种:

  • 直接从CRAN安装:
  1. install.packages("ggpubr")

 

  • 从GitHub上安装最新版本:
  1. if(!require(devtools))
  2. install.packages("devtools")
  3. devtools::install_github("kassambara/ggpubr")

安装完之后直接加载就行:

  1. library(ggpubr)

ggpubr可绘制图形

ggpubr可绘制大部分我们常用的图形,下面一一介绍。

分布图(Distribution)

  1. #构建数据集set.seed(1234) 
  2.  
  3. df <- data.frame( sex=factor(rep(c("f", "M"), each=200)), weight=c(rnorm(200, 55), rnorm(200, 58))) 
  4. head(df)
  5. ##   sex   weight
  6. ## 1  f   53.79293
  7. ## 2  f   55.27743
  8. ## 3  f   56.08444
  9. ## 4  f   52.65430
  10. ## 5  f   55.42912
  11. ## 6  f   55.50606

密度分布图以及边际地毯线并添加平均值线

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

利用ggpubr包进行R语言可视化-图片1

带有均值线和边际地毯线的直方图

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

利用ggpubr包进行R语言可视化-图片2

箱线图与小提琴图

  1. #加载数据集ToothGrowth
  2.  
  3. data("ToothGrowth") 
  4. df1 <- ToothGrowth head(df1)
  5. ##    len  supp  dose
  6. ## 1  4.2   VC    0.5
  7. ## 2  11.5  VC    0.5
  8. ## 3  7.3   VC    0.5
  9. ## 4  5.8   VC    0.5
  10. ## 5  6.4   VC    0.5
  11. ## 6  10.0  VC    0.5
  12. p <- ggboxplot(df1, x="dose", y="len", color = "dose", palette = c("#00AFBB", "#E7B800", "#FC4E07"), add = "jitter", shape="dose")#增加了jitter点,点shapedose映射p

 

利用ggpubr包进行R语言可视化-图片3

增加不同组间的p-value值,可以自定义需要标注的组间比较

  1. 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)

 

利用ggpubr包进行R语言可视化-图片4

内有箱线图的小提琴图

  1. ggviolin(df1, x="dose", y="len", fill = "dose", 
  2. palette = c("#00AFBB", "#E7B800", "#FC4E07"), 
  3. add = "boxplot", add.params = list(fill="white"))+ stat_compare_means(comparisons = my_comparisons, label ="p.signif") + stat_compare_means(label.y = 50)#label这里表示选择显著性标记(星号)

利用ggpubr包进行R语言可视化-图片5

条形图

  1. data("mtcars") 
  2.  
  3. df2 <- mtcars 
  4.  
  5. df2$cyl <- factor(df2$cyl)
  6.  
  7. df2$name <- rownames(df2)#添加一行name
  8.  
  9. head(df2[, c("name", "wt", "mpg", "cyl")])

利用ggpubr包进行R语言可视化-图片6

按从小到大顺序绘制条形图(不分组排序)

  1. ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",
  2.  
  3. palette = "jco",#杂志jco的配色
  4.  
  5. sort.val = "desc",#下降排序
  6.  
  7. sort.by.groups=FALSE,#不按组排序
  8.  
  9. x.text.angle=60)

 

利用ggpubr包进行R语言可视化-图片7

按组进行排序

  1. ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",
  2.  
  3. palette = "jco",#杂志jco的配色
  4.  
  5. sort.val = "asc",#上升排序,区别于desc,具体看图演示
  6.  
  7. sort.by.groups=TRUE,#按组排序
  8.  
  9. x.text.angle=90)

利用ggpubr包进行R语言可视化-图片8

偏差图

偏差图展示了与参考值之间的偏差

  1. df2$mpg_z <- (df2$mpg-mean(df2$mpg))/sd(df2$mpg) 
  2.  
  3. df2$mpg_grp <- factor(ifelse(df2$mpg_z<0, "low", "high"), levels = c("low", "high")) 
  4.  
  5. head(df2[, c("name", "wt", "mpg", "mpg_grp", "cyl")])

利用ggpubr包进行R语言可视化-图片9

绘制排序过的条形图

  1. ggbarplot(df2, x="name", y="mpg_z", fill = "mpg_grp", color = "white", 
  2.  
  3. palette = "jco", sort.val = "asc", sort.by.groups = FALSE, x.text.angle=60, ylab = "MPG z-score", xlab = FALSE, legend.title="MPG Group")

利用ggpubr包进行R语言可视化-图片10

坐标轴变换

  1. ggbarplot(df2, x="name", y="mpg_z", fill = "mpg_grp", color = "white", 
  2.  
  3. palette = "jco", sort.val = "desc", sort.by.groups = FALSE, 
  4.  
  5. x.text.angle=90, ylab = "MPG z-score", xlab = FALSE, 
  6.  
  7. legend.title="MPG Group", rotate=TRUE, ggtheme = theme_minimal())

利用ggpubr包进行R语言可视化-图片11

点图(Dot charts)

棒棒糖图(Lollipop chart)

棒棒图可以代替条形图展示数据

  1. ggdotchart(df2, x="name", y="mpg", color = "cyl", 
  2.  
  3. palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "ascending", 
  4.  
  5. add = "segments", ggtheme = theme_pubr())

利用ggpubr包进行R语言可视化-图片12

可以自设置各种参数

  1. ggdotchart(df2, x="name", y="mpg", color = "cyl", 
  2.  
  3. palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending", 
  4.  
  5. add = "segments", rotate = TRUE, group = "cyl", dot.size = 6, 
  6.  
  7. label = round(df2$mpg), font.label = list(color="white", size=9, vjust=0.5), 
  8.  
  9. ggtheme = theme_pubr())

利用ggpubr包进行R语言可视化-图片13

偏差图

  1. ggdotchart(df2, x="name", y="mpg_z", color = "cyl", 
  2.  
  3. palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending", 
  4.  
  5. add = "segment", add.params = list(color="lightgray", size=2), 
  6.  
  7. group = "cyl", dot.size = 6, label = round(df2$mpg_z, 1), 
  8.  
  9. font.label = list(color="white", size=9, vjust=0.5),
  10.  
  11. ggtheme = theme_pubr())+ geom_line(yintercept=0, linetype=2, color="lightgray")

利用ggpubr包进行R语言可视化-图片14

Cleveland点图

  1. ggdotchart(df2, x="name", y="mpg", color = "cyl", 
  2.  
  3. palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  4.  
  5. sorting = "descending", rotate = TRUE, dot.size = 2, y.text.col=TRUE, 
  6.  
  7. ggtheme = theme_pubr())+ theme_cleveland()

利用ggpubr包进行R语言可视化-图片15

SessionInfo

  1. sessionInfo()
  2.  
  3. ## R version 3.4.0 (2017-04-21)
  4.  
  5. ## Platform: x86_64-w64-mingw32/x64 (64-bit)
  6.  
  7. ## Running under: Windows 8.1 x64 (build 9600)
  8.  
  9. ##
  10.  
  11. ## Matrix products: default
  12.  
  13. ##
  14.  
  15. ## locale:
  16.  
  17. ## [1] LC_COLLATE=Chinese (Simplified)_China.936
  18.  
  19. ## [2] LC_CTYPE=Chinese (Simplified)_China.936
  20.  
  21. ## [3] LC_MONETARY=Chinese (Simplified)_China.936
  22.  
  23. ## [4] LC_NUMERIC=C
  24.  
  25. ## [5] LC_TIME=Chinese (Simplified)_China.936
  26.  
  27. ##
  28.  
  29. ## attached base packages:
  30.  
  31. ## [1] stats graphics grDevices utils datasets methods base
  32.  
  33. ##
  34.  
  35. ## other attached packages:
  36.  
  37. ## [1] ggpubr_0.1.3 magrittr_1.5 ggplot2_2.2.1
  38.  
  39. ##
  40.  
  41. ## loaded via a namespace (and not attached):
  42.  
  43. ## [1] Rcpp_0.12.11 knitr_1.16 munsell_0.4.3 colorspace_1.3-2
  44.  
  45. ## [5] R6_2.2.1 rlang_0.1.1 stringr_1.2.0 plyr_1.8.4
  46.  
  47. ## [9] dplyr_0.5.0 tools_3.4.0 grid_3.4.0 gtable_0.2.0
  48.  
  49. ## [13] DBI_0.6-1 htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0
  50.  
  51. ## [17] rprojroot_1.2 digest_0.6.12 assertthat_0.2.0 tibble_1.3.3
  52.  
  53. ## [21] ggsignif_0.2.0 ggsci_2.4 purrr_0.2.2.2 evaluate_0.10
  54.  
  55. ## [25] rmarkdown_1.5 labeling_0.3 stringi_1.1.5 compiler_3.4.0
  56.  
  57. ## [29] scales_0.4.1 backports_1.1.0

 

发表评论

匿名网友

拖动滑块以完成验证
加载失败