Multivariate categorical data visualization

In this recipe, we will learn how we can visualize more than one categorical variable into a single plot and see what it looks like. The command structure will be similar, but this will be applicable for factor variables.

Getting ready

Let's call the modified mtcars dataset once again. After loading the modified mtcars dataset we will select a subset of that data by taking only the factor variables:

cat_dat <- modified_mtcars[c("cyl","vs","am","gear","carb")]

Now, we will use this data to produce a plot.

How to do it…

The command structure is the same as with the continuous data but the input variables are factors in this case:

tableplot(cat_dat,sortCol=carb)
How to do it…

How it works…

Though the function to produce the plot is the same for both categorical and continuous data, for the categorical data, the tableplot function produces each bar with a different color, corresponding to each category in the variable. The noticeable feature is that we have to supply the factor variable for this plot. If we do not input the factor variable, then it automatically treats the input as a continuous variable during the plotting process.

There's more…

With the main plot, we can use different color palettes using the pal argument.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.34.208