简体   繁体   English

带有 facet_grid 的条带标签内的 y 轴标签

[英]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.想象一下,有充分的理由在多面 ggplot 中的每一行都有一个像 mpg 这样的变量在不同的尺度上。

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?如何将 y 轴标签放在 facet strip 标签内,以便很清楚单位是由 y 轴标签而不是 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!当您在想象这种情况时,也请想象有充分的理由不在右侧放置条带,并且不在facet_wrap中出现双条带,并帮助我在“之间获得“每加仑英里数” mini”、“modest”、“muscle”和 y 轴滴答作响:-) 谢谢!

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.我试过让它用“每加仑英里数”代替“mpg”,但还没能破解这部分。 Maybe someone else will have thoughts on this.也许其他人会对此有想法。 :) :)

Also, here's more information on the labeller: https://ggplot2.tidyverse.org/reference/labeller.html此外,这里还有关于贴标机的更多信息: https ://ggplot2.tidyverse.org/reference/labeller.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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