Skip to content

strip.text angle parameter does not work in conjuction with facet_grid switch #4330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zlskidmore opened this issue Jan 27, 2021 · 5 comments

Comments

@zlskidmore
Copy link

Hi,

I've noticed that it is not possible to rotate strip text when forcing strip text to the left hand side of the plot using facet grid. For example:

ggplot(mtcars, aes(disp, hp)) + geom_point() + facet_grid(cyl~carb, switch="y") + theme(strip.text.y=element_text(angle=90))

ggplot(mtcars, aes(disp, hp)) + geom_point() + facet_grid(cyl~carb, switch="y") + theme(strip.text.y=element_text(angle=0))

does nothing, however removing switch="y" and rotating the text seems fine:

ggplot(mtcars, aes(disp, hp)) + geom_point() + facet_grid(cyl~carb) + theme(strip.text.y=element_text(angle=90))

ggplot(mtcars, aes(disp, hp)) + geom_point() + facet_grid(cyl~carb) + theme(strip.text.y=element_text(angle=0))

ggplot2 version is 3.3.2

@smouksassi
Copy link

Try
strip.text.y.left

@clauswilke
Copy link
Member

As a general rule, these kinds of questions are more appropriate for the R Studio community or stackoverflow.

@zlskidmore
Copy link
Author

Yes I see now, my appologies.

I didn't see that documented in the current docs and just assumed it was a bug.

Thanks again!

@slowkow
Copy link
Contributor

slowkow commented Jul 6, 2021

Here’s my attempt to display the problem and the workaround.

Problem

The angle for strip text is discarded when we use facet_grid(switch = "y").

Let's start with dummy data, and load ggplot2.

set.seed(1)
d <- data.frame(
  i = rep(1:10, 5),
  j = rep(1:5, each = 10),
  x = rnorm(50),
  z = rep(c("a", "a", "b", "b", "c"), each = 10)
)

library(ggplot2)

Here's the base plot.

Notice: we set the angle for strip.text.y to 0.

p <- ggplot(d) +
  geom_tile(
    data = d,
    mapping = aes(x = i, y = j, fill = x)
  ) +
  theme(
    strip.text.y = element_text(size = 20, angle = 0),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

As long as we do not use switch = "y", the plot looks OK:

p + facet_grid(
  rows = vars(z), scales = "free", space = "free"
)

Notice what happens when we add switch = "y":

  • the angle is set back to 90 instead of 0

  • the size is still set to 20

So, size is preserved but angle is discarded.

Is this expected behavior?

p + facet_grid(
  rows = vars(z), scales = "free", space = "free",
  switch = "y"
)

Workaround

Here’s the workaround: use theme(strip.text.y.left) instead of theme(strip.text.y).

p + facet_grid(
  rows = vars(z), scales = "free", space = "free",
  switch = "y"
) +
  theme(
    strip.text.y.left = element_text(size = 20, angle = 0)
  )

Created on 2021-07-06 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       macOS Catalina 10.15.7      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2021-07-06                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                             
#>  assertthat    0.2.1      2019-03-21 [2] CRAN (R 4.0.2)                     
#>  blob          1.2.1      2020-01-20 [2] CRAN (R 4.0.2)                     
#>  callr         3.5.1      2020-10-13 [2] CRAN (R 4.0.2)                     
#>  cli           2.3.1      2021-02-23 [1] CRAN (R 4.0.3)                     
#>  colorspace    2.0-0      2020-11-11 [2] CRAN (R 4.0.2)                     
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.0.2)                     
#>  curl          4.3        2019-12-02 [2] CRAN (R 4.0.1)                     
#>  DBI           1.1.0      2019-12-15 [2] CRAN (R 4.0.2)                     
#>  debugme       1.1.0      2017-10-22 [1] CRAN (R 4.0.2)                     
#>  desc          1.2.0      2018-05-01 [2] CRAN (R 4.0.2)                     
#>  devtools      2.3.0      2020-04-10 [2] CRAN (R 4.0.2)                     
#>  digest        0.6.27     2020-10-24 [2] CRAN (R 4.0.2)                     
#>  dplyr         1.0.4      2021-02-02 [1] CRAN (R 4.0.2)                     
#>  ellipsis      0.3.1      2020-05-15 [2] CRAN (R 4.0.2)                     
#>  evaluate      0.14       2019-05-28 [2] CRAN (R 4.0.1)                     
#>  fansi         0.4.2      2021-01-15 [1] CRAN (R 4.0.2)                     
#>  farver        2.0.3      2020-01-16 [2] CRAN (R 4.0.2)                     
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                     
#>  generics      0.1.0      2020-10-31 [2] CRAN (R 4.0.2)                     
#>  ggplot2     * 3.3.5      2021-06-25 [1] CRAN (R 4.0.2)                     
#>  glue          1.4.2      2020-08-27 [2] CRAN (R 4.0.2)                     
#>  gtable        0.3.0      2019-03-25 [2] CRAN (R 4.0.2)                     
#>  highr         0.8        2019-03-20 [2] CRAN (R 4.0.2)                     
#>  htmltools     0.5.1.1    2021-01-22 [1] CRAN (R 4.0.2)                     
#>  httr          1.4.2      2020-07-20 [2] CRAN (R 4.0.2)                     
#>  knitr         1.31       2021-01-27 [1] CRAN (R 4.0.2)                     
#>  labeling      0.4.2      2020-10-20 [2] CRAN (R 4.0.2)                     
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.0.2)                     
#>  magrittr      2.0.1.9000 2020-12-15 [1] Github (tidyverse/magrittr@bb1c86a)
#>  memoise       1.1.0.9000 2020-12-15 [1] Github (r-lib/memoise@0901e3f)     
#>  mime          0.10       2021-02-13 [1] CRAN (R 4.0.2)                     
#>  munsell       0.5.0      2018-06-12 [2] CRAN (R 4.0.2)                     
#>  pillar        1.5.0      2021-02-22 [1] CRAN (R 4.0.3)                     
#>  pkgbuild      1.1.0      2020-07-13 [2] CRAN (R 4.0.2)                     
#>  pkgconfig     2.0.3      2019-09-22 [2] CRAN (R 4.0.2)                     
#>  pkgload       1.1.0      2020-05-29 [2] CRAN (R 4.0.2)                     
#>  prettyunits   1.1.1      2020-01-24 [2] CRAN (R 4.0.2)                     
#>  processx      3.4.5      2020-11-30 [2] CRAN (R 4.0.2)                     
#>  ps            1.5.0      2020-12-05 [2] CRAN (R 4.0.2)                     
#>  purrr         0.3.4      2020-04-17 [2] CRAN (R 4.0.2)                     
#>  R6            2.5.0      2020-10-28 [2] CRAN (R 4.0.2)                     
#>  remotes       2.2.0      2020-07-21 [1] CRAN (R 4.0.2)                     
#>  rlang         0.4.10     2020-12-30 [1] CRAN (R 4.0.2)                     
#>  rmarkdown     2.6        2020-12-14 [1] CRAN (R 4.0.2)                     
#>  rprojroot     2.0.2      2020-11-15 [2] CRAN (R 4.0.2)                     
#>  scales        1.1.1      2020-05-11 [2] CRAN (R 4.0.2)                     
#>  sessioninfo   1.1.1      2018-11-05 [2] CRAN (R 4.0.2)                     
#>  stringi       1.5.3      2020-09-09 [2] CRAN (R 4.0.2)                     
#>  stringr       1.4.0      2019-02-10 [2] CRAN (R 4.0.2)                     
#>  testthat      3.0.0      2020-10-31 [2] CRAN (R 4.0.2)                     
#>  tibble        3.0.6      2021-01-29 [1] CRAN (R 4.0.2)                     
#>  tidyselect    1.1.0      2020-05-11 [2] CRAN (R 4.0.2)                     
#>  usethis       1.6.1      2020-04-29 [2] CRAN (R 4.0.2)                     
#>  utf8          1.1.4      2018-05-24 [2] CRAN (R 4.0.2)                     
#>  vctrs         0.3.6      2020-12-17 [1] CRAN (R 4.0.2)                     
#>  withr         2.4.1      2021-01-26 [1] CRAN (R 4.0.2)                     
#>  xfun          0.21       2021-02-10 [1] CRAN (R 4.0.2)                     
#>  xml2          1.3.2      2020-04-23 [2] CRAN (R 4.0.2)                     
#>  yaml          2.2.1      2020-02-01 [2] CRAN (R 4.0.2)                     
#> 
#> [1] /Users/kamil/Library/R/4.0/library
#> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

@karawoo
Copy link
Member

karawoo commented Jul 7, 2021

This is expected behavior.

Theme elements only inherit values from higher levels if they are not explicitly set themselves. theme_grey() explicitly sets the angle of strip.text.y.left to 90. So you need to edit strip.text.y.left if you want to change the angle for strip text on the left.

theme_grey() does not explicitly set the size for strip.text.y.left, nor anything for strip.text.y.right, so those will inherit from strip.text.y.

If you're interested in hearing a bit more about this, I covered it in my rstudio::global talk: https://www.rstudio.com/resources/rstudioglobal-2021/always-look-on-the-bright-side-of-plots/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants