相关疑难解决方法(0)

R:ggplot2,我可以将facet/strip文本换行吗?

我发现这是非常有用的代码文本换行位置:

 wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n")`

 my_title <- "This is a really long title of a plot that I want to nicely wrap and fit onto the plot without having to manually add the backslash n, but at the moment it does not"

 r + geom_smooth() + opts(title = wrapper(my_title, width = 20))
Run Code Online (Sandbox Code Playgroud)

我想用它来将文本包装在一个方面/条带中,但不知道如何.

 p + geom_bar(stat="identity")+facet_wrap(~variable1) + 
    opts(strip.text.x=theme_text(size=12, face="bold")
Run Code Online (Sandbox Code Playgroud)

它是否传递给strip.text.x选项?

r ggplot2

5
推荐指数
2
解决办法
4191
查看次数

如何在 facet_wrap 中使用 label_wrap_gen 和 as_labeller

我有一个分面图,并且想将分面条标题包装在多行上(如果超过一定数量的字符),所以我知道我使用labeller = label_wrap_gen(10)(例如包装超过 10 个字符),这在传递给 时效果很好facet_wrap,但是,我还想传递新标签。我知道我可以labeller = as_labeller(new labels)用来做到这一点。有没有办法一起做?我不想弄乱数据并直接在 data.frame 中重新标记它们(在我自己的情况下是一个小标题)。

下面是一个示例来演示:

data(iris)

## plot iris lengths by species
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length)) + 
  geom_point() + 
  facet_wrap(~Species)

## re-label species names
newLabs <- c(paste("this one is called", levels(iris$Species)))
newLabs <- setNames(newLabs, nm = levels(iris$Species))
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length)) + 
  geom_point() + 
  facet_wrap(~Species, labeller = as_labeller(newLabs))
Run Code Online (Sandbox Code Playgroud)

这产生了这个: 在此处输入图片说明

还使用时如何包装刻面条标题as_labeller

sessionInfo
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

4
推荐指数
1
解决办法
495
查看次数

标签 统计

ggplot2 ×2

r ×2