我有一个分面图,并且想将分面条标题包装在多行上(如果超过一定数量的字符),所以我知道我使用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
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
Run Code Online (Sandbox Code Playgroud)
你可以做
plot(iris, aes(x=Sepal.Length, y=Petal.Length)) +
geom_point() +
facet_wrap(~Species, labeller = as_labeller(newLabs, default=label_wrap_gen(10)))
Run Code Online (Sandbox Code Playgroud)
在as_labeller采用默认的贴标机的功能。所以你只需要在那里传递标签包装器。