我有一个方面图,并且想将方面条标题包装在多行上(如果超过一定数量的字符),所以我知道我使用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))
This produces this:
在使用时如何包装分面条标题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)))
The as_labeller
takes a default labeller function. So you just need to pass the label wrapper there.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)