Skip to content

Using margin to adjust legend.text? #1502

@Henrik-P

Description

@Henrik-P

In issue #1435 on adjusting element_text, we find that "vjust and hjust no longer work. Instead, use the margin() parameter of element_text()". Does this apply also for legend.text?

I have a plot in which I want to adjust the legend.text horizontally.

df <- data.frame(x = c("a", "b"), y = 1)

p <- ggplot(df, aes(x = x, y = y, fill = x)) +
   geom_bar(stat = "identity") +
   ggtitle("My plot")
p

I followed the suggestion in #1435: "if you're having problems understanding the layout, try debug = TRUE":

p + theme(text = element_text(debug = TRUE))

I am not entirely sure I interpret the "debugging plot" correctly, but I thought it indicated all text elements which could be adjusted. Here, areas corresponding to axis.text, axis.title, and plot.title, are highlighted, but seemingly not those for legend.text (nor for legend.title).

I have tried to "push" and "pull" the legend.text horizontally using

p + theme(legend.text = element_text(margin = margin(l =  , r =  )))

...with various combinations of l and r, to no avail.

Thus my question: is legend.text (and legend.title) not included among the element_text elements which can be adjusted using margin? Which is the current ''official' way to adjust these elements?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions