gpt4 book ai didi

r - 如何在 facet_wrap 中使用 label_wrap_gen 和 as_labeller

转载 作者:行者123 更新时间:2023-12-04 01:18:39 26 4
gpt4 key购买 nike

我有一个分面图,并且想将分面条标题包装在多行上(如果超过一定数量的字符),所以我知道我使用 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))
这产生了这个:
enter image description here
还使用 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
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats4 parallel stats graphics grDevices utils datasets methods base

other attached packages:
[1] rsconnect_0.8.16 forcats_0.5.0 stringr_1.4.0 purrr_0.3.4 readr_1.3.1
[6] tibble_3.0.1 tidyverse_1.3.0 gtools_3.8.2 dendextend_1.13.4 patchwork_1.0.1
[11] gridExtra_2.3 plotly_4.9.2.1 pRolocdata_1.26.0 tidyr_1.1.0 reshape2_1.4.4
[16] pRoloc_1.29.0 BiocParallel_1.22.0 MLInterfaces_1.68.0 cluster_2.1.0 annotate_1.66.0
[21] XML_3.99-0.3 AnnotationDbi_1.50.0 IRanges_2.22.2 MSnbase_2.14.2 ProtGenerics_1.20.0
[26] S4Vectors_0.26.1 mzR_2.22.0 Rcpp_1.0.4.6 Biobase_2.48.0 BiocGenerics_0.34.0
[31] ggplot2_3.3.1 shinyhelper_0.3.2 colorspace_1.4-1 colourpicker_1.0 shinythemes_1.1.2
[36] DT_0.13 shiny_1.4.0.2 dplyr_1.0.0

最佳答案

你可以做

plot(iris, aes(x=Sepal.Length, y=Petal.Length)) + 
geom_point() +
facet_wrap(~Species, labeller = as_labeller(newLabs, default=label_wrap_gen(10)))
enter image description here as_labeller 采用默认的贴标函数。所以你只需要在那里传递标签包装器。

关于r - 如何在 facet_wrap 中使用 label_wrap_gen 和 as_labeller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62944834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com