teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

Error when facetting and using `ggtext` #151

Closed danielcoral closed 3 months ago

danielcoral commented 3 months ago

Hi!

I have previously been able to format the text of facet strips generated with ggh4x (version 0.2.8), using the markdown functionality of ggtext version 0.1.2, and ggplot2 version 3.4.4. After upgrading to ggplot2 version 3.5.0, this now fails :

library(dplyr, warn.conflicts = FALSE)
library(tidyr)
library(ggplot2)
dt <- iris %>% 
   pivot_longer(-Species)
dt
#> # A tibble: 600 × 3
#>    Species name         value
#>    <fct>   <chr>        <dbl>
#>  1 setosa  Sepal.Length   5.1
#>  2 setosa  Sepal.Width    3.5
#>  3 setosa  Petal.Length   1.4
#>  4 setosa  Petal.Width    0.2
#>  5 setosa  Sepal.Length   4.9
#>  6 setosa  Sepal.Width    3  
#>  7 setosa  Petal.Length   1.4
#>  8 setosa  Petal.Width    0.2
#>  9 setosa  Sepal.Length   4.7
#> 10 setosa  Sepal.Width    3.2
#> # ℹ 590 more rows
dt %>% 
   mutate(name = gsub("\\.", "<br>", name)) %>% 
   ggplot(aes(value)) + 
   geom_density() + 
   facet_grid(name ~ Species, scales = 'free') + 
   theme(strip.text.y = ggtext::element_markdown(angle = 0))

dt %>% 
   mutate(name = gsub("\\.", "<br>", name)) %>% 
   ggplot(aes(value)) + 
   geom_density() + 
   ggh4x::facet_grid2(name ~ Species, scales = 'free') + 
   theme(strip.text.y = ggtext::element_markdown(angle = 0))
#> Error in `FUN()`:
#> ! Don't know how to get width of <integer> object

Created on 2024-03-13 with reprex v2.1.0

teunbrand commented 3 months ago

Hi there, thanks for the report! I seem unable to reproduce the bug. Are you at the latest CRAN release of ggh4x? EDIT: it was me who was not on the latest CRAN release version of ggh4x 😅 Yes can confirm now.

teunbrand commented 3 months ago

Should work with current github version:

devtools::load_all("~/packages/ggh4x")
#> ℹ Loading ggh4x
#> Loading required package: ggplot2
library(dplyr, warn.conflicts = FALSE)
library(tidyr)

dt <- iris %>% 
  pivot_longer(-Species)

dt %>% 
  mutate(name = gsub("\\.", "<br>", name)) %>% 
  ggplot(aes(value)) + 
  geom_density() + 
  ggh4x::facet_grid2(name ~ Species, scales = 'free') + 
  theme(strip.text.y = ggtext::element_markdown(angle = 0))

Created on 2024-03-25 with reprex v2.1.0

danielcoral commented 3 months ago

Thanks a lot @teunbrand !