A R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

A.1 插图

Base R 绘图系统不太容易独立地设置坐标轴标签和刻度标签的字体

layout(matrix(c(1, 2), nrow = 1, byrow = TRUE), c(1, 1))
par(family = "serif", mar = c(4.1, 4.1, 0.5, 0.5)) 
plot(pressure, family = "sans")
par(family = "sans") 
plot(pressure, xlab = "温度", ylab = "压力", family = "GB1")
text(x = 50, y = 600, labels = "散点图", family = "GB1")
在 Base R 绘图系统中设置中英文字体,当 family =

图 A.1: 在 Base R 绘图系统中设置中英文字体,当 family = "serif" 在 Windows 环境下会调用 TimesNewRomanPSMT 字体,在 Linux 系统上调用 NimbusRomNo9L-Regu 字体,设置 family = "sans" 在 Windows 平台上会调用 ArialMT 字体,更多说明见 https://github.com/r-lib/systemfonts#system-defaults

ggplot2 系统可以更加精细地调整字体设置

library(ggplot2)
p1 <- ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_point()
p2 <- p1 + theme(
  axis.title = element_text(family = "sans"),
  axis.text = element_text(family = "serif")
)
p3 <- p1 + labs(x = "温度", y = "压力") +
  theme(
    axis.title = element_text(family = "GB1"),
    axis.text = element_text(family = "serif")
  )
p4 <- p1 + labs(
  x = "温度", y = "压力", title = "散点图",
  subtitle = "Vapor Pressure of Mercury as a Function of Temperature",
  caption = paste("Data on the relation between temperature in degrees Celsius and",
    "vapor pressure of mercury in millimeters (of mercury).",
    sep = "\n"
  )
) +
  theme(
    axis.title = element_text(family = "GB1"),
    axis.text.x = element_text(family = "serif"),
    axis.text.y = element_text(family = "sans"),
    title = element_text(family = "GB1"),
    plot.subtitle = element_text(family = "sans", size = rel(0.7)),
    plot.caption = element_text(family = "sans", size = rel(0.6))
  )
library(cowplot)
plot_grid(p1, p2, p3, p4,
  labels = c(
    "默认字体设置", "英文字体设置",
    "中文字体设置", "任意字体设置"
  ), label_fontfamily = "GB1", ncol = 2,
  label_x = 0.1, label_y = 0.6
)
在 ggplot2 绘图系统中,字体可以非常容易地做到精细的设置,轴的标题、标签和图的主、副标题等都可以设置不同的字体、字号和字样

图 A.2: 在 ggplot2 绘图系统中,字体可以非常容易地做到精细的设置,轴的标题、标签和图的主、副标题等都可以设置不同的字体、字号和字样

A.2 插表