Visualization: Quantile and Box-Whisker Plots

Brett Champion
Wolfram Research
wolfram-u@wolfram.com

Quantile and Probability Plots

Quantile and probability plots allow datasets to be compared against a reference distribution.

QuantilePlot — Q-Q plot of data against a reference distribution

ProbabilityPlot — P-P plot of data against a reference distribution

ProbabilityScalePlot — plot data on “probability paper” scaled to linearize data from specific distributions

Reference distributions can be from data, specific distributions, or estimated from distribution families.

Reference lines of various types can be included in the plot.

Quantile and Probability Plots: QuantilePlot

Quantile plots compare a dataset to the reference by matching values by their quantile. Draw points at {Quantile[data, p], Quantile[reference, p]} with values of p between zero and one and chosen depending on the data.

The axes correspond to actual values of the data. The reference can be either another dataset, a specific distribution, or the best-fitting instance of a type of distribution. Unless specified, the dataset will be compared to the best-fitting normal distribution.

Reference lines can be either a best fit or simply go through the 25th and 75th quantiles.

Compare with the best normal distribution.

QuantilePlot[salaryA,PlotRange->All]

Quantile and Probability Plots: Compare Datasets

Compare with another dataset.

QuantilePlot[salaryA,salaryB,PlotRange->All]

Quantile and Probability Plots: Estimate Distributions

Estimate the best log-gamma distribution for the data.

QuantilePlot[salaryA,LogGammaDistribution[α,β,μ]]//Quiet

loggamma=EstimatedDistribution[salaryA,LogGammaDistribution[α,β,μ]]//Quiet

Quantile and Probability Plots: Interpretation

By comparing normally distributed data to a standard normal distribution, we can visually pick out the mean and standard deviation.

Compare with a standardized normal distribution.

data=RandomVariate[NormalDistribution[5,3],100];

QuantilePlot[data,NormalDistribution[0,1],GridLines->{{#,Red}&/@{-1,0,1},Range[-5,20]}]

Check our guess.

{Mean[data],StandardDeviation[data]}

Quantile and Probability Plots: ProbabilityPlot

Probability plots compare a dataset to the reference by matching values by their CDF. Draw points at {EmpiricalDistribution[data, x], CDF[reference, x]} with values of x between the minimum and maximum of the data.

The axes run from zero to one in each direction.

Compare with a normal distribution.

ProbabilityPlot[salaryA,PlotStyle->RGBColor[0,0.5,0]]

Quantile and Probability Plots: Compare Datasets

Compare with another dataset.

ProbabilityPlot[salaryA,salaryB,PlotStyle->RGBColor[0,0.5,0]]

Quantile and Probability Plots: Estimate Distributions

Estimate the best exponential distribution for the data (same as for QuantilePlot).

ProbabilityPlot[salaryA,loggamma,PlotStyle->RGBColor[0,0.5,0]]//Quiet

Quantile and Probability Plots: ProbabilityScalePlot

ProbabilityScalePlot uses axes scaled to linearize data from specific types of distributions. The supported distributions are normal, Weibull, exponential, Frechet, Gumbel, log-normal, and Rayleigh.

The salaries are clearly not distributed according to a normal distribution.

ProbabilityScalePlot[salaryA,"Normal",PlotRange->All,PlotStyle->RGBColor[0.8,0,0]]

ProbabilityScalePlot[RandomVariate[NormalDistribution[2.045,8.25],{100}],"Normal",PlotRange->All,PlotStyle->RGBColor[0.8,0,0]]

ProbabilityScalePlot[{RandomVariate[NormalDistribution[2.045,8.25],{100}],RandomVariate[NormalDistribution[3.545,6.25],{100}]},"Normal",PlotRange->All,PlotStyle->RGBColor[0.8,0,0]]

Quantile and Probability Plots: ProbabilityScalePlot

Exponential is a better fit for the main body of values.

ProbabilityScalePlot[salaryA,"Exponential",PlotStyle->RGBColor[0.8,0,0]]

If the data crosses y=36.8 at x, then λ=(1/x).

NumberForm[1./\[Placeholder],1]

NumberForm[FindDistributionParameters[salaryA,ExponentialDistribution[λ]],1]

Box-Whisker and Distribution Charts

Box-whisker and distribution charts allow many datasets to be compared against each other.

BoxWhiskerChart — box-whisker chart of one or more datasets

DistributionChart — violin chart of one or more datasets

Box-whisker charts can easily show medians, means, quartiles, outliers, and confidence intervals to compare discrete properties of several datasets.

Distribution charts use shape or shading to compare the overall distribution of several datasets.

Each element in a box-whisker or distribution chart automatically has a tooltip that contains key values of the underlying data.

Box-Whisker and Distribution Charts: BoxWhiskerChart

Use several built-in sets of elements for box-whisker charts.

Row[BoxWhiskerChart[deptSalaries,#,PlotLabel->#,ImageSize->300]&/@{"Median","Basic","Notched","Outliers","Mean","Diamond"}]

Box-Whisker and Distribution Charts: Rendering Functions

Use alternative rendering functions for the box.

BoxWhiskerChart[deptSalaries,ChartElementFunction->"GlassBoxWhisker",ChartStyle->56]

Box-Whisker and Distribution Charts: Custom Elements

({{{"\"Fences\"","width","style"}, "width and style of fences"}, {{"\"MeanDiamond\"","width","style"}, "width and style of mean confidence interval"}, {{"\"MeanMarker\"","width","style"}, "width and style of mean marker line"}, {{"\"MedianNotch\"","width","style"}, "width and style of median confidence interval"}, {{"\"MedianMarker\"","width","style"}, "width and style of median marker line"}, {{"\"Outliers\"","marker","style"}, "marker symbol and style for outliers"}, {{"\"FarOutliers\"","marker","style"}, "marker symbol and style for far outliers"}, {{"\"Whiskers\"","style"}, "style for whiskers"}})

Customize the appearance of box-whisker charts.

BoxWhiskerChart[deptSalaries,{"Notched",{"Whiskers",Directive[Thick,Dashing[{0.02}]]},{"Fences",0.75,Thick},{"MedianMarker",0.5,Directive[Thick,Red]},{"Outliers",Style["*",16,Bold],Red}},ChartLabels->depts,ImageSize->Large]

Box-Whisker and Distribution Charts: DistributionChart

Compare datasets by relative density.

Row[(DistributionChart[deptSalaries,ChartElementFunction->#1,PlotLabel->#1,ChartStyle->47,ImageSize->244]&)/@{"SmoothDensity","Density","HistogramDensity","LineDensity","PointDensity"},"   "]

Row[DistributionChart[deptSalaries,ChartElementFunction->#,PlotLabel->#,ChartStyle->40,ImageSize->244]&/@{"Quantile","DensityQuantile","FadingQuantile","GlassQuantile"},"  "]

DistributionChart[deptSalaries,ChartElementFunction->ChartElementDataFunction["GlassQuantile","GradientOrigin"->Automatic,"Quantile"->9,"QuantileShading"->True,"QuantileStyle"->GrayLevel[0.9`]],ChartStyle->40,ImageSize->Large]

Resources

In addition to each function’s reference page (e.g., BoxWhiskerChart), the following guide pages give an overview of the functionality in these areas:

Statistical Visualization

Probability & Statistics

New in 8.0: Visualization & Graphics

The Statistical Visualization web page contains dozens of examples.

Initialization

ceramics=ExampleData[{"Statistics","CeramicStrength"}];

oldfaithful=ExampleData[{"Statistics","OldFaithful"}];

{salaryA,salaryB,salaryC}=Cases[ExampleData[{"Statistics","UniversitySalaries"}],{_,_,s_/;0<s<250000,#}:>s+RandomReal[]]&/@{"A","B","C"};

glass=ExampleData[{"Statistics","AirplaneGlass"}];

depts=Sort@{"Mathematics","English","Physics","Chemistry","Psychology","History"};
deptSalaries=Cases[ExampleData[{"Statistics","UniversitySalaries"}],{#,_,s_/;0<s<250000,"A"}:>s+RandomReal[]]&/@depts;

PicturePrint[expr_,opts:OptionsPattern[Cell]]:=CellPrint[TextCell[expr,"Picture",opts,ShowStringCharacters->False]]

DisplayCell[expr_]:=CellPrint[TextCell[expr,"DisplayFormula",ShowStringCharacters->False]]

DisplayTable[list_List,opts:OptionsPattern[Grid]]:=TraditionalForm@Grid[list,opts,Dividers->All,Spacings->{1,2},Alignment->{Center,Center},BaseStyle->{FontFamily->"Verdana"},Background->{None,{{Hue[.6,.15,.9],GrayLevel[.9]}}},FrameStyle->Directive[Thick,White]]

(*
data={
{Style[{"\"Fences\"",Style["width","TI"],Style["style","TI"]},"MR"],"width and style of fences"},
{Style[{"\"MeanDiamond\"",Style["width","TI"],Style["style","TI"]},"MR"],"width and style of mean confidence interval"},
{Style[{"\"MeanMarker\"",Style["width","TI"],Style["style","TI"]},"MR"],"width and style of mean marker line"},
{Style[{"\"MedianNotch\"",Style["width","TI"],Style["style","TI"]},"MR"],"width and style of median confidence interval"},
{Style[{"\"MedianMarker\"",Style["width","TI"],Style["style","TI"]},"MR"],"width and style of median marker line"},
{Style[{"\"Outliers\"",Style["marker","TI"],Style["style","TI"]},"MR"],"marker symbol and style for outliers"},
{Style[{"\"FarOutliers\"",Style["marker","TI"],Style["style","TI"]},"MR"],"marker symbol and style for far outliers"},
{Style[{"\"Whiskers\"",Style["style","TI"]},"MR"],"style for whiskers"}
};
DisplayCell@
DisplayTable[data,Spacings->{3,1},
BaseStyle->{FontFamily->"Times"},Alignment->Left]
*)

SetOptions[Histogram,{ImageSize->Large,BaseStyle->{FontSize->12}}];

SetOptions[{Histogram3D,DensityHistogram},{ImageSize->Medium,BaseStyle->{FontSize->12}}];

$Line=0;