cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
ravij
Level II

Add variable in Graph builder

I have data table with multiple columns and rows

I want to loop through each column and add it to graph builder on x -axis 

This is my simple script 

 

dt = Open( filepath );
all_cols = dt << Get Column Names( "String" );

cols_to_plot={};
For Each ({col}, all_cols,
           If(col == "A" || col == "B",,
                        Insert into (cols_to_plot, col);
               )
);
graph = dt << Graph Builder();

For Each ({col},cols_to_plot,
           graph << Add Variable ("X", Column(dt, col)); // Also tried  graph << Add Variable (X, Column(dt, col));
 );
 
 
This script is always giving me below error 
 

argument value is invalid in access or evaluation of 'Add Variable' , Bad Argument( "X" ), Add Variable( "X", Column( dt, col ) ) /*###*/at line 600

 

Just as a check if I run below script it works fine, but I want to plot it for ~1000 columns

graph = dt << Graph Builder(
Variables(X(cols_to_plot[1]),X(cols_to_plot[2]),X(cols_to_plot[3]));
);

what am I missing here, appreciate your help 

 

Edit (jthi): added jsl formatting

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Add variable in Graph builder

As you have so many columns, I would most likely build the Graph Builder expression instead of adding columns one by one. Create the graph manually, copy the script to see how it has been built and then replicate it using JSL. Below is one example

JMP created script

Graph Builder(
	Size(528, 458),
	Show Control Panel(0),
	Variables(
		X(:"0.642539"n, Combine("Parallel Merged")),
		X(:"0.645736349"n, Position(1), Combine("Parallel Merged")),
		X(:"0.648933698"n, Position(1), Combine("Parallel Merged")),
		X(:"0.652131047"n, Position(1), Combine("Parallel Merged")),
		X(:"0.655328396"n, Position(1), Combine("Parallel Merged")),
		X(:"0.658525745"n, Position(1), Combine("Parallel Merged")),
		X(:"0.661723094"n, Position(1), Combine("Parallel Merged")),
		X(:"0.664920443"n, Position(1), Combine("Parallel Merged")),
		X(:"0.668117792"n, Position(1), Combine("Parallel Merged")),
		X(:"0.671315141"n, Position(1), Combine("Parallel Merged")),
		X(:"0.67451249"n, Position(1), Combine("Parallel Merged")),
		X(:"0.677709839"n, Position(1), Combine("Parallel Merged")),
		X(:"0.680907188"n, Position(1), Combine("Parallel Merged")),
		X(:"0.684104537"n, Position(1), Combine("Parallel Merged")),
		X(:"0.687301886"n, Position(1), Combine("Parallel Merged")),
		X(:"0.690499235"n, Position(1), Combine("Parallel Merged"))
	),
	Elements(Points(X(1), X(2), X(3), X(4), X(5), X(6), X(7), X(8), X(9), X(10), X(11), X(12), X(13), X(14), X(15), X(16), Legend(4)))
);

To create this with JSL you could do something like this

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Functional Data/NMR DoE.jmp");

nrmcols = dt << get column group("NMR Spectra");

colnames = Transform Each({col}, nrmcols,
	AsColumn(dt, col) << get name;
);

colnames = colnames[1::20];

var_expr = Expr(Variables());
points_expr = Expr(Points());

For Each({colname, idx}, colnames,
	x_expr = EvalExpr(X(Expr(NameExpr(AsColumn(dt, colname))), Position(1), Combine("Parallel Merged")));
	point_expr = EvalExpr(X(Expr(idx)));
	
	Insert Into(var_expr, Name Expr(x_expr));
	Insert Into(points_expr, Name Expr(point_expr));
);

gb = Eval(Substitute(
	Expr(dt << Graph Builder(
		Size(528, 458),
		Show Control Panel(0),
		_var_,
		Elements(_points_)
	)),
	Expr(_var_), Name Expr(var_expr),
	Expr(_points_), Name Expr(points_expr)
));

Write();
-Jarmo

View solution in original post

8 REPLIES 8
jthi
Super User

Re: Add variable in Graph builder

Not exactly sure what type of plot you are going for, but one example would be something like this

Names Default To Here(1); 

dt = open("$SAMPLE_DATA/Big Class.jmp");

cols_to_plot = {"age", "sex"};

graph = dt << Graph Builder();


For Each ({col},cols_to_plot,
	Eval(EvalExpr(
		graph << Add Variable({Expr(NameExpr(AsColumn(dt, col))), Role("X")});
	));
 );

Check correct syntax from Scripting Index for << Add Variable and use expression evaluation to get column reference in correct format

jthi_0-1764243332023.png

(Depending on your final plot, you might want to go other route where you build the whole graph builder expression)

-Jarmo
ravij
Level II

Re: Add variable in Graph builder

I tried this method and also tried

For Each ({col}, cols_to_plot,

col_ref = Column(dt, col);

graph << Add variable (col_ref, Role("X"));

);

In both cases my jsl hangs, I only have 1000 columns and 500 rows in my data table

is it the efficient way of doing it, or is there any better way to plot all columns of the table on X axis at once, if I try the same on GUI it is much faster

jthi
Super User

Re: Add variable in Graph builder

What type of plot are you trying to create?

-Jarmo
ravij
Level II

Re: Add variable in Graph builder

I am trying to create a Simple scatter plot, with all columns on X-axis serially 

Something as below

ravij_1-1764251140418.png

 

jthi
Super User

Re: Add variable in Graph builder

As you have so many columns, I would most likely build the Graph Builder expression instead of adding columns one by one. Create the graph manually, copy the script to see how it has been built and then replicate it using JSL. Below is one example

JMP created script

Graph Builder(
	Size(528, 458),
	Show Control Panel(0),
	Variables(
		X(:"0.642539"n, Combine("Parallel Merged")),
		X(:"0.645736349"n, Position(1), Combine("Parallel Merged")),
		X(:"0.648933698"n, Position(1), Combine("Parallel Merged")),
		X(:"0.652131047"n, Position(1), Combine("Parallel Merged")),
		X(:"0.655328396"n, Position(1), Combine("Parallel Merged")),
		X(:"0.658525745"n, Position(1), Combine("Parallel Merged")),
		X(:"0.661723094"n, Position(1), Combine("Parallel Merged")),
		X(:"0.664920443"n, Position(1), Combine("Parallel Merged")),
		X(:"0.668117792"n, Position(1), Combine("Parallel Merged")),
		X(:"0.671315141"n, Position(1), Combine("Parallel Merged")),
		X(:"0.67451249"n, Position(1), Combine("Parallel Merged")),
		X(:"0.677709839"n, Position(1), Combine("Parallel Merged")),
		X(:"0.680907188"n, Position(1), Combine("Parallel Merged")),
		X(:"0.684104537"n, Position(1), Combine("Parallel Merged")),
		X(:"0.687301886"n, Position(1), Combine("Parallel Merged")),
		X(:"0.690499235"n, Position(1), Combine("Parallel Merged"))
	),
	Elements(Points(X(1), X(2), X(3), X(4), X(5), X(6), X(7), X(8), X(9), X(10), X(11), X(12), X(13), X(14), X(15), X(16), Legend(4)))
);

To create this with JSL you could do something like this

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Functional Data/NMR DoE.jmp");

nrmcols = dt << get column group("NMR Spectra");

colnames = Transform Each({col}, nrmcols,
	AsColumn(dt, col) << get name;
);

colnames = colnames[1::20];

var_expr = Expr(Variables());
points_expr = Expr(Points());

For Each({colname, idx}, colnames,
	x_expr = EvalExpr(X(Expr(NameExpr(AsColumn(dt, colname))), Position(1), Combine("Parallel Merged")));
	point_expr = EvalExpr(X(Expr(idx)));
	
	Insert Into(var_expr, Name Expr(x_expr));
	Insert Into(points_expr, Name Expr(point_expr));
);

gb = Eval(Substitute(
	Expr(dt << Graph Builder(
		Size(528, 458),
		Show Control Panel(0),
		_var_,
		Elements(_points_)
	)),
	Expr(_var_), Name Expr(var_expr),
	Expr(_points_), Name Expr(points_expr)
));

Write();
-Jarmo
ravij
Level II

Re: Add variable in Graph builder

This works perfectly, Thanks for the help

ravij
Level II

Re: Add variable in Graph builder

Hello, 

I have a follow up problem with these graphs I plotted, I am not able to save them in any format I tried .jmpreport .jrp etc, as I don't want to save graph in static png, jpg formates, I want to save interactive graph with data embedded in it


nw = New Window("printing window",
gb = Eval(Substitute(
Expr(dt << Graph Builder(
    Show Window(1),
    Size(528, 458),
    Show Control Panel(0),
    _var_,
    Elements(_points_)
)),
Expr(_var_), Name Expr(var_expr),
Expr(_points_), Name Expr(points_expr)
));
); 

reportpath = ::my_dir_path||"/metadata/"||metaTestfile; newWindow<< Save(reportpath);
Also tried 
gb = Eval(Substitute(
		Expr(dt << Graph Builder(
				Show Window(1),
				Size(528, 458),
				Show Control Panel(0),							
				_var_,
				Elements(_points_)
		)),
		Expr(_var_), Name Expr(var_expr),
		Expr(_points_), Name Expr(points_expr)
));

gb << Show Window;
newWindow = Window (gb);					
				
reportpath = ::my_dir_path||"/metadata/"||metaTestfile;
newWindow<< Save(reportpath );
The script doesn't throw any error in any case but also not saving the graph
jthi
Super User

Re: Add variable in Graph builder

Scripting Index usually has the answers for scripting (<< Save Window Report)

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Functional Data/NMR DoE.jmp");

nrmcols = dt << get column group("NMR Spectra");

colnames = Transform Each({col}, nrmcols,
	AsColumn(dt, col) << get name;
);

colnames = colnames[1::20];

var_expr = Expr(Variables());
points_expr = Expr(Points());

For Each({colname, idx}, colnames,
	x_expr = EvalExpr(X(Expr(NameExpr(AsColumn(dt, colname))), Position(1), Combine("Parallel Merged")));
	point_expr = EvalExpr(X(Expr(idx)));
	
	Insert Into(var_expr, Name Expr(x_expr));
	Insert Into(points_expr, Name Expr(point_expr));
);

gb = Eval(Substitute(
	Expr(dt << Graph Builder(
		Size(528, 458),
		Show Control Panel(0),
		_var_,
		Elements(_points_)
	)),
	Expr(_var_), Name Expr(var_expr),
	Expr(_points_), Name Expr(points_expr)
));

gb << Save Window Report("$DOWNLOADS/test.jrp", embed data(1));

// demo purposes
Close(dt, no save);
wait(1);

Open("$DOWNLOADS/test.jrp");

Write();
-Jarmo

Recommended Articles