介绍ggplot2中facet_wrap( )函数中labeller 参数的用法
library(ggplot2)
library(tibble)
df <- tribble(
~x, ~y, ~label,
1, 2, "A super long label oh god how am I going to deal with this",
2, 1, "A shorter one"
)
df
> df
# A tibble: 2 x 3
x y label
<dbl> <dbl> <chr>
1 1 2 A super long label oh god how am I going to deal
2 2 1 A shorter one
ggplot(df) +
geom_point(aes(x = x, y = y)) +
facet_wrap(vars(label))
![](https://img.haomeiwen.com/i16360488/5cb1e621fbe37980.png)
labeller参数,可以使用它来处理太长的facet标签
ggplot(df) +
geom_point(aes(x = x, y = y)) +
facet_wrap(vars(label), labeller = label_wrap_gen())+
theme(panel.spacing.x = unit(0.05, "cm"))
![](https://img.haomeiwen.com/i16360488/277226890f9a9f5b.png)
同时显示变量名称与因子值
mtcars$cyl2 <- factor(mtcars$cyl,labels = c("alpha", "beta", "gamma"))
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
显示因子值
p + facet_grid(. ~ cyl, labeller = label_value)
![](https://img.haomeiwen.com/i16360488/fbfbba2ccc34f6ff.png)
显示变量名称+因子值
p + facet_grid(. ~ cyl, labeller = label_both)
![](https://img.haomeiwen.com/i16360488/1f01ac6243c78456.png)
labeller参数转化添加希腊字母
p + facet_grid(. ~ cyl2)
![](https://img.haomeiwen.com/i16360488/f7dc3a0dd900241a.png)
p + facet_grid(. ~ cyl2, labeller = label_parsed)
![](https://img.haomeiwen.com/i16360488/5f12f757b4a32891.png)
喜欢的小伙伴欢迎关注我的公众号R语言数据分析指南,持续分享更多优质资源
网友评论