Chapter 2 ggplot主题使用

2.1 ggplot作图字体及线条单位

pointsize 是绘图点大小,单位是 1/72 英寸,一般情况下不会修改pointsize大小。

1 in = 25.4mm = 72.27points,1mm = 2.845276 points

1 mm = .pt = 2.845276 points .pt为R语言作图的最小单位。

ggplot2函数用法规定: 字体单位为pt, 线条单位为mm.

library(ggplot2)
.pt
## [1] 2.845276
#设置1.5pt线宽, ggplot2中线条宽度单位为mm
lwd = 1.5/.pt
lwd
## [1] 0.5271897

2.2 theme_grey默认参数

theme_grey的默认参数, 包含了theme所有的默认设定, 可以帮助理解ggplot的theme参数.

# ggplot默认主题及默认参数
theme_grey <- function (base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22) {
    half_line <- base_size/2
    t <- theme(line = element_line(colour = "black", size = base_line_size, linetype = 1, lineend = "butt"), 
               rect = element_rect(fill = "white", colour = "black", size = base_rect_size, linetype = 1), 
               text = element_text(family = base_family, face = "plain", colour = "black", size = base_size, lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE), 
               axis.line = element_blank(), 
               axis.line.x = NULL, 
               axis.line.y = NULL, 
               axis.text = element_text(size = rel(0.8), colour = "grey30"), 
               axis.text.x = element_text(margin = margin(t = 0.8 * half_line/2), vjust = 1), 
               axis.text.x.top = element_text(margin = margin(b = 0.8 * half_line/2), vjust = 0), 
               axis.text.y = element_text(margin = margin(r = 0.8 * half_line/2), hjust = 1), 
               axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line/2), hjust = 0), 
               axis.ticks = element_line(colour = "grey20"), 
               axis.ticks.length = unit(half_line/2, "pt"), axis.ticks.length.x = NULL, 
               axis.ticks.length.x.top = NULL, axis.ticks.length.x.bottom = NULL, 
               axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, 
               axis.ticks.length.y.right = NULL, 
               axis.title.x = element_text(margin = margin(t = half_line/2), vjust = 1), 
               axis.title.x.top = element_text(margin = margin(b = half_line/2), vjust = 0), 
               axis.title.y = element_text(angle = 90, margin = margin(r = half_line/2), vjust = 1), 
               axis.title.y.right = element_text(angle = -90, margin = margin(l = half_line/2), vjust = 0), 
               legend.background = element_rect(colour = NA), 
               legend.spacing = unit(2 * half_line, "pt"), 
               legend.spacing.x = NULL, 
               legend.spacing.y = NULL, 
               legend.margin = margin(half_line, half_line, half_line, half_line), 
               legend.key = element_rect(fill = "grey95", colour = NA), 
               legend.key.size = unit(1.2, "lines"), 
               legend.key.height = NULL, 
               legend.key.width = NULL, 
               legend.text = element_text(size = rel(0.8)), 
               legend.text.align = NULL, 
               legend.title = element_text(hjust = 0), 
               legend.title.align = NULL, 
               legend.position = "right", 
               legend.direction = NULL, 
               legend.justification = "center", 
               legend.box = NULL, 
               legend.box.margin = margin(0, 0, 0, 0, "cm"), 
               legend.box.background = element_blank(), 
               legend.box.spacing = unit(2 * half_line, "pt"), 
               panel.background = element_rect(fill = "grey92", colour = NA), 
               panel.border = element_blank(), 
               panel.grid = element_line(colour = "white"), 
               panel.grid.minor = element_line(size = rel(0.5)), 
               panel.spacing = unit(half_line, "pt"), 
               panel.spacing.x = NULL, 
               panel.spacing.y = NULL, 
               panel.ontop = FALSE, 
               strip.background = element_rect(fill = "grey85", colour = NA), 
               strip.text = element_text(colour = "grey10", size = rel(0.8), 
                                         margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)), 
               strip.text.x = NULL, strip.text.y = element_text(angle = -90), 
               strip.text.y.left = element_text(angle = 90), 
               strip.placement = "inside", 
               strip.placement.x = NULL, 
               strip.placement.y = NULL, 
               strip.switch.pad.grid = unit(half_line/2, "pt"), 
               strip.switch.pad.wrap = unit(half_line/2, "pt"), 
               plot.background = element_rect(colour = "white"), 
               plot.title = element_text(size = rel(1.2), hjust = 0, vjust = 1, margin = margin(b = half_line)), 
               plot.title.position = "panel", 
               plot.subtitle = element_text(hjust = 0, vjust = 1, margin = margin(b = half_line)), 
               plot.caption = element_text(size = rel(0.8), hjust = 1, vjust = 1, margin = margin(t = half_line)), 
               plot.caption.position = "panel", 
               plot.tag = element_text(size = rel(1.2), hjust = 0.5, vjust = 0.5), 
               plot.tag.position = "topleft", 
               plot.margin = margin(half_line, half_line, half_line, half_line), complete = TRUE)
    ggplot_global$theme_all_null %+replace% t
}

2.3 theme_bw默认参数

  1. 修改默认字体大小, 默认线条粗细, 默认边框粗细.
  2. 去除作图背景颜色.(原有背景为灰色)
  3. 调整主坐标轴, 次坐标轴线条颜色为grey92; 次坐标轴线体修改为原来的一半.
  4. 调整图例的背景为白色.

为什么选择theme_bw主题, 是因为paper发表文章, theme_bw经过修改之后比较简洁.

# ggplot theme_bw主题调节内容
theme_bw <- function (base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22) {
    theme_grey(base_size = base_size, 
               base_family = base_family, 
               base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace% 
    theme(panel.background = element_rect(fill = "white", colour = NA), 
          panel.border = element_rect(fill = NA, colour = "grey20"), 
          panel.grid = element_line(colour = "grey92"), 
          panel.grid.minor = element_line(size = rel(0.5)), 
          strip.background = element_rect(fill = "grey85", colour = "grey20"), 
          legend.key = element_rect(fill = "white", colour = NA), complete = TRUE)
}

2.4 theme_bw主题修改

  1. 坐标轴字体调整为黑色;
  2. 坐标轴线调整为黑色

ggplot2函数用法规定: base_size单位为pt, base_line_size和base_rect_size单位为mm.

文章发表字体大小6-12pt, 线条粗细0.5pt ~ 1.5pt, 由于线条单位默认为mm,因此要除以.pt

theme_bw1 <- function (base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22) 
{
    theme_grey(base_size = base_size, 
               base_family = base_family, 
               base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace% 
    theme(panel.background = element_rect(fill = "white", colour = NA), 
          panel.border = element_rect(fill = NA, colour = "black"), 
          panel.grid = element_line(colour = "grey92"), 
          panel.grid.minor = element_line(size = rel(0.5)), 
          strip.background = element_rect(fill = "grey85", colour = "grey20"), 
          legend.key = element_rect(fill = "white", colour = NA), complete = TRUE,
          axis.text = element_text(size = rel(0.8), colour = "black"), )
}


p + theme_bw1(base_size = 12,base_line_size = 1.5/.pt,base_rect_size = 1.5/.pt)

2.5 Figure 保存技巧

在Rstudio的Plots Panel或R语言中调节合适的figure大小,Rstudio默认输出的图片dpi为96,因此显示的(像素大小/96)即为图片的物理尺寸,我们可以根据右侧图片中字体大小,线条粗细和绘图区点等的大小实时调整,调整完后,设置高度及宽度,示例如下:

ggsave("test.png", g, width = 579/96, height =304/96,units = "in",dpi=300)
#578, 304为在Rstudio中显示图片的分辨率; 96为dpi, 比值为物理尺寸大小"in", 一般paper要求ppi高于300。

2.7 包版本

## R version 3.6.3 (2020-02-29)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_3.3.6
## 
## loaded via a namespace (and not attached):
##  [1] bslib_0.4.0      compiler_3.6.3   pillar_1.8.1     jquerylib_0.1.4 
##  [5] tools_3.6.3      digest_0.6.29    jsonlite_1.8.0   evaluate_0.16   
##  [9] lifecycle_1.0.1  tibble_3.1.8     gtable_0.3.1     pkgconfig_2.0.3 
## [13] rlang_1.1.0      DBI_1.1.3        cli_3.4.1        rstudioapi_0.14 
## [17] yaml_2.3.5       xfun_0.33        fastmap_1.1.0    withr_2.5.0     
## [21] stringr_1.4.1    dplyr_1.0.10     knitr_1.40       generics_0.1.3  
## [25] sass_0.4.2       vctrs_0.4.1      tidyselect_1.1.2 grid_3.6.3      
## [29] glue_1.6.2       R6_2.5.1         fansi_1.0.3      rmarkdown_2.16  
## [33] bookdown_0.30    purrr_0.3.4      magrittr_2.0.3   scales_1.2.1    
## [37] htmltools_0.5.3  assertthat_0.2.1 colorspace_2.0-3 utf8_1.2.2      
## [41] stringi_1.7.8    munsell_0.5.0    cachem_1.0.6