Skip to content
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

element_textbox() breaks for facet grid labels when switching positions #79

Closed
jdtrat opened this issue Feb 26, 2022 · 2 comments
Closed

Comments

@jdtrat
Copy link

jdtrat commented Feb 26, 2022

Hiya! Thanks so much for such an incredible package. I use ggtext (and cowplot) on a regular basis! I ran into a problem keeping the coloring for facet labels when I use the switch argument in facet_grid() as outlined [here]. Do you know what might be causing this? Reprex below:

library(palmerpenguins)
library(ggplot2)
library(ggtext)

penguin_colors <- c("darkorange", "purple", "cyan4")

base_plot <- ggplot(penguins, 
                    aes(x = sex, y = flipper_length_mm, color = species)) +
  geom_point() +
  scale_color_manual(values = penguin_colors) +
  theme_classic() +
  theme(
    legend.position = "none",
    strip.background = element_blank(),
    strip.text.x = element_textbox(
      size = 16, face = "bold",
      color = "black", fill = "white", box.color = "black",
      halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
      padding = margin(3, 0, 1, 0), margin = margin(3, 3, 3, 3)
    ),
    strip.text.y = element_textbox(
      size = 16, face = "bold",
      valign = 0.5,
      halign = 1, width = unit(0.2, "npc"),
      margin = margin(0, -25, 0, -30)
    ))


base_plot +
  facet_grid(species ~ island,
             scales = "free_y",
             labeller = labeller(
               .rows = as_labeller(~ glue::glue("<span style='color: {penguin_colors};'> {.x} </span>")),
               .cols = as_labeller(~ glue::glue("Island of {.x}"))
               )
             )
#> Warning: Removed 2 rows containing missing values (geom_point).

# Switching the axes to be on the left side removes the coloring
base_plot +
  facet_grid(species ~ island,
             scales = "free_y",
             switch = "y",
             labeller = labeller(
               .rows = as_labeller(~ glue::glue("<span style='color: {penguin_colors};'> {.x} </span>")),
               .cols = as_labeller(~ glue::glue("Island of {.x}"))
             )
  )
#> Warning: Removed 2 rows containing missing values (geom_point).

Created on 2022-02-26 by the reprex package (v2.0.1)

@teunbrand
Copy link

This can be fixed by setting the strip.text.y.left theme element. It's an issue of theme settings not inheriting the 'richer' element. See also tidyverse/ggplot2#5457.

@bwiernik
Copy link
Collaborator

bwiernik commented Oct 7, 2023

Yep this is a ggplot2 bug, so closing this here

@bwiernik bwiernik closed this as completed Oct 7, 2023
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

3 participants