图形分面是指根据某个或某些分类变量(不一定是因子类型)将绘图数据分成若干子集并分别绘图。
ggplot2绘图系统中有两个专门的分面函数:
facet_wrap()
facet_grid()
1 facet_wrap()
facet_wrap()
函数的语法结构如下:
facet_wrap(
facets, nrow = NULL, ncol = NULL,
scales = "fixed", shrink = TRUE,
labeller = "label_value",
as.table = TRUE,
drop = TRUE, dir = "h",
strip.position = "top"
)
facets
:分面变量;使用vars()
函数加以引用。
不添加任何变量:
library(ggplot2)
data("mtcars")
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars())
![7511a44c9d84085fb58ced3326acd4e6.png](https://i-blog.csdnimg.cn/blog_migrate/b1ea0b7ef3bf8aa6cc4968d291002d26.png)
添加分面变量:
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(gear))
![c1e9c026d3454c1471bf07f9527d72a7.png](https://i-blog.csdnimg.cn/blog_migrate/2d554ed090d13783f65e24f2aba2913c.png)
nrow
、ncol
:控制子图排列的行数或列数。
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(gear), nrow = 2)
![763ea98ead818e21d95cfc825d63cc14.png](https://i-blog.csdnimg.cn/blog_migrate/7032a88a59f498191121844cafa79336.png)
scales
:控制子图坐标刻度是否保持一致。fixed
表示子图的横、纵坐标都保持一致;free
表示各子图坐标刻度自由变化;free_x
、fix_y
分别表示允许横、纵坐标自由变化。
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(gear), nrow = 2,
scales = "free_y")
![3a66d3b429a21debc546cf95063f47e2.png](https://i-blog.csdnimg.cn/blog_migrate/860604ba8448e08730eba0bf0d21b189.png)
labeller
:修改子图标题格式。
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(carb), nrow = 2,
labeller = label_both)
![46aa980ec208474aa2818cefeaa1cf05.png](https://i-blog.csdnimg.cn/blog_migrate/77f7d93d85bb62ef2d6533446c558fe9.png)
as.table
:修改子图排列顺序。TRUE表示分面变量数值越大对应的子图越靠近左下方;FALSE表示数值越大越靠近右上方。
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(carb), nrow = 2,
labeller = label_both,
as.table = F)
![513d617d21d1f4e92556eb18aee2a1e2.png](https://i-blog.csdnimg.cn/blog_migrate/8749510641c0b8f8a5b49e2c2d193f4c.png)
drop
:当分面变量是因子类型时,是否删除没有对应样本的因子水平。
library(tidyverse)
mtcars %>%
mutate(carb2 = factor(carb, levels = 1:8)) %>%
ggplot(aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(carb2), nrow = 2,
labeller = label_both,
drop = F)
![e5f181a0dff6bcbb97a56396a4136935.png](https://i-blog.csdnimg.cn/blog_migrate/46527d3b857dc32d1e4ae61745bea33a.png)
dir
:控制子图排列顺序。h
(默认值)表示水平排序,即先左右、后上下;v
表示垂直排序,即先上下、后左右。
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(carb), nrow = 2,
labeller = label_both,
dir = "v")
![8da4355dfc7c0f4b807529154c6896de.png](https://i-blog.csdnimg.cn/blog_migrate/91e5f4ff47e2523080bf1f74e20cb657.png)
strip.position
:控制子图标题位置(top、bottom、left、right)。
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_wrap(vars(carb), nrow = 2,
labeller = label_both,
strip.position = "bottom")
![43dc65cab1bc684ad4b7e766abb58262.png](https://i-blog.csdnimg.cn/blog_migrate/46bcbd892743fcc35ed6cd003cfc3815.png)
2 facet_grid()
facet_grid()
函数的语法结构如下:
facet_grid(
rows = NULL, cols = NULL,
scales = "fixed", space = "fixed",
shrink = TRUE, labeller = "label_value",
as.table = TRUE,
drop = TRUE, margins = FALSE
)
行分面变量:
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(rows = vars(gear),
labeller = label_both)
## 或者
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(gear ~ .,
labeller = label_both)
![2924288a88dc13a3fb01804c1079dfe8.png](https://i-blog.csdnimg.cn/blog_migrate/210660262cef298021896a175a4edc11.png)
列分面变量:
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(cols = vars(vs),
labeller = label_both)
## 或者
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(. ~ vs,
labeller = label_both)
![c36e8143cfddcb7c7dd2a1fad7e058bc.png](https://i-blog.csdnimg.cn/blog_migrate/eab20c6543128e7e190bc6080dab0687.png)
行、列同时进行分面:
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(rows = vars(gear),
cols = vars(vs),
labeller = label_both)
## 或者
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(gear ~ vs,
labeller = label_both)
![fe3b99a6b4d3eeecb92e22f23c7955d7.png](https://i-blog.csdnimg.cn/blog_migrate/14c708ce0765d64132facfa3a8d9a532.png)
margins
:添加边际图形(var:(all)):
ggplot(mtcars, aes(mpg, qsec)) +
geom_point() +
facet_grid(gear ~ vs,
margins = c("gear", "vs"),
labeller = label_both)
![9d18b221241eee0c7094a63745f923cf.png](https://i-blog.csdnimg.cn/blog_migrate/a95f486823cf7ec681cd51b6056860bc.png)