如何使用facet和margin = TRUE更改ggplot中的strip.text标签 [英] how to change strip.text labels in ggplot with facet and margin=TRUE

查看:570
本文介绍了如何使用facet和margin = TRUE更改ggplot中的strip.text标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这里但仍然无法弄清楚。如何使用faceting更改ggplot中的strip.text.x标签?具体来说,我正在使用具有边距的facet_grid。边距的strip.text标签是(全部) - 但由于我在非英语国家,我宁愿在我的母语中写上Total或类似的东西。

I have looked here but still can't figure it out. How do I change the strip.text.x labels in a ggplot with faceting? Specifically I am using facet_grid with margins. The strip.text label for the margin is "(all)" - but since I am in a non-english speaking country I would rather write "Total" or something similar in my native tongue.

opts(stip.text.x=c(levels(facetvariabel,"Total")) does not work.

有什么想法?

示例(并不是真正的最佳数据集 - 但我想它会工作)

Example (not really the best dataset for this - but I guess it will work)

ggplot(cars, aes(x=dist))+geom_bar()+facet_grid(.~speed, margin=T)


推荐答案

函数:
$ b

You can customize the facet labels by giving labeller function:

f <- function(x, y) {
  if (x == "speed")
    c(y[-length(y)], "Total")
  else
    y
}

ggplot(cars, aes(x = dist)) +
  geom_bar() +
  facet_grid(. ~ speed, margin = TRUE, labeller = f)

这篇关于如何使用facet和margin = TRUE更改ggplot中的strip.text标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆