cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Learn how to build custom Python data connectors and further customize JMP’s Data Connector Framework with the Python Data Connector Demo, available now in the JMP Marketplace!
  • See how to create experiments to support product design and ID useful product features. Register for June 12 webinar, 2pm US Eastern Time.

Discussions

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

Contingency table and mosaic plot are not same orientation

// For a contingency table report, swap the x and y
// currently can swap the mosaic plot only

Names Default to Here(1);

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
myr = Contingency( Y( :Age ), X( :sex ) );

// Have to have a Contingency Analysis report open
// Can add test to make sure it is open in the future

myr = Current Report();

// taking apart CrossTabBox was easier using XML than JSL

mys = myr["Contingency Table"][CrossTabBox(1)] << Get XML;

// get current X role or rows of contingency table

start = Contains(mys,"<CrosstabBoxRowsLabel>")+22;
end = Contains(mys,"</CrosstabBoxRowsLabel>");
newY = Substr(mys,start,end-start);

// get current Y role or cols of contingency table
start = Contains(mys,"<CrosstabBoxColsLabel>")+22;
end = Contains(mys,"</CrosstabBoxColsLabel>");
newX = Substr(mys,start,end-start);


// Recreate same contingency table but with X and Y flipped
// also temporarily use Value Order to match the Mosaic Plot

myValueLabels = Reverse(Associative Array(Column(dt,newX)) << Get Keys);
Column(dt,newX) << Set Property("Value Ordering",myValueLabels);

tempCont = Contingency( Y(Column(dt,newY) ), X(Column(dt,newX) ), Contingency Table );
tempContr = tempCont << Report;
mycopy = tempContr["Contingency Table"][CrossTabBox(1)] << Clone Box;
tempCont << Close Window;

Column(dt,newX) << Delete Property("Value Ordering");

// remove the original contingency table and replace with transpose

myr["Contingency Table"][CrossTabBox(1)] << Delete;
myr["Contingency Table"] << Append(mycopy);

// show original side by side
obj = Contingency( Y( :Age ), X( :sex ) );

Report(obj)["Contingency Table"] << Set Title("Contingency Table - Current output");

myr["Contingency Table"] << Set Title("Contingency Table - Wish List for JMP15");

Is anyone else bothered by the orientation of the contingency table and mosaic plot? I do not use it, but one of our JMP users wanted to flip the orientation of the table so that it matches the mosaic plot. Found out that if you swap X and Y, both the table and mosaic plot update. See script below to run an example and compare them side-by-side. It seems you would want the numbers in the table to line up with the cells in the mosaic plot for easier viewing and referencing. I could not find any previous discussions. It appears the contingency table cannot be changed, it is a CrossTabBox, which does not lend itself to changing anything. The script below has in the top part the work around, which is to make a clone of the table made when switching the x and y, and then appending that to the mosaic plot. Also had to do a little with Value Ordering to get it to line up. Will add to the JMP15 wishlist. I did discuss at JMP Discovery Summit with the EA lab and the developers.

 

 

Found some posts on R language, where you can use the dir() argument and specify the horizontal and vertical, while the "count" output in RStudio stays the same for the orientation.

# R Mosaic Plot Example - Changing Direction
getwd()
employee <- read.csv("Products.csv", TRUE, sep = ",",
                     na.strings = TRUE)
count <- table(employee$EnglishCountryRegionName,
               employee$Color)
count
mosaicplot(count, main = "Countries Mosaic Plot",
           sub = "Product Colors by Country",
           xlab = "Colors",
           ylab = "Countries",
           las = 1,
           dir = c("h", "v"),
           color = "skyblue2",
           border = "chocolate")

0 REPLIES 0

Recommended Articles