# Legend of a plot: how to increase the size of the line/marker?

I'm using the following kind of code to do the legends of my plots:

PlotLegend -> {"Test1","Test2"}, LegendPosition -> {0.05,
0.2}, LegendBackground -> White, LegendBorder -> White, \
ShadowBackground -> White, LegendSize -> {0.75 ,
0.35}, LegendBorder -> None, LegendTextSpace -> 3


Which gives something like

I would like to increase the size (width) of the line (color/type) of one item of the legend.

I know it depends on the size of the whole box, but is it possible to control that independently ?

EDIT

The "package" of Jens looks like the perfect solution for me. I used it to produce the following plot. In this case I use only one legend entry for both the data and the corresponding fit, but as he mentions in his answer they can be split.

-
I strongly recommend dumping PlotLegends for something more thorough, such as a homebrew solution. See mathematica.stackexchange.com/questions/4025/… and mathematica.stackexchange.com/questions/4444/… to name a few –  tkott Apr 19 '12 at 16:30
@tkott, I've just updated my post to address this type of question. I had worked on it last night and planned to do some more, but decided to update what I have now. I'll try to post an example as an answer here to show how the sizes can be changed. Although these two questions are closely related, the size issue wasn't in the earlier question linked here. I just happened to address it in the other post... –  Jens Apr 19 '12 at 16:44
@all: Indeed I should dump PlotLegends and go for another solution. Jens' one seems to do exactly what I want ! –  Cedric H. Apr 20 '12 at 7:37

If you're willing to abandon PlotLegends (which tkott advises in the comment and many others would advise too), then you could work with the code posted in this related answer to achieve some customization. You'll have to execute the definitions in that post before trying the lines below.

Take the following two plots which generate the same curve first as a line and then as points from a table, using plot markers:

p = Plot[Evaluate[Table[1/x + i, {i, 0, 3, 1}]], {x, 0, 3},
PlotRange -> {0, 3}];
q = ListPlot[
Table[Evaluate@Table[{x, 1/x + i}, {x, .5, 3, .5}], {i, 0, 3, 1}],
PlotRange -> {0, 3}, PlotMarkers -> Automatic];


Now simply say

autoLegend[Show[p, q], {"Asymptote 1", "Asymptote 2", "Asymptote 3"}]


and the result is this:

You could replace autoLegend by deployLegend above if you want to be able to edit the result as a graphic. To customize the sizes inside the legend, I'll add some options:

autoLegend[Show[p, q], {"Asymptote 1", "Asymptote 2", "Asymptote 3"},
"LegendLineWidth" -> 20,
"LegendLineAspectRatio" -> .5,
"LegendGridOptions" -> {Alignment -> Left, Spacings -> {1, 1}}]


The "LegendGridOptions" setting takes the same options as Grid and determines the horizontal and vertical space between entries in the legend. The "LegendLineWidth" and "LegendLineAspectRatio" have to be changed in tandem to get the markers and lines to fill the space properly.

Edit

If the two plots p and q represent independent data (such as experimental data versus theory curves), then the legend should have six instead of three entries. This is something autoLegend isn't able to figure out, so we have to do it using the lower-level function legendMaker. It needs to be given the styles for the lines and markers explicitly, as lists of six entries each. To specify that there should be no line or no marker, use the entry None.

The following example first defines the text for the legend, and the styles that appear in the two plots:

textLabels =
Map[Style[#, FontFamily -> "Helvetica",
GrayLevel[.9]] &, {"Asymptote 1", "Asymptote 2", "Asymptote 3",
"Data 1", "Data 2", "Data 3"}];

plotstyles = Map[ColorData[1][#] &, Range[3]];

manualPlotstyles = Join[plotstyles, {None, None, None}];

markers = {Style["\[FilledCircle]", Hue[0.67, 0.6, 0.6],
FontSize -> 8.96],
Style["\[FilledSquare]", Hue[0.9060679774997897, 0.6, 0.6],
FontSize -> 8.96],
Style["\[FilledDiamond]", Hue[0.1421359549995791, 0.6, 0.6],
FontSize -> 10.88]};

manualMarkers = Join[{None, None, None}, markers];


In the variables manualPlotstyles and manualMarkers, I added None in the list positions where either the line or the marker should be omitted. The result is overlayed with the combined plots (produced by Show):

Overlay[{
Show[p, q, PlotLabel -> Style["Comparison", "Subsection"]],
legendMaker[
textLabels,
PlotStyle -> manualPlotstyles,
PlotMarkers -> manualMarkers,
Background -> Directive[Opacity[.5], Black],
"LegendLineAspectRatio" -> .3,
"LegendGridOptions" -> {Alignment -> Left, Spacings -> {.7, .1},
Background -> {{GrayLevel[.8], None}, None}
}
]},
Alignment -> {-.8, -.7}]


This shows how some of the optional parameters can be used to get a different legend appearance.

-

If you aren't keen to write your own custom function for legends, there is always the ShowLegend version of the legends functionality in the PlotLegends package. It is just as slow as the PlotLegend option, but seems to offer a bit more control over the markers. It can also be used with the full array of plotting functions, not just the Plot, ListPlot and related functions.

In your specific case, you can get control over the length of the key lines by setting their coordinates appropriately, for example Line[{{0, 0.5}, {1.4, 0.5}}]. It took a bit of trial and error to get those numbers, but there might be a general principle behind them, relating the higher x-coordinate to the LegendSize and LegendTextSpace option values.

ShowLegend[
Plot[{Sin[x], Cos[x]}, {x, -2 \[Pi], 2 \[Pi]},
PlotStyle -> {Red,
Blue}], {{{Graphics[{Blue, AbsoluteThickness[2],
Line[{{0, 0.5}, {1.4, 0.5}}]}],
Sin[x]}, {Graphics[{Red, AbsoluteThickness[2],
Line[{{0, 0.5}, {1.4, 0.5}}]}], Cos[x]}},
LegendPosition -> {0.5, 0.2}, LegendBackground -> White,
LegendBorder -> None, LegendShadow -> None,
LegendSize -> {0.45, 0.35}, LegendBorder -> None,
LegendTextSpace -> 0.6} ]


-