Skip to content

Aesthetic improvement: left-justified legends #500

@grantmcdermott

Description

@grantmcdermott

Something that's bothered me for a while is the fact that legends are center-justified. This didn't look too bad with a single legend, but has become more of an eyesore with dual legends IMO, since the respective symbols and labels can become quite misaligned.

library(tinyplot)

plt(
  Sepal.Length ~ Petal.Length | Species, iris, 
  cex = iris$Petal.Width
)

Left-justifying legends is a little tricky, since the base legend function doesn't support this out of the gate. (The available adjustment args like adj, x.intersp, etc. affect separate component of the legend.) But you can facilitate the same effect by adjusting the text.width and title.adj arguments:

plt(
  Sepal.Length ~ Petal.Length | Species, iris,
  cex = iris$Petal.Width,
  legend = list("right!", title.adj = 0,text.width = strwidth("Petal.Width"))
)

Aside: In looking into this issue, I discovered that there's actually an example in the ?legend docs that demonstrates this kind of manual adjustment.

It would require some internal accounting, but I'm inclined to make this left-justified behaviour the default for all of our legends.

Thoughts?

P.S. It also works for continuous legends, where there is an even more marked aesthetic improvement IMO.

plt(
  Sepal.Length ~ Petal.Length | Sepal.Width, iris, 
  cex = iris$Petal.Width
)

plt(
  Sepal.Length ~ Petal.Length | Sepal.Width, iris,
  cex = iris$Petal.Width,
  legend = list("right!", title.adj = 0,text.width = strwidth("Petal.Width"))
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions