我想使用facet_wrap 总结几列。我想给几个方面窗格起长标题。例如:
set.seed(123)
df <-
data.frame(
a = sample(0:1, 20, replace = T),
b = sample(1:4, 20, replace = T)
)
names <- list(
"a" = "Nice table 1",
"b" = "Here is a really long title that I would like to wrap within the facet pane")
labeller_fun <-
function(variable,value){
return(names[value])
}
ggplot(gather(df,, factor_key = TRUE), aes(x = factor(value))) +
geom_bar() +
facet_wrap(~ key, scales = "free_x", as.table = TRUE, labeller = labeller_fun) +
xlab("")
在这里,长标题溢出并且几乎看不见。有没有办法在窗格标题框中自动换行长文本?
您可以使用该功能str_wrap
from stringr
打包并将其添加到“labeller_fun”函数的返回值中。
随着参数width
,您可以指定每行的最大大小:
libtrary(stringr)
names <- list(
"a" = "Nice table 1",
"b" = "Here is a really long title that I would like to wrap within the facet pane")
labeller_fun <-
function(variable,value){
return(str_wrap(names[value], width = 30))
}
ggplot(gather(df, factor_key = TRUE), aes(x = factor(value))) +
geom_bar() +
facet_wrap(~ key, scales = "free_x", as.table = TRUE, labeller = labeller_fun) +
xlab("")
它能回答你的问题吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)