Skip to content

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

@Henrik-P

Description

@Henrik-P

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)))                                                                                                                                

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorhelp wanted ❤️we'd love your help!themes 💃

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions