What is the best way to visualize the relationship two categorical variables

I am currently working on an ambulance dataset and one of my tasks is to find when a patient was misdiagnosed by the call dispatcher.

I have two codes; a dispatch code(what the dispatcher believes is wrong with the patient) and a hospital code(what a doctor decides the actual diagnosis is in hospital)

I'm looking for a way of visualizing the relationship between the two codes i.e. given ambulance code x what is the probability of each ambulance code being the outcome.

This can be computed using SQL pretty easily but I'm looking for a way to visualize/cluster it would be great. Any help would be appreciated.

EDIT: Some good feedback in the comments

firstly the dimensionality: Dispatch code can take 1 of 1722 unique values

hospital codes can take 1 of 1058 unique values

The two codes are entirely different an example would be as follows

Dispatcher 17D03:Unconscious
Hospital R41:Other symptoms and signs involving cognitive functions and awareness


My interest is in visualizing the relationship. So for example given a dispatcher code of unconscious what are the most common hospital codes?

Again this is fairly easy to calculate numerically but a visualization would make it easier to explain to my stake holders.

• How many unique codes are there? Do hospital and dispatch use the same set of codes? If the answers are "low" and "yes", then a heatmap should be good. Jul 30 '18 at 16:20
• How many dimensions does it have? Jul 30 '18 at 18:18
• A matrix as in a spreadsheet (columns=dispatch, rows=hospital) certainly seems appropriate. Jul 30 '18 at 19:46
• Unfortunately both codes have high dimensionality and use a different set of codes. Rather than visualising how frequently a code is classified correctly I want to find the commen relationships for example row in the database is this: Aug 1 '18 at 8:28
• Dispatcher 17D03:Unconscious Hospital R41:Other symptoms and signs involving cognitive functions and awareness Aug 1 '18 at 8:30

You can use a confusion matrix to generate the heat map of your data.

Suppose you have:

labels = ['cardiac arrest', 'choking', 'seizure']
dispatch_code = ['cardiac arrest', 'choking', 'seizure', 'choking', 'seizure', 'seizure', 'cardiac arrest', 'cardiac arrest']
hospital_code = ['cardiac arrest', 'choking', 'cardiac arrest', 'choking', 'seizure', 'seizure', 'seizure', 'cardiac arrest']


Then you can plot with:

from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

cm = confusion_matrix(hospital_code, dispatch_code)
fig = plt.figure()
cax = ax.matshow(cm)
plt.title('Confusion matrix of the classifier')
fig.colorbar(cax)
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()


This example shows that often there is a correct code coming from the dispatcher (yellow stripe in the middle). Cardiac arrest and seizure are often confused (blue) and chocking is never misclassified (purple).

If you are after counts of multi dimensional variables then Mosaicplot can help. In R there is a function from the graphics package called mosaicplot. It's one of base packages that come with R.

HairEyeColor , , Sex = Male

   Eye


Hair Brown Blue Hazel Green Black 32 11 10 3 Brown 53 50 25 15 Red 10 10 7 7 Blond 3 30 5 8

, , Sex = Female

   Eye


Hair Brown Blue Hazel Green Black 36 9 5 2 Brown 66 34 29 14 Red 16 7 7 7 Blond 4 64 5 8

mosiacplot(HairEyeColor)

In the end I ended up using an alluvial diagram on rawgraphs.