Main Content

boxchart

Box chart (box plot) for multivariate analysis of variance (MANOVA)

Since R2023b

    Description

    boxchart(maov,response) creates a notched box plot of the response variable specified by response for each factor value of the one-way manova object maov. This syntax is supported only if maov is a one-way manova object and the factor is categorical.

    example

    boxchart(maov,response,factors) creates a notched box plot for each value of the categorical factors in factors. The argument factors must contain one or two of the categorical factor names in maov.FactorNames.

    example

    boxchart(___,Name=Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in the previous syntaxes. For example, you can specify the box median line color and the marker style. For a list of properties, see BoxChart Properties.

    example

    boxchart(ax,___) plots into the axes specified by ax instead of the current axes (gca).

    b = boxchart(___) returns a graphics vector b of BoxChart objects for the generated box plots.

    Examples

    collapse all

    Load the fisheriris data set.

    load fisheriris

    The column vector species contains iris flowers of three different species: setosa, versicolor, and virginica. The matrix meas contains four types of measurements for the flower: the length and width of sepals and petals in centimeters.

    Perform a one-way MANOVA with species as the factor and the measurements in meas as the response variables.

    maov = manova(species,meas,FactorNames="species",ResponseNames=["sepal_length" "sepal_width"... 
        "petal_length" "petal_width"])
    maov = 
    1-way manova
    
    sepal_length,sepal_width,petal_length,petal_width ~ 1 + species
    
        Source     DF     TestStatistic    Value       F       DFNumerator    DFDenominator      pValue  
        _______    ___    _____________    ______    ______    ___________    _____________    __________
    
        species      2       pillai        1.1919    53.466          8             290         9.7422e-53
        Error      147                                                                                   
        Total      149                                                                                   
    
    
      Properties, Methods
    
    
    

    maov is a one-way manova object that contains the results of the one-way MANOVA. The small p-value for species indicates that the flower species has a statistically significant effect on at least one of the flower measurements.

    Create a figure of box plots for the sepal length data, grouped by flower species.

    boxchart(maov,"sepal_length")
    ylabel("Sepal Length")

    Figure contains an axes object. The axes object with ylabel Sepal Length contains an object of type boxchart. This object represents species.

    The shaded regions of the box plots do not overlap, indicating that the median sepal length is statistically different for each flower species. This result supports the conclusion that flower species has a statistically significant effect on at least one of the flower measurements.

    Load the carsmall data set.

    load carsmall

    The variables Model_Year and Origin contain data for the year and country in which a car was manufactured, and the variable Cylinders contains data for the number of engine cylinders in the car. The Acceleration and Displacement variables contain data for car acceleration and displacement.

    Convert the character arrays in Origin to string vectors. Then, use the table function to create a table from the data in Model_Year, Origin, and Cylinders. Create a matrix of response data from the variables Acceleration and Displacement by using the string, " " function.

    Origin = string(Origin);
    tbl = table(Model_Year,Origin,Cylinders,VariableNames=["Year" "Origin" "Cylinders"]);
    response = [Acceleration Displacement];

    Perform a three-way MANOVA using the variables in tbl as factors, and the data in Acceleration and Displacement as response variables. For each term in the MANOVA model, the MANOVA tests the null hypothesis that the mean response vectors are not statistically different for different values of the term.

    maov = manova(tbl,response,ResponseNames=["Acceleration" "Displacement"])
    maov = 
    N-way manova
    
    Acceleration,Displacement ~ 1 + Year + Origin + Cylinders
    
         Source      DF    TestStatistic     Value        F       DFNumerator    DFDenominator      pValue  
        _________    __    _____________    ________    ______    ___________    _____________    __________
    
        Year          2       pillai        0.083278    1.9552          4             180             0.1033
        Origin        5       pillai         0.16931    1.6647         10             180           0.092136
        Cylinders     2       pillai         0.84457    32.893          4             180         1.3974e-20
        Error        90                                                                                     
        Total        99                                                                                     
    
    
      Properties, Methods
    
    
    

    maov is a manova object that contains the results of the three-way MANOVA. The table output contains a p-value for each term in the MANOVA model. The p-values for Year and Origin are small. However, not enough evidence exists to reject their corresponding null hypotheses at the 95% confidence level.

    Create box plots of the response data, grouped by the factor values in Year and Origin. Use the nexttile function to create subplots.

    tiledlayout(2,2)
    nexttile
    boxchart(maov,"Acceleration","Year")
    ylabel("Acceleration")
    xlabel("Year")
    nexttile
    boxchart(maov,"Acceleration","Origin")
    ylabel("Acceleration")
    xlabel("Origin")
    nexttile
    boxchart(maov,"Displacement","Year")
    ylabel("Displacement")
    xlabel("Year")
    nexttile
    boxchart(maov,"Displacement","Origin")
    ylabel("Displacement")
    xlabel("Origin")

    Figure contains 4 axes objects. Axes object 1 with xlabel Year, ylabel Acceleration contains an object of type boxchart. This object represents Year. Axes object 2 with xlabel Origin, ylabel Acceleration contains an object of type boxchart. This object represents Origin. Axes object 3 with xlabel Year, ylabel Displacement contains an object of type boxchart. This object represents Year. Axes object 4 with xlabel Origin, ylabel Displacement contains an object of type boxchart. This object represents Origin.

    The majority of the shaded regions in the box plots overlap for each pairing of the factors and response variables, but some do not. The shaded regions of the box plots in the first set of axes indicate that, at the 95% confidence level, the median acceleration for cars built in 1970 is statistically different from the median accelerations for cars built in 1976 and 1982. The top right axes show that the median acceleration is not statistically different for cars built in different countries. The bottom left axes show that the median displacement for cars built in 1970 is statistically different from the median displacement for cars built in 1982. Finally, the last set of axes show that the median displacement for cars built in the USA is statistically different from the median displacements for cars built in any of the other countries. The box plots illustrate why the p-values for Year and Origin are small, but not small enough to reject their corresponding null hypotheses.

    Load the fisheriris data set.

    load fisheriris

    The column vector species contains iris flowers of three different species: setosa, versicolor, and virginica. The matrix meas contains four types of measurements for the flower: the length and width of sepals and petals in centimeters.

    Perform a one-way MANOVA with species as the factor and the measurements in meas as the response variables.

    maov = manova(species,meas,FactorNames=["species"],ResponseNames=["sepal_length" "sepal_width" "petal_length" "petal_width"]);

    maov is a one-way manova object that contains the results of the one-way MANOVA.

    Plot the sepal length data for the different values of species. Plot the box face in gray, the box edges in black, and the median line in red. By default, the box face is semitransparent.

    boxchart(maov,"sepal_length",BoxFaceColor="k",...
        BoxEdgeColor="k",BoxMedianLineColor="r")
    ylabel("Sepal Length")

    Figure contains an axes object. The axes object with ylabel Sepal Length contains an object of type boxchart. This object represents species.

    The shaded regions of the box plots do not overlap, indicating that enough evidence exists to conclude that the median sepal length for each value of species is statistically different from the median sepal lengths for the other two values.

    Input Arguments

    collapse all

    MANOVA results, specified as a manova object. The properties of maov contain the response data and factor values used by boxchart to create box plots.

    Response variable to plot, specified as a string scalar or character array. response must be a name in maov.ResponseNames.

    Example: "sepal_length"

    Data Types: char | string

    Factors used to group the response data, specified as a string vector or a cell array of character vectors. The boxchart function groups the response data by the combinations of values for factors in factors. The factors input argument must be one or two of the categorical factor names in maov.FactorNames.

    Example: "species"

    Data Types: string | cell

    Target axes, specified as an Axes object. If you do not specify the axes, then boxchart uses the current axes (gca).

    Name-Value Arguments

    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: boxchart(maov,"Y",Orientation="horizontal",BoxMedianLineColor="r",WhiskerLineStyle="--") creates horizontal box plots with red median lines and dashed whiskers.

    The BoxChart properties listed here are only a subset. For a complete list, see BoxChart Properties.

    Box edge color, specified as an RGB triplet, hexadecimal color code, color name, or short name. The box edges include the median line. To specify the median line color separately, use the BoxMedianLineColor property.

    For a custom color, specify an RGB triplet or a hexadecimal color code.

    • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

    • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

    Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

    Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
    "red""r"[1 0 0]"#FF0000"

    Sample of the color red

    "green""g"[0 1 0]"#00FF00"

    Sample of the color green

    "blue""b"[0 0 1]"#0000FF"

    Sample of the color blue

    "cyan" "c"[0 1 1]"#00FFFF"

    Sample of the color cyan

    "magenta""m"[1 0 1]"#FF00FF"

    Sample of the color magenta

    "yellow""y"[1 1 0]"#FFFF00"

    Sample of the color yellow

    "black""k"[0 0 0]"#000000"

    Sample of the color black

    "white""w"[1 1 1]"#FFFFFF"

    Sample of the color white

    "none"Not applicableNot applicableNot applicableNo color

    Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

    RGB TripletHexadecimal Color CodeAppearance
    [0 0.4470 0.7410]"#0072BD"

    Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

    [0.8500 0.3250 0.0980]"#D95319"

    Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

    [0.9290 0.6940 0.1250]"#EDB120"

    Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

    [0.4940 0.1840 0.5560]"#7E2F8E"

    Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

    [0.4660 0.6740 0.1880]"#77AC30"

    Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

    [0.3010 0.7450 0.9330]"#4DBEEE"

    Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

    [0.6350 0.0780 0.1840]"#A2142F"

    Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

    Example: b = boxchart(rand(10,1),'BoxEdgeColor','none')

    Example: b.BoxEdgeColor = [0 0 0];

    Example: b.BoxEdgeColor = '#7E2F8E';

    Box color, specified as an RGB triplet, hexadecimal color code, color name, or short name. The box includes the box edges and median line. To specify the color of the box edges and median line separately, you can use the BoxEdgeColor property. To specify the color of the median line only, use the BoxMedianLineColor property.

    For a custom color, specify an RGB triplet or a hexadecimal color code.

    • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

    • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

    Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

    Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
    "red""r"[1 0 0]"#FF0000"

    Sample of the color red

    "green""g"[0 1 0]"#00FF00"

    Sample of the color green

    "blue""b"[0 0 1]"#0000FF"

    Sample of the color blue

    "cyan" "c"[0 1 1]"#00FFFF"

    Sample of the color cyan

    "magenta""m"[1 0 1]"#FF00FF"

    Sample of the color magenta

    "yellow""y"[1 1 0]"#FFFF00"

    Sample of the color yellow

    "black""k"[0 0 0]"#000000"

    Sample of the color black

    "white""w"[1 1 1]"#FFFFFF"

    Sample of the color white

    "none"Not applicableNot applicableNot applicableNo color

    Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

    RGB TripletHexadecimal Color CodeAppearance
    [0 0.4470 0.7410]"#0072BD"

    Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

    [0.8500 0.3250 0.0980]"#D95319"

    Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

    [0.9290 0.6940 0.1250]"#EDB120"

    Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

    [0.4940 0.1840 0.5560]"#7E2F8E"

    Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

    [0.4660 0.6740 0.1880]"#77AC30"

    Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

    [0.3010 0.7450 0.9330]"#4DBEEE"

    Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

    [0.6350 0.0780 0.1840]"#A2142F"

    Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

    Example: BoxFaceColor="red"

    Example: BoxFaceColor=[0 0.5 0.5]

    Example: BoxFaceColor="#EDB120"

    Box median line color, specified as an RGB triplet, hexadecimal color code, color name, or short name.

    For a custom color, specify an RGB triplet or a hexadecimal color code.

    • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

    • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

    Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

    Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
    "red""r"[1 0 0]"#FF0000"

    Sample of the color red

    "green""g"[0 1 0]"#00FF00"

    Sample of the color green

    "blue""b"[0 0 1]"#0000FF"

    Sample of the color blue

    "cyan" "c"[0 1 1]"#00FFFF"

    Sample of the color cyan

    "magenta""m"[1 0 1]"#FF00FF"

    Sample of the color magenta

    "yellow""y"[1 1 0]"#FFFF00"

    Sample of the color yellow

    "black""k"[0 0 0]"#000000"

    Sample of the color black

    "white""w"[1 1 1]"#FFFFFF"

    Sample of the color white

    "none"Not applicableNot applicableNot applicableNo color

    Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

    RGB TripletHexadecimal Color CodeAppearance
    [0 0.4470 0.7410]"#0072BD"

    Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

    [0.8500 0.3250 0.0980]"#D95319"

    Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

    [0.9290 0.6940 0.1250]"#EDB120"

    Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

    [0.4940 0.1840 0.5560]"#7E2F8E"

    Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

    [0.4660 0.6740 0.1880]"#77AC30"

    Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

    [0.3010 0.7450 0.9330]"#4DBEEE"

    Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

    [0.6350 0.0780 0.1840]"#A2142F"

    Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

    Example: b = boxchart(maov,"Y",BoxMedianLineColor="black")

    Example: b.BoxMedianLineColor = [1 0 0];

    Example: b.BoxMedianLineColor = '#7E2F8E';

    Outlier style, specified as one of the options listed in this table.

    MarkerDescriptionResulting Marker
    "o"Circle

    Sample of circle marker

    "+"Plus sign

    Sample of plus sign marker

    "*"Asterisk

    Sample of asterisk marker

    "."Point

    Sample of point marker

    "x"Cross

    Sample of cross marker

    "_"Horizontal line

    Sample of horizontal line marker

    "|"Vertical line

    Sample of vertical line marker

    "square"Square

    Sample of square marker

    "diamond"Diamond

    Sample of diamond marker

    "^"Upward-pointing triangle

    Sample of upward-pointing triangle marker

    "v"Downward-pointing triangle

    Sample of downward-pointing triangle marker

    ">"Right-pointing triangle

    Sample of right-pointing triangle marker

    "<"Left-pointing triangle

    Sample of left-pointing triangle marker

    "pentagram"Pentagram

    Sample of pentagram marker

    "hexagram"Hexagram

    Sample of hexagram marker

    "none"No markersNot applicable

    Example: MarkerStyle="x"

    Example: MarkerStyle="none"

    Outlier marker displacement, specified as "on" or "off", or as numeric or logical 1 (true) or 0 (false). A value of "on" is equivalent to true, and "off" is equivalent to false. So, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

    If you set the JitterOutliers property to "on", then boxchart randomly displaces the outlier markers along the XData direction to help you distinguish between outliers that have similar aov.Y values. For an example, see Visualize and Find Outliers.

    Example: JitterOutliers="on"

    Median comparison display, specified as "on" or "off", or as numeric or logical 1 (true) or 0 (false). A value of "on" is equivalent to true, and "off" is equivalent to false. So, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

    If you set the Notch property to "on", then boxchart creates a tapered, shaded region around each median. Box plots whose notches do not overlap have different medians at the 5% significance level. For more information, see Box Chart (Box Plot).

    Notches can extend beyond the lower and upper quartiles.

    Example: Notch="off"

    Orientation of the box plots, specified as "vertical" or "horizontal". By default, box plots have a vertical orientation, so that the maov.Y statistics are aligned with the y-axis. Regardless of the orientation, boxchart stores the maov.Y values in the YData property of the BoxChart object.

    Example: Orientation="horizontal"

    Output Arguments

    collapse all

    Box plots, returned as a vector of BoxChart objects. If the factors input argument contains only one categorical factor name, b contains a single BoxChart object. If factors contains two categorical factor names, b contains the same number of BoxChart objects as the number of values for the second factor. Use b to set properties of the box plots after creating them. For more information see BoxChart Properties.

    Version History

    Introduced in R2023b