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

No default angle in element_text when using label.theme and title.theme in guide_legend leads to error when setting other parameters #2544

Closed
Henrik-P opened this issue Apr 29, 2018 · 5 comments

Comments

@Henrik-P
Copy link
Contributor

@Henrik-P Henrik-P commented Apr 29, 2018

When using guide = guide_legend(...) to change non-angle parameters of label.theme or title.theme, an error is generated due to non-NULL value for angle.

Here are examples trying to set face, without setting angle:

library(ggplot2)                                                                                                                                                       
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +                                                                                               
geom_point() +                                                                                                                                                         
scale_color_discrete(guide = guide_legend(label.theme = element_text(face = "italic")))                                                                                
#> Error in element_grob.element_text(element = label.theme, label = label, : Text element requires non-NULL value for 'angle'.
                                                                                                                                                                       
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +                                                                                               
geom_point() +                                                                                                                                                         
scale_color_discrete(guide = guide_legend(title.theme = element_text(face = "italic")))                                                                                
#> Error in element_grob.element_text(guide$title.theme %||% calc_element("legend.title", : Text element requires non-NULL value for 'angle'.

(reprex inserts an 'empty plot placeholder' here, despite the code above errors...)

Trying to set size, hjust or vjust without setting angle leads to the same error.

To change any non-angle parameters, we need to set angle as well. E.g.:

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +                                                                                               
geom_point() +                                                                                                                                                         
scale_color_discrete(guide = guide_legend(label.theme = element_text(face = "italic", angle = 0)))                                                                     


In contrast, there is no need to explicitly set any of the other NULL arguments in element_text when setting angle only:

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +                                                                                               
geom_point() +                                                                                                                                                         
scale_color_discrete(guide = guide_legend(                                                                                                                             
label.theme = element_text(angle = -5)))                                                                                                                               

                                                                                                                                                                       
                                                                                                                                                                       
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +                                                                                               
geom_point() +                                                                                                                                                         
scale_color_discrete(guide = guide_legend(                                                                                                                             
title.theme = element_text(angle = 5)))                                                                                                                                

@Henrik-P Henrik-P changed the title No default angle in element_text when using label.theme and title.theme in guide_legend -> Error when setting other parameters No default angle in element_text when using label.theme and title.theme in guide_legend leads to error when setting other parameters Apr 29, 2018
@daniel-barnett
Copy link
Contributor

@daniel-barnett daniel-barnett commented May 4, 2018

title.theme <- guide$title.theme %||% calc_element("legend.title", theme)

label.theme <- guide$label.theme %||% calc_element("legend.text", theme)

These two lines seem to be the problem. Once you change at least one parameter, guide$title.theme isn't NULL anymore so guide$title.theme$angle will still be NULL when it is used as element$angle here (same applies for label.theme):

ggplot2/R/theme-elements.r

Lines 206 to 209 in 9034d53

angle <- angle %||% element$angle
if (is.null(angle)) {
stop("Text element requires non-NULL value for 'angle'.")
}

Adding guide$title.theme$angle <- guide$title.theme$angle %||% calc_element("legend.title", theme)$angle on the next line removes the problem, but there's probably a more elegant fix than this.

@clauswilke
Copy link
Member

@clauswilke clauswilke commented May 7, 2018

I think the correct fix is to replace lines 206-209 in theme-elements.r with:

angle <- angle %||% element$angle %||% 0

It's reasonable to always assume that if no angle is given then it's zero.

@hadley
Copy link
Member

@hadley hadley commented May 7, 2018

@clauswilke's sounds like the right fix to me. Would anyone like to do a PR?

@clauswilke
Copy link
Member

@clauswilke clauswilke commented May 7, 2018

Done.

@lock
Copy link

@lock lock bot commented Nov 3, 2018

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 Nov 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants