# Plots.jl Align legend labels, :right not working

**URL:** https://discourse.julialang.org/t/plots-jl-align-legend-labels-right-not-working/60493
**Category:** Visualization
**Tags:** plotting, recipe
**Created:** [May 4, 2021, 1:51am UTC](https://discourse.julialang.org/t/plots-jl-align-legend-labels-right-not-working/60493 "2021-05-04T01:51:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![VMHidalgo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vmhidalgo/32/23055_2.png) [@VMHidalgo](https://discourse.julialang.org/u/VMHidalgo)
#### Post date: [May 4, 2021, 1:51am UTC](https://discourse.julialang.org/t/plots-jl-align-legend-labels-right-not-working/60493/1 "2021-05-04T01:51:37Z")

</div>

Hi,

I am getting familiar with the Plots recipes ecosystem, and I just wrote my first recipe but the attribute “legendfonthalign” does not seem to work. Does anyone know how to do it?

Here is a MWE

```julia
@recipe function f(g::CVEpdfPlot)
    legend --> false
    legendfonthalign := :right # this should work!!! right?
    grid := false
    linewidth := 1
    linestyle := :solid

    @series begin
        seriestype := :density
        [g.args...]
    end
end

```

And when I call that plot (with some extra features, like the rainbow color) I get this,with the legend text aligned to the :left ,

 ![rawGCVEpdfshalf](https://global.discourse-cdn.com/julialang/original/3X/8/a/8a744b4de08cae9399f80354d322765dbbc1b9a8.png)

what maybe happening?

Thank you

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 4, 2021, 7:44am UTC](https://discourse.julialang.org/t/plots-jl-align-legend-labels-right-not-working/60493/2 "2021-05-04T07:44:07Z")

</div>

Does it work when you pass it explicitly like `plot(my_cve_object, legendfonthalign = :right)`?

Also which backend are you using? Note that `legendfonthalign` is only supported by GR and PGFPlotsX according to [the docs](https://docs.juliaplots.org/latest/generated/supported/).

That said, just doing `plot(rand(10, 10), legendfonthalign = :right)` doesn’t seem to do anything for me even with the GR backend, so maybe this isn’t the way to use the attribute, or there’s a bug somewhere in Plots…

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 4, 2021, 9:41am UTC](https://discourse.julialang.org/t/plots-jl-align-legend-labels-right-not-working/60493/3 "2021-05-04T09:41:04Z")

</div>

No success either with `legendfonthalign` attribute.

A workaround is to left-pad the labels beforehand and use a font such as Courier so that the columns of characters are consistently aligned.  
Computer Modern legends are not perfectly aligned but look better:

```julia
using LaTeXStrings, Plots; gr()

default(fontfamily="Computer Modern")
n = 3:2:23
x = 0:0.01:1; y = [3*n*exp.(-(n*(x .- 0.6)).^4) for n in n];
labels= permutedims(string.(n) .* " [s]")
n = maximum(length.(labels))
plot(x, y, label=lpad.(labels,n), legend=:outertopright) # legendfont=font(6,"Courier")
plot!(xlabel=L"CVE",ylabel=L"Density") 

```

Legend with Courier font:  
 ![Plots_gr_legend_right_justification1](https://global.discourse-cdn.com/julialang/original/3X/f/e/fee1f6e6e5df84c017bbcb9e0a1eb65be7789234.png)

Legend with Computer Modern font:  
 ![Plots_gr_legend_right_justification2](https://global.discourse-cdn.com/julialang/original/3X/9/0/906eebec856ff1068fd59884e9e323a7ecd4bac0.png)

---

<div class="post-metadata">

### Author: ![VMHidalgo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vmhidalgo/32/23055_2.png) [@VMHidalgo](https://discourse.julialang.org/u/VMHidalgo)
#### Post date: [May 4, 2021, 10:49pm UTC](https://discourse.julialang.org/t/plots-jl-align-legend-labels-right-not-working/60493/4 "2021-05-04T22:49:22Z")

</div>

@nilshg @rafael.guerra I appreciate your help guys.

I am using gr() actually, and the lpad solution seems to be good enough at this point. I can save it as a SVG and then align it by hand. Obviously a Plots.jl solution would be ideal.

Thank you!
