This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Chinese-R-Markdown-Template.Rmd
154 lines (132 loc) · 4.19 KB
/
Chinese-R-Markdown-Template.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
title: "中文 R Markdown 文档"
author: "黄湘云"
date: "`r Sys.Date()`"
output:
pdf_document:
extra_dependencies:
ctex: UTF8
sourcecodepro: scale = 0.85
sourceserifpro: default
fig_caption: yes
latex_engine: xelatex
number_sections: yes
classoptions: "preview"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
echo = TRUE,
cache = FALSE,
out.width = "70%",
fig.align = "center",
fig.width = 6,
fig.asp = 0.618, # 1 / phi
fig.show = "hold",
fig.showtext = TRUE
)
lapply(c("ggplot2", "showtext", "tikzDevice"), function(pkg) {
if (system.file(package = pkg) == "") install.packages(pkg)
})
# library(showtext)
# Install Source Han Sans font (by default Simplified Chinese)
# showtext::font_install(source_han_serif("CN")) # 思源宋体
# showtext::font_install(source_han_sans("CN")) # 思源黑体
```
# 字体
## 中文
```{r}
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(colour = Species)) +
scale_colour_brewer(palette = "Set1") +
labs(
title = "鸢尾花数据的散点图",
x = "萼片长度", y = "萼片宽度", colour = "鸢尾花类别"
) +
theme_minimal(base_size = 10.54, base_family = "wqy-microhei")
```
图形里的字体,以矿大学位论文的要求为例,
1. 字体宋体,五号
2. 数字、字母字体都是 Times New Roman
3. 5 号字体对应 10.54pt,宋体此处采用思源宋体
```{r}
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(colour = Species)) +
scale_colour_brewer(palette = "Set1") +
labs(
title = "鸢尾花数据的散点图",
x = "萼片长度", y = "萼片宽度", colour = "鸢尾花类别"
) +
theme_minimal(base_size = 10.54, base_family = "source-han-serif-cn") +
theme(
legend.text = element_text(family = "serif", size = 10.54),
axis.text = element_text(family = "serif", size = 10.54)
)
```
简单起见,字体为思源宋体,字号5号,英文和数字随思源宋体,不再另外设置,其实看不出来差别
```{r}
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(colour = Species)) +
scale_colour_brewer(palette = "Set1") +
labs(
title = "鸢尾花数据的散点图",
x = "萼片长度", y = "萼片宽度", colour = "鸢尾花类别"
) +
theme_minimal(base_size = 10.54, base_family = "source-han-serif-cn")
```
自定义中文字体
1. 标题:思源宋体加粗
2. 坐标轴标签:思源宋体
3. 图例:思源黑体
```{r}
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(colour = Species)) +
scale_colour_brewer(palette = "Set1") +
labs(
title = "鸢尾花数据的散点图",
x = "萼片长度", y = "萼片宽度", colour = "鸢尾花类别"
) +
theme(
title = element_text(family = "source-han-sans-cn", face = "plain", size = 10.54),
axis.title = element_text(family = "source-han-serif-cn", face = "plain", size = 10.54),
legend.title = element_text(family = "source-han-serif-cn", face = "plain", size = 10.54),
axis.ticks = element_line(colour = "black"),
legend.text = element_text(family = "serif", size = 10.54),
axis.text = element_text(family = "serif", size = 10.54),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size = 0.2)
)
```
## 英文
```{r fig.cap="西文衬线字体"}
op <- par(family = "serif", mar = c(4, 4, .5, .5))
plot(pressure)
par(op)
```
```{r eval=FALSE}
library(showtext)
showtext_auto()
cairo_pdf(file = "demo_times.pdf", width = 8, height = 7, pointsize = 16)
pdf(file = "demo_times.pdf", width = 8, height = 7, pointsize = 16)
op <- par(mar = c(4, 4, .5, .5))
plot(pressure, xlab = "", ylab = "", family = "Times") # Times
# plot(pressure, xlab = "", ylab = "", family = "sans") # arial
title(xlab = "温度", ylab = "压力", family = "source-han-serif-cn")
par(op)
dev.off()
```
```{r fig.cap="Times New Roman字体"}
op <- par(mar = c(4, 4, .5, .5))
plot(pressure, xlab = "", ylab = "", family = "serif") # Times
par(op)
```
pdf 设备可用字体
```{r}
names(pdfFonts())
```
- https://github.com/wch/fontcm
- https://github.com/wch/extrafont