ggplot2 | 图形分面函数

图形分面是指根据某个或某些分类变量(不一定是因子类型)将绘图数据分成若干子集并分别绘图。

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

添加分面变量:

ggplot(mtcars, aes(mpg, qsec)) + 
  geom_point() +
  facet_wrap(vars(gear))
c1e9c026d3454c1471bf07f9527d72a7.png

nrowncol:控制子图排列的行数或列数。

ggplot(mtcars, aes(mpg, qsec)) + 
  geom_point() +
  facet_wrap(vars(gear), nrow = 2)
763ea98ead818e21d95cfc825d63cc14.png

scales:控制子图坐标刻度是否保持一致。fixed表示子图的横、纵坐标都保持一致;free表示各子图坐标刻度自由变化;free_xfix_y分别表示允许横、纵坐标自由变化。

ggplot(mtcars, aes(mpg, qsec)) + 
  geom_point() +
  facet_wrap(vars(gear), nrow = 2,
             scales = "free_y")
3a66d3b429a21debc546cf95063f47e2.png

labeller:修改子图标题格式。

ggplot(mtcars, aes(mpg, qsec)) + 
  geom_point() +
  facet_wrap(vars(carb), nrow = 2,
             labeller = label_both)
46aa980ec208474aa2818cefeaa1cf05.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

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

dir:控制子图排列顺序。h(默认值)表示水平排序,即先左右、后上下;v表示垂直排序,即先上下、后左右。

ggplot(mtcars, aes(mpg, qsec)) + 
  geom_point() +
  facet_wrap(vars(carb), nrow = 2,
             labeller = label_both,
             dir = "v")
8da4355dfc7c0f4b807529154c6896de.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

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

列分面变量:

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

行、列同时进行分面:

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

margins:添加边际图形(var:(all)):

ggplot(mtcars, aes(mpg, qsec)) + 
  geom_point() +
  facet_grid(gear ~ vs,
             margins = c("gear", "vs"),
             labeller = label_both)
9d18b221241eee0c7094a63745f923cf.png
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值