cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
crabduchess
Level I

Plotting two y variables on same graph with overlay on one

I am trying to create a single graph with two plots.  The first is a bar chart of survival across six sites, overlayed with a treatment replicated at each site.  The second is a line graph of the average survival at each site across treatment.  I can create exactly what I want in graph builder, except that I have to plot the line on a second graph above the bars (attached graph builder image).  And I can create exactly what I want with the chart platform, except that I cannot figure out how to overlay the treatment variable for the bars (chart image).  I feel like there has to be a fairly obvious way to achieve this that I am missing.

chart.PNGgraph builder.PNG

7 REPLIES 7
txnelson
Super User

Re: Plotting two y variables on same graph with overlay on one

Here is how I did what I think you want

graph builder.PNG

1. Drag the x (site_order) and y(daily.survival.rate) columns into the graph.

2. Drag the Treatment column to the overlay

3. Click on the graph and select

     Add==>Line

4. Two lines will appear.  One for each Overlay.  So what you need to do, is to go to the Properties Area on the left of the graph, and find the Properties for the Line.  Go to the Variables area and click on the gray triangle to open up the variables list.  Unclick the Overlay for the Treatment variable.

There will then only be one line.

 

I believe this does what you want?

Jim
crabduchess
Level I

Re: Plotting two y variables on same graph with overlay on one

The image you included is exactly what I'm looking for.  I'm able to follow along with your instructions until the final point.  When I open the variables for the line, I don't see an option for the overlay.

Graph builder 2.PNGI'm working in JMP Pro 13, could that be an issue?

txnelson
Super User

Re: Plotting two y variables on same graph with overlay on one

You are correct, the last step in my list of instructions requires JMP 14.  However, the script below shows how to add the line you want using a Graphics Script

Names Default To Here( 1 );
dt = Current Data Table();

// Create the Graph
gb = dt << Graph Builder(
	Variables( X( :age ), Y( :height ), Overlay( :sex ) ),
	Elements( Bar( X, Y, Legend( 6 ) ) )
);

// Create a pointer to the framebox() in the graph
rgb = Report( gb )[FrameBox( 1 )];

// Calculate the means for each age
Summarize( byAge = by( :Age ), theMeans = Mean( :Height ) );

// Because the X axis is not a continuous scale, the values
// for each position on the X axis start with 0 and is increased
// by 1 for each group
// Create the matrix of X axis values
ageMat = Index( 0, N Items( byAge ) - 1 );

// Add the graphic commands to draw a line on the graph
rgb << add graphics script(
	Pen Color( "green" );
	Pen Size( 3 );
	Line( ageMat, theMeans );
);
Jim
crabduchess
Level I

Re: Plotting two y variables on same graph with overlay on one

I think I've correctly updated your script to reference my data, but this is my first foray into scripting in JMP (and I've had a fair amount of training in R from a scientific perspective, but I'm far from computer science literate).  I can produce the bar graph from the script but I'm not sure how to call the code for line.

 

Names Default To Here( 1 );
dt = Current Data Table(DataFromKimbroSurvival);

// Create the Graph
gb = dt << Graph Builder(
    Variables( X( :site.order ), Y( :daily.survival.rate ), Overlay( :treatment ) ),
    Elements( Bar( X, Y, Legend( 6 ) ) ),
);

rgb = Report( gb )[FrameBox( 1 )];

Summarize( bySite = by( :site.order ), theMeans = Mean( :daily.survival.rate ) );

siteMat = Index( 0, N Items( bySite ) - 1 );

rgb << add graphics script(
    Pen Color( "green" );
    Pen Size( 3 );
    Line( siteMat, theMeans );
);
txnelson
Super User

Re: Plotting two y variables on same graph with overlay on one

If you run the script, the chart, and the "add graphics script" will run.  If you are not getting a line drawn on the chart, I assume that there may be an issue with matching the x values for the line, with what the x values for the chart are.  Please go to the x axis on the chart, and right click on it and open the axis settings.  The assumption in my code, is that the x axis values have a minimum value of around zero, and the max a value about equal to the number groupings of your column :treatment.

If that is not the case, then reply back with the min and max axis values. 

Jim
crabduchess
Level I

Re: Plotting two y variables on same graph with overlay on one

The values run from 0 to 5, so if I'm understanding you correctly, there shouldn't be an issue, correct?axis settings.PNG

txnelson
Super User

Re: Plotting two y variables on same graph with overlay on one

You are correct.  So are there any errors in the log?  You may want the use the show() function to list out the matricies that are created, to validate they are correct.

Can you attach a sample data table, and then I could take a better look at the issue?

Jim