0%

ggplot2集锦之五theme

本文总结一下ggplot2包绘图时主题的美化。

1.theme主题

ggplot2有一些自带的常用的主题,如图:theme_bw()、默认背景theme_grey()、theme_linedraw()、theme_light()、theme_mininal()、theme_classic()、theme_void()。
alt 图标

theme也可以自定义。可以将自己喜欢的theme()部分保存为变量,重复调用。

1
2
3
mytheme <- theme(...)
ggplot(...)+
mytheme

常见的四种类型:
element_text(),对文本进行设置,如title、subtitle等。
element_line(),对线进行设置,如轴线、主网格线、次网格线等等。
element_rect(),修改基于矩形的组件,如绘图区域和面板区域的背景。
element_blank(),不绘制任何东西,并关闭相关的内容设置。

1
ggplot变量+theme(组件=element_功能())

例:

1
2
3
4
data(diamonds)
set.seed(1234)
diamond <- diamonds[sample(nrow(diamonds), 2000), ]
p <- ggplot(data = diamond) +geom_point(aes(x=carat, y=price, colour=color,shape=cut)) + labs(title="TITLE",subtitle = "SUBTITLE",caption = "CAPTION")

alt 图标

theme()函数内容:

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
theme(
line,rect,text,title,
aspect.ratio,
axis.title,axis.title.x,axis.title.x.top,axis.title.x.bottom,
axis.title.y,axis.title.y.left,axis.title.y.right,
axis.text,axis.text.x,axis.text.x.top,axis.text.x.bottom,
axis.text.y,axis.text.y.left,axis.text.y.right,
axis.ticks,
axis.ticks.x,axis.ticks.x.top,axis.ticks.x.bottom,
axis.ticks.y,axis.ticks.y.left,axis.ticks.y.right,
axis.ticks.length,
axis.ticks.length.x,axis.ticks.length.x.top,axis.ticks.length.x.bottom,
axis.ticks.length.y,axis.ticks.length.y.left,axis.ticks.length.y.right,
axis.line,
axis.line.x,axis.line.x.top,axis.line.x.bottom,
axis.line.y,axis.line.y.left,axis.line.y.right,
legend.background,
legend.margin,
legend.spacing,legend.spacing.x,legend.spacing.y,
legend.key,legend.key.size,legend.key.height,legend.key.width,
legend.text,legend.text.align,
legend.title,legend.title.align,
legend.position,
legend.direction,
legend.justification,
legend.box,
legend.box.just,legend.box.margin,legend.box.background,legend.box.spacing,
panel.background,panel.border,
panel.spacing,panel.spacing.x,panel.spacing.y,
panel.grid,
panel.grid.major,panel.grid.major.x,panel.grid.major.y,
panel.grid.minor,panel.grid.minor.x,panel.grid.minor.y,
panel.ontop,
plot.background,
plot.title,plot.title.position,
plot.subtitle,
plot.caption,plot.caption.position,
plot.tag,
plot.tag.position,
plot.margin,
strip.background,strip.background.x,strip.background.y,
strip.clip,
strip.placement,
strip.text,strip.text.x,strip.text.y,
strip.switch.pad.grid,
strip.switch.pad.wrap,
...,
complete = FALSE,
validate = TRUE
)

2. 修改背景、标题,坐标轴

2.1 图像组件

组件 对应的element功能 描述
plot.title element_rect() 图像标题
plot.background element_text() 图像背景
plot.margin margin() 图像边距

plot.background=element_rect(fill = NA)可以将背景矩形设为透明。

2.2 标题组件

组件 对应的element功能 描述
plot.title element_text() 图片标题
plot.subtitle element_text() 图片副标题
plot.caption elemnet_text() 图片caption

2.3 坐标轴组件

组件 对应的element功能 描述
axis.title element_text() 坐标轴标题
axis.title.x element_text() x轴标题
axis.title.y element_text() y轴标题
axis.text element_text() 坐标轴文本
axis.text.x element_text() x轴文本
axis.text.y element_text() y轴文本
axis.ticks element_text() 轴须
axis.ticks.x element_text() x轴轴须
axis.ticks.y element_text() y轴轴须
axis.line element_line() 轴线

axis.line轴线处理,默认是隐藏的。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
p + theme(plot.title=element_text(size=20, face="bold", #字体
color="skyblue", #颜色
hjust=0.5, #调整位置,正中间(水平)
lineheight=1.2),
plot.subtitle=element_text(size=15,face="bold",
color = "red",hjust=0.5), # subtitle
plot.caption=element_text(size=15), # caption
axis.title.x=element_text(vjust=1, size=20), # X axis title
axis.title.y=element_text(size=10, color = "blue"), # Y axis title
axis.text.x=element_text(size=10, angle = 45,
color = "red", vjust=.5), # X axis text
axis.text.y=element_text(size=10)) # Y axis text

alt 图标

一些其他参数、函数:
vjust,控制标题(或标签)和绘图之间的垂直间距。
hjust,控制水平间距。将其设置为0.5将标题居中。
face,设置字体(“plain”,“italic”,“bold”,“bold.italic”)
unit(),用于设定网格单位,如unit(1,”cm”)或unit(0.25,”in”)

2. 修改图例

图例组件:

组件 对应的element功能 描述
legend.background element_rect() 图例背景
legend.key element_rect() 图例符号背景
legend.key.size unit() 图例符号大小
legend.key.height unit() 图例符号高度
legend.key.width unit() 图例符号宽度
legend.margin unit() 图例边距
legend.text element_text() 图例标签
legend.text.align 0-1 图例标签对齐(0=右,1=左)
legend.title element_text() 图例名
legend.title.align 0-1 图例名对齐(0=右,1=左)
legend.justification c(x,y)图例的x和y的坐标 将图例设置在图内部
legend.position “none”, “left”, “right”, “bottom”, “top”,or two-element numeric vector 图例位置

2.1 设置图例标题,文本和键的样式

例如:

1
2
3
4
5
6
p + theme(legend.title = element_text(size=15, color = "firebrick"), # 图例名字
legend.text = element_text(size=10), # 图例文本
legend.key=element_rect(fill='green'), # 图例符号背景
legend.background = element_rect(fill = "red"), #图例背景
legend.key.size=unit(1,"cm"), # 图例符号大小
legned.text.align=1) # 图例文本右对齐

alt 图标

2.2 删除图例和修改图例位置

删掉图例:

1
p + theme(legend.position="None") + labs(subtitle="No Legend")

alt 图标

修改图例位置:

1
p + theme(legend.position="bottom", legend.box = "horizontal") + labs(subtitle="Legend bottom")

alt 图标

修改图例到图片内部:

1
2
3
4
5
6
p + theme(legend.title = element_text(size=12, color = "salmon", face="bold"),
legend.justification=c(1,0),
legend.position=c(0.95, 0.05), #c(0, 1)为面板左上角,c(1, 0)为面板右下角,c(0.5, 0,5)为面板中间
legend.background = element_blank(),
legend.key = element_blank()) +
labs(subtitle="Legend: Bottom-Right Inside the Plot")

alt 图标

legend.position也可以用两个元素构成的数值向量来控制,主要是设置图例在图片中间所在具体位置,而不是图片的外围。数值大小一般在0-1之间,超出数值往往导致图例隐藏。
其他诸如guide_legend()或guide_colourbar()等函数也可以用来修改图例。以及向legend.position\legend.direction\legend.justification\legend.box等属性控制图例在图像中的布局。

3. 修改面板

组件 对应的element_功能() 描述
panel.background element_rect() 面板背景(数据下面)
panel.border element_rect() 面板边界(数据上面)
panel.grid element_line() 网格线
panel.grid.major element_line() 主网格线
panel.grid.major.x element_line() 竖直主网格线
panel.grid.major.y element_line() 水平主网格线
panel.grid.minor element_line() 次网格线
panel.grid.minor.x element_line() 竖直次网格线
panel.grid.minor.y element_line() 水平次网格线
aspect.ratio 数值(m/n,两个整数) 图像宽高比

3.1 更改绘图背景

1
2
3
4
p + theme(panel.background = element_rect(fill = 'grey80'),
plot.background=element_rect(fill="lightblue"),
plot.margin = unit(c(3, 2, 1, 1), "cm")) + #设置绘图区域距离边的据类,上,右,下,左
labs(title="Modified Background", subtitle="Change Plot Margin")

alt 图标

3.2 更改主次网格线以及X、Y坐标轴

1
2
3
4
5
6
7
8
p + theme(panel.grid.major = element_line(colour = "blue", linewidth=1.5), #主网格线 蓝色
panel.grid.minor = element_line(colour = "red", linewidth=0.25,
linetype = "dashed"),# 次网格线 红色
panel.border = element_blank(),
axis.line.x = element_line(colour = "yellow", linewidth=1.5,
lineend = "butt"), # x轴 黄色,y轴 绿色
axis.line.y = element_line(colour = "green",linewidth=1.5)) +
labs(subtitle="Change Major and Minor grid, Axis Lines")

alt 图标

3.3 删除主次网格线

1
2
3
4
5
6
7
p + theme(panel.grid.major = element_blank(), #主网格线
panel.grid.minor = element_blank(), #次网格线
panel.border = element_blank(), #边框
axis.title = element_blank(), #轴标题
axis.text = element_blank(), # 文本
axis.ticks = element_blank()) +
labs(title="Modified Background", subtitle="Remove major and minor axis grid, border, axis title, text and ticks")

alt 图标

4. 分面组件

利用facet绘制多个图像时使用。

组件 对应的element_功能() 描述
strip.background element_rect() 分面标签背景
strip.text element_text() 条状文本
strip.text.x element_text() 水平条状文本
strip.text.y element_text() 竖直条状文本
panel.spacing unit() 分面间边距
panel.spacing.x unit() 竖直分面间边距
panel.spacing.y unit() 水平分面间边距

5. gides()

在ggplot2中,美学映射相应的标度函数可以调整图形本身的效果的同时调整图例的外观,但是有时候只想改变图例的外形,并不想影响图形的效果,这时就可以使用guides()函数。
override.aes()可以只修改图例中的外观,例如size、shape等等,而图形本身的外观不受影响。

1
2
3
4
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(alpha = .5, size = 1) +
guides(color = guide_legend(override.aes = list(size = 3)))

alt 图标

如果是多个参数,用list就可以。

1
2
3
4
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(alpha = .5, size = 1) +
guides(color = guide_legend(override.aes = list(size = 3, alpha=0.5)))

参考资料:

  1. https://zhuanlan.zhihu.com/p/79968177
  2. https://zhuanlan.zhihu.com/p/463041897
  3. https://zhuanlan.zhihu.com/p/370223674
  4. https://zhuanlan.zhihu.com/p/374283162
  5. https://zhuanlan.zhihu.com/p/460976888