Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Level I

Graph Builder Mean Line

I have a "points" plot in graph builder. I would like to add a mean line to the plot automatically, like one can do in Fit Y by X by selecting Fit Mean. I want it to happen automatically because I have a local data filter that I want to use to filter my data and each time I do so, I want the mean line to update with the mean of the filtered data. Basically, I would like the Mean Line to behave the same way the Line Of Fit currently does in graph builder in this situation. (You might say I would like to add a 0th order line of fit.)


I am very new to jmp, so please have patience with my ignorance. I have been trying to adding a script (Customize>Add script). If I could find a way to call the mean I could add Y Function(mean(), x), but I can't figure out the right way to do it. I guess the mean must be accessible because I can add a caption box that shows the mean (and this mean updates depending on how the data is filtered), but I can't figure out how to access this value. Thanks for any help you might have!


Re: Graph Builder Mean Line

Here is one way to do it  - I use a graphics script for drawing, but the logic for computing the mean and responding to row-state changes is done outside of the drawing.


Working: with data table exclude changes and adding or removing of data filters.


Needs work: (1) Data table and Y column are hard-coded.  (2) It only works on the most recent report launched from the script.  You can't run it multiple times, or use Redo Analysis to create a second working copy of the report.



dt=Open("$SAMPLE_DATA/Big Class.jmp");
gb=dt<<Graph Builder(
	Size( 534, 454 ),
	Show Control Panel( 0 ),
	Variables( X( :height ), Y( :weight ) ),
	Elements( Points( X, Y, Legend( 2 ) ) )

// add or update a mean line that honors exclude flags
	rpt[FrameBox(1)] << Remove Graphics Script(1);

	rs = rpt[FrameBox(1)] << Get Row States;
	val = dt:weight << Get Values;
	inc = Loc(1-(rs&2));
	m = mean(val[inc]);

	Eval(EvalExpr( rpt[FrameBox(1)] << Add Graphics Script(
		Pen Color("red");
		Y Function(Expr(m), x);
		Text Color("red");
		Text({X Origin(), Expr(m)}, "Mean=" || Char(Expr(m)));

// listen for row-state changes to dt or data filter
rsupdate = Function( {a},
	If( Is Matrix( a ),
		// row states have changed
		// else (a==-1) indicates that filter may have been added or removed
		rsh = rpt[FrameBox(1)] << Make Row State Handler( dt, rsupdate );
rsh = rpt[FrameBox(1)] << Make Row State Handler( dt, rsupdate );


Article Labels

    There are no labels assigned to this post.