# Visualizing categorical data with 7 variables

I'm working with a grouped data set on the presence of a particular disease (Byssinosis):

>head(data)

Employment Smoking Sex Race Workspace Byssinosis Non.Byssinosis
1        <10     Yes   M    W         1          3             37
2        <10     Yes   M    O         1         25            139
3        <10     Yes   F    W         1          0              5
4        <10     Yes   F    O         1          2             22
5        <10      No   M    W         1          0             16
6        <10      No   M    O         1          6             75

>str(data)
'data.frame':    72 obs. of  7 variables:
$$Employment : Factor w/ 3 levels "<10",">=20","10-19": 1 1 1 1 1 1 1 1 3 3 ...$$ Smoking       : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 1 1 1 2 2 ...
$$Sex : Factor w/ 2 levels "F","M": 2 2 1 1 2 2 1 1 2 2 ...$$ Race          : Factor w/ 2 levels "O","W": 2 1 2 1 2 1 2 1 2 1 ...
$$Workspace : int 1 1 1 1 1 1 1 1 1 1 ...$$ Byssinosis    : int  3 25 0 2 0 6 0 1 8 8 ...
$Non.Byssinosis: int 37 139 5 22 16 75 4 24 21 30 ...  And here is a description of my data set: In 1973, a large cotton textile company in North Carolina participated in a study to investigate the prevalence of byssinosis, a form of pneumoconiosis to which workers exposed to cotton dust are subject. Data was collected on 5,419 workers. Type of work place [1 (most dusty), 2 (less dusty), 3 (least dusty)] Employment, years [< 10, 10–19, 20+] Smoking [Smoker, or not in last 5 years] Sex [Male, Female] Race [White, Other] Byssinosis [number of cases having the disease] Non.Byssinosis [number of cases not having the disease] I plan on running a logistic regression on this data set to see the relationship between my response variable and my predictor variables, but I want to visualize my data before regressing. What would be the best way to do that considering I have multiple variables with some having multiple factors? • For clarification: what are your individual observations/cases? The fact that you have variables that indicate smokers or gender suggests that an observation is a person but the fact that your Byssinosis variable measures the number of people with the disease suggests you're looking at work places as unit of observation. Commented Dec 11, 2019 at 8:32 • @MaartenPunt This data was collected in 1973 from a large cotton textile company. There were a total of 5,419 workers so if you add up the columns of Byssinosis and Non.Byssinosis you get 5,419. – John Commented Dec 11, 2019 at 9:04 • @MaartenPunt From my understanding, each row in Byssinosis and Non.Byssinosis is the number of workers with the combination of the variables in that row of the data frame. So for example, in row 1 there were a total of 3 workers who had Byssinosis. Those 3 workers have been employed at the company for <10 years, are smokers, male, White, and worked in the most dusty work place. There were also 37 workers who did not have Byssinosis, and those 37 workers have been employed at the company for <10 years, are smokers, male, White, and worked in the most dusty work place. – John Commented Dec 11, 2019 at 9:05 ## 2 Answers It's going to be difficult to consider multiple variables at the same time as it gets messy pretty quick. If you consider one independent variable at a time I would recommend a stacked bar graph that stacks the percentage of people within a category that do and do not get the disease with each bar representing a category. You would then for example see the percentage of smokers that do and do not get the disease (of the total of all smokers) and next to that the same for the non-smokers. By looking at percentages within a category you take away the fact that you may have more people in one category than in another and only look at how often the disease occurs relatively within the different categories. This visualization easily adapts to multiple categories, you'd just have more bars. If you really want to consider multiple independents at the same time you may be able to pull that off by "merging" categories looking e.g. at the relative prevalence of the disease in the category smokers that work in a dusty work place vs smokers that do not work in a dusty work place vs non-smokers that do work in a dusty work placevs non-smokers that do not work in a dusty work place, but as I said that gets messy pretty quickly. EDIT: As a visual explanation here is a stacked bar graph of the survival rates by gender of the titanic (data from here ) This clearly shows that they had a policy of women (and children?) first. The following graph combines gender and passenger/crew type, showing how it becomes messier when you want to show all categories (note that some categories have 0 count e.g. female deck crew, which is why the colums are missing), but the gender effect is still present as is the fact that survival rates are higher for the "upper class" passengers and (perhaps unsurprising) the deck crew. @MaartenPunt has provided a good start. Let me expand on those ideas. The notion of a stacked bar chart stratified by another variable is very close to a mosaic plot (see also here). The primary difference is that the latter allocates horizontal (vertical) space to the stratification variable according to the proportions of the data for each level of that categorical variable. In addition, mosaic plots can often be colored according to the residuals from the corresponding chi-squared test. I discuss mosaic plots here, and their coloring here. From there, the standard way to explore a multidimensional dataset is to use a plot matrix. The most common type that you will be familiar with is a scatterplot matrix, which is just a matrix of the scatterplots of each variable by each other variable. However, most any kind of plot can be created $$p \times p$$ times and displayed in a matrix. For example, we can display a matrix of mosaic plots. Since all of your variables are categorical, this is very convenient. One word of caution is that plot matrices quickly become very busy as the number of variables increases—seven may be too much. A simple solution is to split your variables into subsets and make several plot matrices (cf., my answer to How to extract information from a scatterplot matrix when you have large N, discrete data, & many variables?). At any rate, this can be done easily in R with the vcd package; consider the example below: # I start with the subset of your data that you displayed, # drop the constant variables, & turn it into a dataframe # with a more typical layout. Then I make the plot. d = read.table(text="Employment Smoking Sex Race Workspace Byssinosis Non.Byssinosis <10 Yes M W 1 3 37 <10 Yes M O 1 25 139 <10 Yes F W 1 0 5 <10 Yes F O 1 2 22 <10 No M W 1 0 16 <10 No M O 1 6 75", header=T) d = d[,-which(names(d)%in%c("Employment", "Workspace"))] dl = reshape(d, direction="long", varying=c("Byssinosis", "Non.Byssinosis"), v.names="count", timevar="outcome", times=c("sick", "not")) dl = dl[rep(rownames(dl), dl$count),]
dl = dl[,-which(names(dl)%in%c("count", "id"))]
row.names(dl) = NULL

library(vcd)
windows()