如何使用 facet 和 margin=TRUE 更改 ggplot 中的 strip.text 标签 [英] how to change strip.text labels in ggplot with facet and margin=TRUE
问题描述
我看过这里 但仍然无法弄清楚.如何更改带有刻面的 ggplot 中的 strip.text.x 标签?具体来说,我正在使用带有边距的 facet_grid.边距的 strip.text 标签是(all)"——但由于我在一个非英语国家,我宁愿用我的母语写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)
推荐答案
你可以通过给labeller函数自定义facet标签:
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屋!