Documentation

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 = 

     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 = 

     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)
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.

See Also

| | |

Related Examples

More About

Was this topic helpful?