Themes
参考:ggplot2: Elegant Graphics for Data Analysis (opens new window)
ggplot2的主题系统theme
,主要用来美化图片,控制图片颜色、字体、边距、背景等。
主题系统由四个主要部分组成:
- 主题元素(element, e.g.
plot.title
,axis.ticks.x
) - 元素设置函数(element function, e.g.
element_text()
) theme()
函数,是设置主题的接口。- 完整的主题(e.g.
theme_bw()
,theme_grey()
)
# 1. 修改主题
修改主题的通用公式:
# 直接修改
plot + theme(element.name = element_function())
## 或者,在完整主题基础之上修改
plot + theme_bw() + theme(element.name = element_function())
1
2
3
4
5
2
3
4
5
# 2. 元素设置函数
ggplot2内置了4种基本的元素设置函数: 文本、线条、矩形和空白。每个元素设置函数都有一组用于控制外观的参数。
# 2.1. element_text
element_text()
绘制文本标签和标题。可以控制字体、颜色、大小、hjust、vjust、角度(以度为单位)和行高(作为字体大小的比例)等。
base <- ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +
geom_jitter() +
geom_abline(colour = "grey50", size = 2)
base_t <- base + labs(title = "This is a ggplot") + xlab(NULL) + ylab(NULL)
base_t + theme(plot.title = element_text(size = 16))
base_t + theme(plot.title = element_text(face = "bold", colour = "red"))
base_t + theme(plot.title = element_text(hjust = 1))
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2.2. element_line
element_line()
控制线条颜色、粗细和线型等。
base + theme(panel.grid.major = element_line(colour = "black"))
base + theme(panel.grid.major = element_line(size = 2))
base + theme(panel.grid.major = element_line(linetype = "dotted"))
1
2
3
2
3
# 2.3. element_rect
element_rect()
绘制矩形,主要用于背景设置,控制填充颜色,边框颜色、大小和线型等。
base + theme(plot.background = element_rect(fill = "linen", colour = "skyblue", size = 2, linetype = "dashed"))
1
# 2.3. element_blank
element_blank()
, 用于隐藏元素,且不分配空间。
base
last_plot() + theme(panel.grid.minor = element_blank())
last_plot() + theme(panel.grid.major = element_blank())
1
2
3
2
3
使用
theme_update()
可以起到与base plot 中par()
类似的效果,保存当前设置,在将来绘图中重用。
old_theme <- theme_update(
plot.background = element_rect(fill = "lightblue3", colour = NA),
panel.background = element_rect(fill = "lightblue", colour = NA),
axis.text = element_text(colour = "linen"),
axis.title = element_text(colour = "linen")
)
base
theme_set(old_theme)
base
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 3. 主题元素
ggplot2主题元素可以分为5类:plot, axis, legend, panel and facet.
# 3.1. plot元素
base + theme(plot.background = element_rect(colour = "grey50", size = 2))
base + theme(
plot.background = element_rect(colour = "grey50", size = 2),
plot.margin = margin(20, 20, 20, 20)
)
base + theme(plot.background = element_rect(fill = "lightblue"))
1
2
3
4
5
6
7
2
3
4
5
6
7
# 3.2. axis元素
df <- data.frame(x = 1:3, y = 1:3)
base <- ggplot(df, aes(x, y)) + geom_point()
# Accentuate the axes
base + theme(axis.line = element_line(colour = "grey50", size = 1))
# Style both x and y axis labels
base + theme(axis.text = element_text(color = "blue", size = 12))
# Useful for long labels
base + theme(axis.text.x = element_text(angle = -90, vjust = 0.5))
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 3.3. legend元素
df <- data.frame(x = 1:4, y = 1:4, z = rep(c("a", "b"), each = 2))
base <- ggplot(df, aes(x, y, colour = z)) + geom_point()
base + theme(
legend.background = element_rect(
fill = "lemonchiffon",
colour = "grey50",
size = 1
)
)
base + theme(
legend.key = element_rect(color = "grey50"),
legend.key.width = unit(0.9, "cm"),
legend.key.height = unit(0.75, "cm")
)
base + theme(
legend.text = element_text(size = 15),
legend.title = element_text(size = 15, face = "bold")
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 3.4. panel元素
base <- ggplot(df, aes(x, y)) + geom_point()
# Modify background
base + theme(panel.background = element_rect(fill = "lightblue"))
# Tweak major grid lines
base + theme(
panel.grid.major = element_line(color = "gray60", size = 0.8)
)
# Just in one direction
base + theme(
panel.grid.major.x = element_line(color = "gray60", size = 0.8)
)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 3.5. facet元素
df <- data.frame(x = 1:4, y = 1:4, z = c("a", "a", "b", "b"))
base_f <- ggplot(df, aes(x, y)) + geom_point() + facet_wrap(~z)
base_f
base_f + theme(panel.spacing = unit(0.5, "in"))
base_f + theme(
strip.background = element_rect(fill = "grey20", color = "grey80", size = 1),
strip.text = element_text(colour = "white")
)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 4. 完整主题
- theme_grey: ggplot2默认主题
- theme_bw: 与theme_grey类似,白色背景
- theme_linedraw
- theme_light
- theme_dark
- theme_minimal
- theme_classic: 常用,类似base plot
- theme_void: 空主题
# 5. theme_Publication
theme_Publication <- function(base_size=14, base_family="sans") {
library(grid)
library(ggthemes)
(theme_foundation(base_size=base_size, base_family=base_family)
+ theme(plot.title = element_text(face = "bold",
size = rel(1.2), hjust = 0.5),
text = element_text(),
panel.background = element_rect(colour = NA),
plot.background = element_rect(colour = NA),
panel.border = element_rect(colour = NA),
axis.title = element_text(face = "bold",size = rel(1)),
axis.title.y = element_text(angle=90,vjust =2),
axis.title.x = element_text(vjust = -0.2),
axis.text = element_text(),
axis.line.x = element_line(colour="black"),
axis.line.y = element_line(colour="black"),
axis.ticks = element_line(),
panel.grid.major = element_line(colour="#f0f0f0"),
panel.grid.minor = element_blank(),
legend.key = element_rect(colour = NA),
legend.position = "bottom",
legend.direction = "horizontal",
legend.key.size= unit(0.2, "cm"),
legend.margin = unit(0, "cm"),
legend.title = element_text(face="italic"),
plot.margin=unit(c(10,5,5,5),"mm"),
strip.background=element_rect(colour="#f0f0f0",fill="#f0f0f0"),
strip.text = element_text(face="bold")
))
}
scale_fill_Publication <- function(...){
library(scales)
discrete_scale("fill","Publication",manual_pal(values = c("#386cb0","#fdb462","#7fc97f","#ef3b2c","#662506","#a6cee3","#fb9a99","#984ea3","#ffff33")), ...)
}
scale_colour_Publication <- function(...){
library(scales)
discrete_scale("colour","Publication",manual_pal(values = c("#386cb0","#fdb462","#7fc97f","#ef3b2c","#662506","#a6cee3","#fb9a99","#984ea3","#ffff33")), ...)
}
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
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
使用方法:
source("https://cdn.jsdelivr.net/gh/koundy/ggplot_theme_Publication/R/ggplot_theme_Publication.R")
ggplot(mtcars, aes(wt, mpg, color = as.factor(cyl))) +
geom_point(size = 3) +
theme_Publication()
1
2
3
4
2
3
4
编辑 (opens new window)
上次更新: 2021/07/12, 11:19:29