本文主要介绍在ggplot2中如何调整facet_wrap() 分面函数下的标签位置、字体、背景颜色等。
1、数据准备
library(ggplot2)
library(gcookbook)
head(heightweight)
# sex ageYear ageMonth heightIn weightLb
#1 f 11.92 143 56.3 85.0
#2 f 12.92 155 62.3 105.0
#3 f 12.75 153 63.3 108.0
str(heightweight)
#'data.frame': 236 obs. of 5 variables:
# $ sex : Factor w/ 2 levels "f","m": 1 1 1 1 1 1 1 1 1 1 ...
# $ ageYear : num 11.9 12.9 12.8 13.4 15.9 ...
# $ ageMonth: int 143 155 153 161 191 171 185 142 160 140 ...
# $ heightIn: num 56.3 62.3 63.3 59 62.5 62.5 59 56.5 62 53.8 ...
# $ weightLb: num 85 105 108 92 112 ...
2、分面标签位置调整
通过控制facet_wrap() 函数内的strip.position参数可实现对分面标签的位置调整。strip.position参数可设置为"top", "bottom", "left", "right"等其中的任意一个。
2.1 默认:strip.position = "top"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex) + # strip.position = "top"
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0ee123d6-1314-11ed-b5a2-fa163eb4f6be.png)
2.2 strip.position = "bottom"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "bottom") +
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f186c7e-1314-11ed-b5a2-fa163eb4f6be.png)
2.3 strip.position = "left"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "left") +
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f3d9f80-1314-11ed-b5a2-fa163eb4f6be.png)
2.4 strip.position = "right"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "right") +
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f47fe80-1314-11ed-b5a2-fa163eb4f6be.png)
3、分面标签方向调整
通过控制facet_wrap() 函数内的dir参数可实现对分面标签的方向调整。dir参数可设置为h水平或v水平。
3.1 strip.position = "top",dir = "v"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, dir = "v") + # strip.position = "top"
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f56f89a-1314-11ed-b5a2-fa163eb4f6be.png)
3.2 strip.position = "bottom",dir = "v"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "bottom", dir = "v") +
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f67b46e-1314-11ed-b5a2-fa163eb4f6be.png)
3.3 strip.position = "left", dir = "v"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "left", dir = "v") +
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f7806a2-1314-11ed-b5a2-fa163eb4f6be.png)
3.4 strip.position = "right", dir = "v"
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "right", dir = "v") +
theme_bw()
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f83d9a0-1314-11ed-b5a2-fa163eb4f6be.png)
分面标签字体、颜色调整
利用strip.text() 函数调整分面标签字体相关属性,利用strip.background() 函数调整分面标签背景相关属性。
ggplot(heightweight, aes(ageYear , heightIn )) +
geom_point() +
facet_wrap(~sex, strip.position = "left", dir = "v") +
theme_bw() +
theme(strip.text = element_text(size = 15),
strip.background = element_rect(fill = "lightblue"))
![](https://oss-emcsprod-public.modb.pro/wechatSpider/modb_20220803_0f91f2b0-1314-11ed-b5a2-fa163eb4f6be.png)
其他
关于facet_wrap() 函数的更多内容可在RStudio控制台输入help(facet_wrap) 查看。
facet_wrap(
facets,
nrow = NULL,
ncol = NULL,
scales = "fixed",
shrink = TRUE,
labeller = "label_value",
as.table = TRUE,
switch = NULL,
drop = TRUE,
dir = "h",
strip.position = "top"
)
如有帮助请多多点赞哦!
文章转载自日常分享的小懒猫,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。