# Putting a vertical line in each Histogram using GraphicsGrid

I'm using GraphicsGrid to show several histograms.

In each histogram, I would like to show 2 vertical lines on the 2.5 and 97.5 percentiles. If I had an isolated histogram I would use Line, and Show. However, I have no idea how to proceed with a GraphicsGrid...

• Could you just put your isolated-histogram solution in GraphicsGrid? It'd be easier to diagnose with some code... – Chris K Feb 24 '19 at 10:13

## 2 Answers

You can use GridLines combined with the option Method ->{"GridLinesInFront" -> True}:

SeedRandom[1]
{data1, data2} = RandomVariate[NormalDistribution[#, 1], 500] & /@ {2, 4};
GraphicsGrid[{Histogram[#, ImageSize -> 300,
GridLines -> {Thread[{Quantile[#, {.025, .975}],
Directive[Opacity[1], Thick, #] & /@ {Red, Blue}}], None},
Method -> {"GridLinesInFront" -> True}] & /@ {data1, data2}}]


• Do you want to edit this so it's using {0.025, .975} not {0.25, .975}? – Eric William Smith Feb 24 '19 at 13:21
• Than you @Eric. Done. – kglr Feb 24 '19 at 13:42
• Many thanks for the answer. ;) – An old man in the sea. Feb 24 '19 at 16:01
• @Anoldmaninthesea., myt pleasure. Thank you for the accept. – kglr Feb 24 '19 at 17:20

Combining two sets of graphics objects with Show in a graphics grid is not difficult as long as the sets are compatible. That means, at least, all the objects in both lists should be plotted in the same coordinate system and have the same image size.

Here is an example using some graphics I contrived.

Draw random group of $$n$$ circles

circles[n_] :=
Module[{r, cntr},
r := RandomReal[.25];
cntr := RandomReal[1, {2}];
Graphics[
Table[{EdgeForm[Black], Hue[RandomReal[]], Disk[cntr, r]}, n],
PlotRange -> {{0, 1}, {0, 1}},
PlotRangeClipping -> True,
Frame -> True]]


Draw two random vertical lines with the left one red and the right one blue.

lines[] :=
Module[{lf, rt},
lf := With[{x = RandomReal[.48]}, {Red, Line[{{x, 0}, {x, 1}}]}];
rt := With[{x = RandomReal[{.52, 1}]}, {Blue, Line[{{x, 0}, {x, 1}}]}];
Graphics[{lf, rt},
PlotRange -> {{0, 1}, {0, 1}},
PlotRangeClipping -> True,
Frame -> True]]


Now the following simple function will combined any two lists of graphics that are compatible in sense mentioned in the preamble to this answer. The rather elaborate argument patterns on the lefthand side of the SetDelayed expression represent my attempt to enforce the compatibility of the arguments.

makeGrid[g1 : {_Graphics ..}, g2 : {_Graphics ..}, rows_Integer /; rows > 0] /;
Length[g1] == Length[g2] && Mod[Length[g1], rows] == 0 :=
GraphicsGrid @ Apply[Show, Partition[Transpose[{g1, g2}], rows], {2}]


So let's make a 4 x 4 graphics grid from a list of four circles groups and a list of four pairs of vertical lines.

SeedRandom[4];
makeGrid[Table[circles[8], 4], Table[lines[], 4], 2]


• many thanks for the answer ;) – An old man in the sea. Feb 24 '19 at 16:02
• I've been trying to use your solution for another problem. However, I can't understand the MakeGrid function... Could you write it in a simpler way? Thanks ;) – An old man in the sea. Mar 23 '19 at 9:23
• @Anoldmaninthesea. I'm sorry you are having difficulties. I think makeGrid is about as simple as possible, but I might have an idea of simple different from yours. The only part that I think might be considered complicated is the constraints imposed on the arguments to insure that garbage doesn't get passed into the function, You can remove them at some risk, but if you are careful of what you pass to the function, it should still work. If there is something in the body of the function you don't understand, open a chat room and we could discuss it there. – m_goldberg Mar 24 '19 at 0:12