facetラベルが見切れないようにする

facetのラベルの文字列が長い時にどうすれば見切れないようにできるか
facet
ggplot2
R
Published

October 31, 2023

Modified

November 22, 2024

タイトル通りfacetのラベルが見切れないようにする。

Load packages

library(ggplot2)

プロット作成

marquee_st <- \(t, c) glue::glue("{.[c] **[t]**}", .open = "[", .close = "]")
tbl_plot <-
  iris |>
  tidyr::pivot_longer(cols = !Species) |>
  tidyr::nest(data = !name) |>
  dplyr::arrange(name) |>
  dplyr::mutate(
    name =
      forcats::fct_inorder(name) |>
      purrr::map_chr(\(x) paste(rep(x, as.integer(x)*2), collapse = " ")) |>
      marquee_st(hcl.colors(n = 4, palette = "Dark 3")) |>
      forcats::fct_inorder()
  ) |>
  tidyr::unnest(cols = data)
tbl_plot
# A tibble: 600 × 3
   name                                     Species value
   <fct>                                    <fct>   <dbl>
 1 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.4
 2 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.4
 3 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.3
 4 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.5
 5 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.4
 6 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.7
 7 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.4
 8 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.5
 9 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.4
10 {.#E16A86 **Petal.Length Petal.Length**} setosa    1.5
# ℹ 590 more rows
gp <-
  tbl_plot |>
  ggplot(aes(value)) +
  geom_histogram() +
  facet_wrap(~ name) +
  theme(
    rect = element_blank(),
    strip.background = element_rect(color = "black"),
    strip.text = marquee::element_marquee(family = "Noto Sans", size = 12, angle = 10, style = marquee::classic_style(weight = 900))
  )

チェック

# facetのラベルが長くなると枠外はクリップされて見切れる
gp
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# `theme(strip.clip = "off")`を指定するとクリップされない
gp + theme(strip.clip = "off")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

参考

Turn off clipping of facet labels

I have a faceted graph with a strip label which is clipped due to the width of the facets - I have been removing this clip manually in Inkscape but would like to do it in R. See this small reproduc...

https://stackoverflow.com/questions/66101250/turn-off-clipping-of-facet-labels

Session info

sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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

time zone: Asia/Tokyo
tzcode source: internal

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

other attached packages:
[1] ggplot2_3.5.1

loaded via a namespace (and not attached):
 [1] gtable_0.3.5      jsonlite_1.8.8    dplyr_1.1.4       compiler_4.3.2   
 [5] renv_1.0.10       tidyselect_1.2.1  tidyr_1.3.1       systemfonts_1.1.0
 [9] scales_1.3.0      textshaping_0.4.0 yaml_2.3.9        fastmap_1.1.1    
[13] R6_2.5.1          labeling_0.4.3    generics_0.1.3    knitr_1.48       
[17] forcats_1.0.0     tibble_3.2.1      munsell_0.5.1     pillar_1.9.0     
[21] rlang_1.1.4       utf8_1.2.4        marquee_0.1.0     xfun_0.46        
[25] cli_3.6.3         withr_3.0.0       magrittr_2.0.3    digest_0.6.34    
[29] grid_4.3.2        lifecycle_1.0.4   vctrs_0.6.5       evaluate_0.24.0  
[33] glue_1.7.0        farver_2.1.2      fansi_1.0.6       colorspace_2.1-1 
[37] rmarkdown_2.25    purrr_1.0.2       tools_4.3.2       pkgconfig_2.0.3  
[41] htmltools_0.5.7