创建及自定义ggplot分面图形参数

简介

ggplot2有一个十分重要的功能就是分面,而今天要讲得R包ggpubr中也有一个函数facet()专门用来进行分面操作。

安装

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

绘图

library(ggpubr)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df)
##    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 <- ggdensity(df, x="len", fill = "dose", palette = "jco", ggtheme = theme_light(), legend="top")
p

分面

水平方向分面

按照变量supp进行水平方向的分面

facet(p, facet.by = "supp")

竖直方向分面

按照变量supp进行竖直方向的分面

facet(p, facet.by = "supp", ncol = 1)

双变量分面

#divide with "supp" vertical, "dose" horizontal
facet(p, facet.by = c("supp", "dose"), short.panel.labs = FALSE)

修改面板外观

主要有以下参数进行修改:

  • short.panel.label:默认为TRUE,此时变量名不会显示
  • panel.label: 通过自定义label来设置面板label,是一个list
  • panel.labs.background: 控制面板背景,里面参数设置很多
  • panel.labs.font: 设置字体

下面演示一下

#divide with "supp" vertical, "sode" horizontal
facet(p, facet.by = c("supp", "dose"),
      panel.labs = list(
        supp=c("Orange Juice", "Vitamin C"),
        dose=c("D0.5", "D1", "D2")),
      panel.labs.background = list(
        color="steelblue", 
        fill="steelblue",
        size=0.5),
      panel.labs.font = list(color="white"),
      panel.labs.font.x = list(angle=45, color="white"))

SessionInfo

sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.3 LTS
## 
## Matrix products: default
## BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
## LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0
## 
## locale:
##  [1] LC_CTYPE=zh_CN.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=zh_CN.UTF-8        LC_COLLATE=zh_CN.UTF-8    
##  [5] LC_MONETARY=zh_CN.UTF-8    LC_MESSAGES=zh_CN.UTF-8   
##  [7] LC_PAPER=zh_CN.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggpubr_0.1.5  magrittr_1.5  ggplot2_2.2.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.12     bindr_0.1        knitr_1.17       munsell_0.4.3   
##  [5] colorspace_1.3-2 R6_2.2.2         rlang_0.1.2      stringr_1.2.0   
##  [9] plyr_1.8.4       dplyr_0.7.3      tools_3.4.1      grid_3.4.1      
## [13] gtable_0.2.0     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.4    
## [21] bindrcpp_0.2     ggsci_2.7        reshape2_1.4.2   purrr_0.2.3     
## [25] glue_1.1.1       evaluate_0.10.1  rmarkdown_1.6    labeling_0.3    
## [29] stringi_1.1.5    compiler_3.4.1   scales_0.5.0     backports_1.1.0 
## [33] pkgconfig_2.0.1
Researcher

I am a PhD student of Crop Genetics and Breeding at the Zhejiang University Crop Science Lab. My research interests covers a range of issues:Population Genetics Evolution and Ecotype Divergence Analysis of Oilseed Rape, Genome-wide Association Study (GWAS) of Agronomic Traits.

comments powered by Disqus