Choose Language Hide Translation Bar
Level IV

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" );
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
employee <- read.csv("Products.csv", TRUE, sep = ",",
                     na.strings = TRUE)
count <- table(employee$EnglishCountryRegionName,
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")

Article Labels

    There are no labels assigned to this post.