3
$\begingroup$

I have a graph with four curves and I want to custom order their appearance in the legend. In particular, in the graph below,

Plot[{(1 - bet) 1/(2 Sqrt[bet]), Sqrt[bet], bet^(1/4), (1 - bet) 1/(4 bet^(3/4))}, {bet, 0, 1}, 
 PlotLegends -> Placed[{"MR1", "MC1", "MR2", "MC2"}, Below], 
 AxesLabel -> {"\[Beta]", "MR,MC"}, Ticks -> {Automatic, None}, 
 Epilog -> {Inset[Text[Style["\!\(\*SuperscriptBox[\(\[Beta]\), \(*\)]\)", 
  FontSize -> 10]], {1/3, -0.075}], 
  Dashed, [{{1/3, 1/Sqrt[3]}, {1/3, 0}}]}]

the legends are showing up by row, but I would like them to show up by column instead like:

(blue)   MR1 Lorem Ipsum   (green) MR2 Lorem Ipsum
(orange) MC1 Lorem Ipsum   (red)   MC2 Lorem Ipsum

After searching hard, it seemed to me that I could switch the order of the curves and manually reassign colors from Wolfram's default colors like so:

Plot[{(1 - bet) 1/(2 Sqrt[bet]), bet^(1/4),  Sqrt[bet], , (1 - bet) 1/(4 bet^(3/4))}, {bet, 0, 1}, 
 PlotLegends -> Placed[{"MR1 lorem ipsum", "MR2 lorem ipsum", "MC1 lorem ipsum", 
    "MC2 lorem ipsum"}, Below], AxesLabel -> {"\[Beta]", "MR,MC"}, 
 PlotStyle -> {{{RGBColor[0.368417`, 0.506779`, 0.709798`]}} ,
     RGBColor[0.560181`, 0.691569`, 0.194885`], 
     RGBColor[0.880722`, 0.611041`, 0.142051`], 
     RGBColor[0.922526`, 0.385626`, 0.209179`]}, 
 Ticks -> {Automatic, None}, 
 Epilog -> {Inset[Text[Style["\!\(\*SuperscriptBox[\(\[Beta]\), \(*\)]\)", 
      FontSize -> 10]], {1/3, -0.075}], Dashed, 
   Line[{{1/3, 1/Sqrt[3]}, {1/3, 0}}]}]

which works for the application, but this would be extremely long and painful if there were many such curves. Generally, if one has a long list of curves in a graph but wants to customize the order of appearance of the legends, how does one do this? There must be a way to do this more conveniently than plotting first and then seeing how it looks, and then having to change the order manually and reassigning all the colors again. I'm new so please excuse if this question is too trivial / redundant. I tried reading seemingly related posts but couldn't find/understand the answer I was looking for. Many of them seemed to just talk about how to reverse the order. Thank you.

plot

Update: Thank you all for your kind answers (sorry I don't have enough ref points to upvote yet). kguler's and belisarius' answers are correct; theirs transpose the legend matrix. I really like Mike Honeychurch's answer: it allows for custom ordering of the curves by simply referring to them by their index numbers, which is quite nice. Below is the implementation. But is there a way to use such a matrix index ordering method with LegendLayout(Grid@...)?

Plot[{(1 - bet) 1/(2 Sqrt[bet]), bet^(1/4), 
  Sqrt[bet], (1 - bet) 1/(4 bet^(3/4))}, {bet, 0, 1}, 
 PlotLegends -> 
  Placed[LineLegend[
    ColorData[97, "ColorList"][[{1, 3, 2, 4}]], {"MR1 lorem ipsum", 
      "MC1 lorem ipsum", "MR2 lorem ipsum", 
      "MC2 lorem ipsum"}[[{1, 3, 2, 4}]]], Below], 
 AxesLabel -> {"\[Beta]", "MR,MC"}, 
 PlotStyle -> ColorData[97, "ColorList"][[{1, 3, 2, 4}]], Ticks -> {Automatic, None}]

plot2

Update2: @kguler @belisarius @MikeHoneychurch Thank you all for your gracious help.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Dec 10 '14 at 23:02
  • $\begingroup$ I just deleted my answer but the way to do what you ask in the edit is to use @kgulers LegendLayout where I have written "Column". But in that case the index order will be {1, 2, 3, 4} $\endgroup$ – Mike Honeychurch Dec 10 '14 at 23:20
  • $\begingroup$ @MikeHoneychurch Why did you do that? I was going to accept yours (I don't have enough reputation points to do so as a newb) because yours actually allow for custom ordering. Could you be a bit more specific? I want to do something like LegendLayout -> [[{1, 3, 2, 4}]]] rather than LegendLayout -> (Grid@Transpose@Partition[Row /@ #, 2] &) but that doesn't work. $\endgroup$ – user174118 Dec 10 '14 at 23:22
  • $\begingroup$ @user174118 Take a look at the edit in my answer $\endgroup$ – Dr. belisarius Dec 10 '14 at 23:38
  • $\begingroup$ ok I undeleted it and made the revision $\endgroup$ – Mike Honeychurch Dec 11 '14 at 0:19
1
$\begingroup$
Plot[{(1 - bet) 1/(2 Sqrt[bet]), Sqrt[bet], 
  bet^(1/4), (1 - bet) 1/(4 bet^(3/4))}, {bet, 0, 1}, 
 PlotLegends -> 
  Placed[LineLegend[
    ColorData[97, "ColorList"][[{1, 2, 3, 4}]], {"MR1 lorem ipsum", 
      "MC1 lorem ipsum", "MR2 lorem ipsum", 
      "MC2 lorem ipsum"}[[{1, 2, 3, 4}]], 
    LegendLayout -> (Grid@Transpose@Partition[Row /@ #, 2] &)], 
   Below], AxesLabel -> {"\[Beta]", "MR,MC"}, 
 PlotStyle -> ColorData[97, "ColorList"], Ticks -> {Automatic, None}]

enter image description here

From @kguler

Alternatively you can use:

PlotLegends -> Placed[LineLegend[Automatic, {"MR1", "MC1", "MR2", "MC2"}, 
   LegendLayout -> (Grid[Row /@ # & /@ {#[[{1, 3}]], #[[{2, 4}]]}] &)], Below]

or

PlotLegends -> Placed[LineLegend[Automatic, {"MR1", "MC1", "MR2", "MC2"}, 
    LegendLayout -> (Grid[Partition[Row /@ #[[{1, 3, 2, 4}]], 2]] &)],  Below],
$\endgroup$
  • $\begingroup$ @kguler I prefer your answer. I've added some additional stuff from your answer $\endgroup$ – Mike Honeychurch Dec 11 '14 at 4:00
0
$\begingroup$

I believe this one is simple enough:

Plot[{bet, bet^2, bet^3, bet^4}, {bet, 0, 1}, 
  PlotLegends -> Placed[{"MR1", "MC1", "MR2", "MC2"}, Below]] /. 
  "Row" :> (Grid[Apply[Sequence, Partition[#, 2], {2}]] &)

Mathematica graphics

Edit:

Here you can see how you can reorder the legends easily

Plot[{bet, bet^2, bet^3, bet^4}, {bet, 0, 1}, 
  PlotLegends -> Placed[{"MR1", "MC1", "MR2", "MC2"}, Below]] /. 
  "Row" :> (Grid[ Apply[Sequence, Partition[#[[{1, 3, 2, 4}]], 2], {2}]] &)

Mathematica graphics

$\endgroup$
0
$\begingroup$

LegendLayout -> {"Column",2} does exactly what you wanted.

Plot[{(1 - bet) 1/(2 Sqrt[bet]), Sqrt[bet], 
  bet^(1/4), (1 - bet) 1/(4 bet^(3/4))}, {bet, 0, 1}, 
 PlotLegends -> 
  Placed[LineLegend[{"MR1", "MC1", "MR2", "MC2"}, 
    LegendLayout -> {"Column", 2}], Below],
 AxesLabel -> {"\[Beta]", "MR,MC"}, Ticks -> {Automatic, None}]

2 column layout

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.