1

I'd like a ggplot2 legend to feature keys of a constant width, no matter the size of the associated label.

For instance

 library(plyr)
 library(tidyverse)

 mtcars %>% 
   group_by(cyl, gear) %>%
   tally %>% 
   mutate(
     gear = gear %>% 
     plyr::mapvalues(
       3:5,
       c(
         "3 g short",
         "4 gears medium",
         "Five gears long label"
       )
     )  
   ) %>% 
   ggplot(
     aes(factor(cyl), n, fill = gear)
   ) +
   geom_col(
     color = "grey10",
     size = .1
   ) +
   scale_fill_grey(
     guide = guide_legend(label.position = "bottom")
   ) +
   theme_minimal() +
   theme(
     legend.position = "bottom"
   )

returns

figure_1

The three shades of grey fill are shown as having different widths, as a function of the label length. How might I ensure that the fill key entries are all the same size?

tomw
  • 3,114
  • 4
  • 29
  • 51
  • I've struggled with this myself and seen a few posts with hacks, such as https://stackoverflow.com/q/52979035/5325862 – camille Nov 18 '19 at 21:19
  • I think that's a duplicate, although I also think it is not very satisfying. If there is a better answer it should go at the other question. – Axeman Nov 18 '19 at 21:21
  • @Axeman I agree. I think the answer is probably that there isn't a great answer. Might be worth a bounty... – camille Nov 18 '19 at 21:22
  • I think a github issue requesting that `keywidth` is respected in all cases would be nice. – Axeman Nov 18 '19 at 21:24

0 Answers0