Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

## Plot Data Grouped by Category

### Note

The `nominal` and `ordinal` array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the Categorical Arrays (MATLAB) data type instead.

### Plot Data Grouped by Category

This example shows how to plot data grouped by the levels of a categorical variable.

Load sample data.

`load carsmall`

The variable `Acceleration` contains acceleration measurements on 100 sample cars. The variable `Origin` is a character array containing the country of origin for each car.

Create a nominal array.

Convert `Origin` to a nominal array.

```Origin = nominal(Origin); getlevels(Origin)```
```ans = 1x6 nominal array France Germany Italy Japan Sweden USA ```

There are six unique countries of origin in the sample. By default, `nominal` orders the countries in ascending alphabetical order.

Plot data grouped by category.

Draw box plots for `Acceleration`, grouped by `Origin`.

```figure boxplot(Acceleration,Origin) title('Acceleration, Grouped by Country of Origin')``` The box plots appear in the same order as the categorical levels (use `reorderlevels` to change the order of the categories).

Few observations have Italy as the country of origin.

Tabulate category counts.

Tabulate the number of sample cars from each country.

`tabulate(Origin)`
``` Value Count Percent France 4 4.00% Germany 9 9.00% Italy 1 1.00% Japan 15 15.00% Sweden 2 2.00% USA 69 69.00% ```

Only one car is made in Italy.

Drop a category.

Delete the Italian car from the sample.

```Acceleration2 = Acceleration(Origin~='Italy'); Origin2 = Origin(Origin~='Italy'); getlevels(Origin2)```
```ans = 1x6 nominal array France Germany Italy Japan Sweden USA ```

Even though the car from Italy is no longer in the sample, the nominal variable, `Origin2`, still has the category `Italy`. Note that this is intentional—the levels of a categorical array do not necessarily coincide with the values.

Drop a category level.

Use `droplevels` to remove the `Italy` category.

```Origin2 = droplevels(Origin2,'Italy'); tabulate(Origin2)```
``` Value Count Percent France 4 4.04% Germany 9 9.09% Japan 15 15.15% Sweden 2 2.02% USA 69 69.70% ```

The `Italy` category is no longer in the nominal array, `Origin2`.

Plot data grouped by category.

Draw box plots of `Acceleration2`, grouped by `Origin2`.

```figure boxplot(Acceleration2,Origin2) title('Acceleration, Grouped by Country of Origin')``` The plot no longer includes the car from Italy.

Download ebook