Building on Jim's thought, here's one way (with the local data filter):
NamesDefaultToHere(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
// Graph Builder
gb = dt << Graph Builder(
Show Control Panel( 0 ),
Lock Scales( 1 ),
Variables( X( :weight ), Y( :height ) ),
Elements( Points( X, Y, Legend( 6 ) ), Line Of Fit( X, Y, Legend( 8 ) ) ),
);
// Get a reference to the report
gbReport = gb << Report;
// Add a local data filter
ldf = gb << Local Data Filter( Add Filter( columns( :sex ) ) );
// Row state handler function that adds a horizontal line to Graph Builder at the mean of the current
// selection in the local data filter
addMeanYLine =
Function({x}, {Default Local},
// Get rows corresponding to the current selection
rows = ldf << getFilteredRows;
// Get the required mean value
mv = Mean((Column(dt, "height") << getValues)[rows]);
// Add the graphics script to the Graph Builder display box . . .
gFrames = gbReport << XPath( "//FrameBox" );
// Try to remove a graphics script that might already be there
Try(Send(gFrames[1], RemoveGraphicsScript(1)));
// Expression that will add the required graphics script
ags = Expr(Send(gFrames[1], AddGraphicsScript(PenColor("Red"); HLine(TBD))));
// 'Bake in' current mean value
SubstituteInto(ags, Expr(TBD), mv);
// Add the graphics script
ags;
);
// Assign the row state handler to the local data filter display box
rsh = gbReport[OutlineBox(1)] << MakeRowStateHandler(addMeanYLine);