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

Different alignment of legend labels when removing the legend title #2739

Closed
Eluvias opened this issue Jul 7, 2018 · 6 comments
Closed

Different alignment of legend labels when removing the legend title #2739

Eluvias opened this issue Jul 7, 2018 · 6 comments
Labels
feature a feature request or enhancement guides 📏

Comments

@Eluvias
Copy link

Eluvias commented Jul 7, 2018

# v3.0.0
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.1

p <- ggplot(mpg, aes(class, fill = class)) + 
     geom_bar()

p

# different alignment of legend labels
p + theme(legend.title = element_blank())

Created on 2018-07-07 by the reprex package (v0.2.0).

@clauswilke
Copy link
Member

What is happening is that the size of the spacing is calculated from the font size of the legend title, and where there is none the font size is set to zero. These are the relevant lines of code:

ggplot2/R/guide-legend.r

Lines 330 to 336 in 3d022ed

title_fontsize <- title.theme$size %||% calc_element("legend.title", theme)$size %||% 0
# gap between keys etc
# the default horizontal and vertical gap need to be the same to avoid strange
# effects for certain guide layouts
hgap <- width_cm(theme$legend.spacing.x %||% (0.5 * unit(title_fontsize, "pt")))
vgap <- height_cm(theme$legend.spacing.y %||% (0.5 * unit(title_fontsize, "pt")))

Arguably, it would make sense to try the general font size from the generic text theme element. However, as the code shows, you can work around this issue by setting legend.spacing.x and legend.spacing.y. By default they are set to half the font size.

library(ggplot2)

ggplot(mpg, aes(class, fill = class)) + 
  geom_bar() + 
  theme(
    legend.title = element_blank(),
    legend.spacing.x = unit(11/2, "pt") # default font size is 11pt
  )

Created on 2018-07-07 by the reprex package (v0.2.0).

@ptoche
Copy link

ptoche commented Jul 7, 2018

Another workaround (trivial remark here): If you do not need to go through a theme(), you can reset the title with the guides, something like p + guides(fill = guide_legend(title = "")) or p + guides(fill = guide_legend(title = NULL)).

@clauswilke
Copy link
Member

@ptoche I'd always recommend setting titles to NULL rather than to "" since the former completely eliminates the title whereas the latter reserves space and just doesn't print anything there.

Exaggerated example:

library(ggplot2)

ggplot(mpg, aes(class, fill = class)) + 
  geom_bar() + 
  scale_fill_discrete(name = NULL) +
  theme(legend.title = element_text(size = 500),
        legend.spacing.x = unit(5.5, "pt"),
        legend.spacing.y = unit(5.5, "pt"))

ggplot(mpg, aes(class, fill = class)) + 
  geom_bar() + 
  scale_fill_discrete(name = "") +
  theme(legend.title = element_text(size = 500),
        legend.spacing.x = unit(5.5, "pt"),
        legend.spacing.y = unit(5.5, "pt"))

Created on 2018-07-07 by the reprex package (v0.2.0).

@paleolimbot
Copy link
Member

It's worth noting that #3180 is also about the vertical spacing of items in guide_legend()

@paleolimbot paleolimbot added feature a feature request or enhancement guides 📏 labels Jun 17, 2019
@clauswilke
Copy link
Member

This was fixed for 3.2.0. Apologies for not closing the issue.

library(ggplot2)

ggplot(mpg, aes(class, fill = class)) + 
  geom_bar() +
  theme(legend.title = element_blank())

Created on 2019-06-17 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       macOS Mojave 10.14.5        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2019-06-17                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.4   2019-04-10 [1] CRAN (R 3.6.0)
#>  callr         3.2.0   2019-03-15 [1] CRAN (R 3.6.0)
#>  cli           1.1.0   2019-03-19 [1] CRAN (R 3.6.0)
#>  colorspace    1.4-1   2019-03-18 [1] CRAN (R 3.6.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.0)
#>  curl          3.3     2019-01-10 [1] CRAN (R 3.6.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.0)
#>  devtools      2.0.2   2019-04-08 [1] CRAN (R 3.6.0)
#>  digest        0.6.19  2019-05-20 [1] CRAN (R 3.6.0)
#>  dplyr         0.8.1   2019-05-14 [1] CRAN (R 3.6.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.0)
#>  fs            1.3.1   2019-05-06 [1] CRAN (R 3.6.0)
#>  ggplot2     * 3.2.0   2019-06-16 [1] CRAN (R 3.6.0)
#>  glue          1.3.1   2019-03-12 [1] CRAN (R 3.6.0)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 3.6.0)
#>  highr         0.8     2019-03-20 [1] CRAN (R 3.6.0)
#>  htmltools     0.3.6   2017-04-28 [1] CRAN (R 3.6.0)
#>  httr          1.4.0   2018-12-11 [1] CRAN (R 3.6.0)
#>  knitr         1.23    2019-05-18 [1] CRAN (R 3.6.0)
#>  labeling      0.3     2014-08-23 [1] CRAN (R 3.6.0)
#>  lazyeval      0.2.2   2019-03-15 [1] CRAN (R 3.6.0)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.0)
#>  mime          0.7     2019-06-11 [1] CRAN (R 3.6.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 3.6.0)
#>  pillar        1.4.1   2019-05-28 [1] CRAN (R 3.6.0)
#>  pkgbuild      1.0.3   2019-03-20 [1] CRAN (R 3.6.0)
#>  pkgconfig     2.0.2   2018-08-16 [1] CRAN (R 3.6.0)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.6.0)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.6.0)
#>  processx      3.3.1   2019-05-08 [1] CRAN (R 3.6.0)
#>  ps            1.3.0   2018-12-21 [1] CRAN (R 3.6.0)
#>  purrr         0.3.2   2019-03-15 [1] CRAN (R 3.6.0)
#>  R6            2.4.0   2019-02-14 [1] CRAN (R 3.6.0)
#>  Rcpp          1.0.1   2019-03-17 [1] CRAN (R 3.6.0)
#>  remotes       2.0.4   2019-04-10 [1] CRAN (R 3.6.0)
#>  rlang         0.3.4   2019-04-07 [1] CRAN (R 3.6.0)
#>  rmarkdown     1.13    2019-05-22 [1] CRAN (R 3.6.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.0)
#>  scales        1.0.0   2018-08-09 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.0)
#>  stringi       1.4.3   2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat      2.1.1   2019-04-23 [1] CRAN (R 3.6.0)
#>  tibble        2.1.3   2019-06-06 [1] CRAN (R 3.6.0)
#>  tidyselect    0.2.5   2018-10-11 [1] CRAN (R 3.6.0)
#>  usethis       1.5.0   2019-04-07 [1] CRAN (R 3.6.0)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun          0.7     2019-05-14 [1] CRAN (R 3.6.0)
#>  xml2          1.2.0   2018-01-24 [1] CRAN (R 3.6.0)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.6.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

@lock
Copy link

lock bot commented Dec 14, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement guides 📏
Projects
None yet
Development

No branches or pull requests

4 participants