简体   繁体   中英

y-axis label inside strip label with facet_grid

Imagine for a moment that there is some good reason to have a variable like mpg on different scales for each row in a faceted ggplot.

How can I put the y-axis label inside the facet strip labels, so that it is very clear that the units are described by the y-axis label but not the facet? Suggestions that modify the code below especially welcomed.

As you're imagining this scenario, please also imagine there is a good reason not to have the strips on the right, and not to have the double strip eg that would occur with facet_wrap , and help me get "miles per gallon" between "mini", "modest", and "muscle" and the y-axis ticks:-) thanks!

library(tidyverse)
data(mtcars)

mtcars %>% 
    mutate(transmission=c("real", "robot")[am+1]
           , engine=c("mini", "modest", "muscle")[cyl/2-1]) %>% 
    ggplot(aes(wt*1000, mpg)) +
    geom_point() +
    facet_grid(engine ~ transmission, switch = "y", scales = "free") +
    labs(x="vehicle weight (lbs)", y = "miles per gallon" ) +
    theme_classic() +
    theme( panel.spacing=unit(2, "lines")
          , strip.placement.y = "outside"
          , strip.background = element_blank()
          , strip.text = element_text(face = "bold")
          )

My approach: Include units in facet names, then enter insert line breaks between name and unit.

library(tidyverse)
data(mtcars)

data <- mtcars %>% 
  mutate(transmission=c("real", "robot")[am+1]
         , engine=c("mini", "modest", "muscle")[cyl/2-1]

         # Later, we're going to insert the line break at a fixed point. To prepare:
         # Change strings to a consistent width, one greater than the length of the longest string. 
         # In this case, the longest name is 6 characters long, 
         # so we'll pad the strings to be 7 characters wide. 
         , engine_l = str_pad(engine, width = 7, side = "right") %>%
                      # add unit to end of string
                      paste0("mpg")
         ,
         # check that string length is consistent - should be 10 for all rows.
         str_length(engine_l))


data %>%
  ggplot(aes(wt*1000, mpg)) +
  geom_point() +
  facet_grid(engine_l ~ transmission, switch = "y", scales = "free",

             # the labeller feature allows us to customize the facet labels. 
             # In this case, we enter a line break after 6 characters, the length of the longest name.
             labeller = labeller(engine_l = label_wrap_gen(6))) +

  # remove units from y axis label
  labs(x="vehicle weight (lbs)", y = "" ) +
  theme_classic() +
  theme( panel.spacing=unit(2, "lines")
         , strip.placement.y = "outside"
         , strip.background = element_blank()
         , strip.text = element_text(face = "bold")
  )

刻面标签中带有单位的图形输出

I've played around with getting it to say "miles per gallon" in place of "mpg", and haven't been able to crack that part yet. Maybe someone else will have thoughts on this. :)

Also, here's more information on the labeller: https://ggplot2.tidyverse.org/reference/labeller.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM