Main Content

interactionplot

Interaction plot for grouped data

Description

interactionplot(Y,group) creates a two-factor interaction plot for the group means of Y with groups defined by the entries in group. The interaction plot is a matrix plot, with the number of rows and columns both equal to the number of grouping variables. Each subplot shows the group means of Y as a function of one grouping variable. The grouping variable names appear on the diagonal of the plot matrix. The plot at off-diagonal position (i,j) is the interaction of the two variables whose names are given at row diagonal (i,i) and column diagonal (j,j), respectively.

interactionplot(Y,group,Name=Value) specifies additional options using one or more name-value arguments. For example, you can specify the grouping variable names, and whether to display the full matrix of interaction plots.

example

interactionplot(parent,___) plots into the figure specified by parent instead of the current figure (gcf) using any of the input argument combinations in the previous syntaxes. (since R2024a)

[h,ax,bigax] = interactionplot(___) returns graphics handles to the figure window in h, the individual plots in ax, and the entire interaction plot in bigax.

Examples

collapse all

Randomly generate data for a response variable y.

y = randn(1000,1);

Randomly generate data for four three-level factors.

group = ceil(3*rand(1000,4));

Create an interaction plot matrix for the factors, and label the factors 'A', 'B', 'C', and 'D'.

interactionplot(y,group,VarNames={'A','B','C','D'})

Figure contains 16 axes objects. Axes object 1 contains an object of type text. Axes object 2 contains 3 objects of type line. Axes object 3 contains 3 objects of type line. Axes object 4 contains 3 objects of type line. These objects represent D = 1, D = 2, D = 3. Axes object 5 contains 3 objects of type line. These objects represent C = 1, C = 2, C = 3. Axes object 6 contains an object of type text. Axes object 7 contains 3 objects of type line. Axes object 8 contains 3 objects of type line. Axes object 9 contains 3 objects of type line. Axes object 10 contains 3 objects of type line. These objects represent B = 1, B = 2, B = 3. Axes object 11 contains an object of type text. Axes object 12 contains 3 objects of type line. Axes object 13 contains 3 objects of type line. Axes object 14 contains 3 objects of type line. Axes object 15 contains 3 objects of type line. These objects represent A = 1, A = 2, A = 3. Axes object 16 contains an object of type text.

The figure contains a matrix of subplots, where each subplot shows the mean response for different values of the grouping variables A, B, C, and D.

Display the interaction plots and omit the duplicate panels.

interactionplot(y,group,VarNames={'A','B','C','D'},Full=false)

Figure contains 6 axes objects. Axes object 1 with xlabel A contains 3 objects of type line. These objects represent B = 1, B = 2, B = 3. Axes object 2 with xlabel A contains 3 objects of type line. These objects represent C = 1, C = 2, C = 3. Axes object 3 with xlabel A contains 3 objects of type line. These objects represent D = 1, D = 2, D = 3. Axes object 4 with xlabel B contains 3 objects of type line. These objects represent C = 1, C = 2, C = 3. Axes object 5 with xlabel B contains 3 objects of type line. These objects represent D = 1, D = 2, D = 3. Axes object 6 with xlabel C contains 3 objects of type line. These objects represent D = 1, D = 2, D = 3.

The plot displays only the unique panels of the full interaction plot matrix.

Copyright 2015 The MathWorks, Inc.

Input Arguments

collapse all

Input data, specified as a numeric vector or numeric matrix. If Y is a vector, the rows give the means of each entry in group. If Y is a matrix, the rows represent different observations, and the columns represent replications of each observation.

Data Types: single | double

Grouping variables, specified as a cell array or numeric matrix. If group is a cell array, then each cell of group must contain a grouping variable that is a categorical variable, numeric vector, character matrix, string array, or single-column cell array of character vectors. If group is a numeric matrix, then its columns represent different grouping variables, and its rows correspond to the rows in Y. Each grouping variable must have the same number of elements as Y. The number of grouping variables must be greater than 1.

Example: {Smoker,Gender}

Data Types: categorical | single | double | char | string | cell

Parent container, specified as a Figure or Panel object.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: interactionplot(Y,group,VarNames=["Weight","MPG"}) creates an interaction plot for the group means of matrix Y with groups defined in group, and grouping variables named "Weight" and "MPG".

Grouping variable names, specified as a character matrix, string array, or cell array of character vectors. interactionplot displays the grouping variable names along the diagonal in the interaction plot. If Varnames is a character matrix, it must have size(group,2) rows. Otherwise, VarNames must have size(group,2) elements. If you do not specify VarNames, the default names are "X1", "X2", ... .

Example: Varnames=["A","B","C"]

Data Types: char | string | cell

Flag to plot the full interaction plot matrix, specified as a numeric or logical 1 (true) or 0 (false). When Full is true (the default), the function displays interaction plots for AB and BA, where A and B are any two factors in group. When Full is false, the function displays only the AB interaction plots.

Example: Full=true

Data Types: single | double | logical

Output Arguments

collapse all

Handle to the figure window, returned as a figure handle.

Axes handles to the individual plots, returned as an array of Axes objects.

Axes handle to the entire interaction plot matrix, returned as an Axes object. bigax points to the current axes, so a subsequent title, xlabel, or ylabel command produces labels that are centered with respect to the entire interaction plot matrix.

Version History

Introduced in R2006b

expand all