4
$\begingroup$

I'm trying to put the finishing touches on a grid of figures and could use some help. I'm trying to keep the figures as clean as possible, so I only want frame ticks and labels on the edge figures. However, when I do this, the figures on the far left and right are shrunk relative to the middle ones. I'd love to know how to fix this.

Here's the code and output.

(*set up scaling of population sizes*)
iVals = {1/2, 2, 8, 16};

(*create simulations *)
Table[
  Ne = 8*i;
  initialCount  = Ne; (*allele frequency = initialCount/(2 Ne) *)
  
  tMax = 5;
  nPops = 100;
  
  driftMatrix[i] = 
   Table[
     (*each row is the distribuiton of p at given generation *)
     
     NestList[
        RandomInteger[BinomialDistribution[2 Ne, #/(2 Ne)]] &, 
        initialCount, tMax + 1]/(2 Ne) // N, {i, nPops}] // 
    Transpose,
   {i, iVals}];

(*set up the bins*)
binWidth = {0.05};
binBoundaries = Range[ 0 - binWidth/2, 1 + binWidth/2, binWidth];
bin = binWidth;

(*choose which time steps to plot *)
tSteps = 10;
tVals = Range[0, 5];
rowVals = tVals + 1;

(*make the plots *)
plots = Table[
    Table[
     xTicks = If[j == Max[rowVals], Range[0, 1, 0.25], None];
     lTicks = 
      If[i == Min[iVals], {{10, Style["0.5", Smaller]}} , None];
     rTicks = 
      If[i == Max[iVals], {{10, Style["0.5", Smaller]}} , None];
     rFrame = If[i == Max[iVals], True , False];
     Histogram[
      driftMatrix[i][[j]], binBoundaries, "PDF", 
      PlotRange -> {{0, 1}, {0, 12}},
      Frame -> {{True, rFrame}, {True, False}},
      FrameTicks -> { 
        {lTicks, rTicks},
        {xTicks, None }
        },
      AspectRatio -> 1/3
      ], {j, rowVals}], {i, iVals}] // Transpose;

(*combine plots into a Grid*)

xlabels = 
  Text[Style[#, Medium]] & /@ (Join[8*iVals, {"Generation"}]);
ylabels = Text[Style[#, Medium]] & /@ (tVals);
Show[
 Labeled[
  Grid[
   Join[
    {xlabels},
    Transpose[
     Join[Transpose[plots], {ylabels}]
     ]
    ], Spacings -> {1, 0}
   ],
  {"Population Size", "Bin Frequency", 
   "Frequency of bw75 Allele"}, {Top, Left, Bottom},
  RotateLabel -> True],
 ImageSize -> 12*72
 ]

Grid of Histograms

It's subtle, but if you look you'll see that the frame lines of the left and right most plots don't match up with the interior ones.

$\endgroup$
4
  • $\begingroup$ try if adding the option ImageSize -> 1 -> 160 to Histogram gives what you need. $\endgroup$ – kglr Feb 11 at 5:30
  • 2
    $\begingroup$ try also Lukas Lang's PlotGrid $\endgroup$ – kglr Feb 11 at 5:37
  • $\begingroup$ @kglr Thanks for both suggestions. The first works almost perfectly, but there's actually an extremely slight difference in the length of the x dimension between the left column and the rest of the columns. Could you please take a minute and explain how the use of two replacement rules affects things and how you concluded to use 160 in the second replacement? I haven't had time to look at PlotGrid, but it seems like a very useful function to add to my init.nb file. $\endgroup$ – mikemtnbikes Feb 12 at 14:38
  • 1
    $\begingroup$ mikemtnbikes, please see the answer I posted. $\endgroup$ – kglr Feb 13 at 1:41
2
$\begingroup$

The form ImageSize -> a -> b (still undocumented) makes a user units correspond to b printer's points. (See also: this q/a)

Play with different values of b to have plots fit into the containing object without being clipped.

sizeinprinterpoints = 165;

plots = Table[Histogram[driftMatrix[i][[j]], binBoundaries, "PDF",
     PlotRange -> {{0, 1}, {0, 12}},
     Frame -> {{True, True}, {True, False}},
     FrameStyle -> {{Automatic, i /. {Max[iVals] -> Automatic, _ -> White}}, 
        {Automatic,  Automatic}}, 
     FrameTicks -> {Table[i /. {m[iVals] -> {{10, Style["0.5", Smaller]}}, _ -> None}, 
          {m, {Min, Max}}],
       {j /. {Max[rowVals] -> Range[0, 1, 0.25], _ -> None}, None}},
     AspectRatio -> 1/3,
     ImageSize -> 1 -> sizeinprinterpoints],
   {j, rowVals}, {i, iVals}];

xlabels = Text[Style[#, Medium]] & /@ (Join[8 iVals, {"Generation"}]);
ylabels = Text[Style[#, Medium]] & /@ (tVals);
    
Framed @ Labeled[Grid[Join[{xlabels}, Transpose[Join[Transpose[plots], {ylabels}]]]], 
  {"Population Size", "Bin Frequency", "Frequency of bw75 Allele"},
  {Top, Left, Bottom}, RotateLabel -> True, ImageSize -> 12*72]

enter image description here

If you wish to specify the size in inches you can use sizeinprinterpoints = 72 2.3 to get (almost) the same picture.

With sizeinprinterpoints = 100; we get

enter image description here

$\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.