美文网首页
ggplot2中facet_wrap( )的高阶用法

ggplot2中facet_wrap( )的高阶用法

作者: R语言数据分析指南 | 来源:发表于2021-03-27 21:27 被阅读0次

介绍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))

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"))

同时显示变量名称与因子值

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)

显示变量名称+因子值

p + facet_grid(. ~ cyl, labeller = label_both)

labeller参数转化添加希腊字母

p + facet_grid(. ~ cyl2)
p + facet_grid(. ~ cyl2, labeller = label_parsed)

喜欢的小伙伴欢迎关注我的公众号R语言数据分析指南,持续分享更多优质资源

相关文章

网友评论

      本文标题:ggplot2中facet_wrap( )的高阶用法

      本文链接:https://www.haomeiwen.com/subject/xwcyhltx.html