11
$\begingroup$

I have a larger data set and want to display it using DistributionChart.

dat = Flatten[{RandomReal[1., 10000], RandomReal[2., 2000]}];
DistributionChart[dat, PlotRange -> {0., 2.}]

Mathematica graphics

In my particular case min and max values are

Min[dat]
(* 0.0000359013 *)

Max[dat]
(* 1.99981 *)

Why does the plot suggest values below the min and above the max?

A BoxWhiskerChart of the same data works fine:

BoxWhiskerChart[dat, {"Median", {"MedianMarker", 1, Black}, {"Whiskers", Black},
{"Fences", 0.5, Black}}, PlotRange -> {0., 2.}]

Mathematica graphics

$\endgroup$
10
$\begingroup$

SmoothHistogram >>> Details and Options:

enter image description here

MaxExtraBandwidths >> Details and Options:

enter image description here

Despite the red syntax highlighting, this option also works in SmoothHistogram:

dat = Flatten[{RandomReal[1., 10000], RandomReal[2., 2000]}]; 

SmoothHistogram[dat, .1, "PDF", 
 ColorFunction -> "Rainbow", Filling -> Axis, ImageSize -> 400,
 MaxExtraBandwidths -> {0, 0}, PlotRange -> {{-.5, 2.5}, {0., 1}}, 
 AxesOrigin -> {-.5, 0}]

enter image description here

For DistributionChart, one would expect/hope that the option Threshold for the built-in ChartElementFunction SmoothDensity would work similarly, but ... it doesn't (see this Q/A). So, one possibility is to build a custom ChartElementFunction that uses SmoothHistogram with the option MaxExtraBandwiths set to {0, 0}. The following is one such example -- which is meant to be suggestive as it needs to be refined/embellished in a number of ways.

ClearAll[cEF];
cEF[bw_: (.1), padding_: {0, 0}] :=
  Module[{color = Charting`ChartStyleInformation["Color"], sh},
    sh = SmoothHistogram[#2, bw, "PDF", MaxExtraBandwidths -> padding,
       Filling -> Axis, FillingStyle -> color, PlotStyle -> color][[1]];
    {EdgeForm[color], GeometricTransformation[sh,
      Composition[TranslationTransform[{2 #1[[1, 1]], 0}],
       ReflectionTransform[{-1, 0}], RotationTransform[Pi/2]]],
     GeometricTransformation[sh, 
      Composition[TranslationTransform[{2 #1[[1, 1]], 0}],
       RotationTransform[Pi/2]]]}] &;

Usage:

DistributionChart[{dat, 1 + dat, 2 + dat}, ChartStyle -> "Rainbow", 
 ImageSize -> 400, GridLines -> {None, {0, 1, 2, 3, 4}}, ChartElementFunction -> cEF[]]

enter image description here

dat2 = # + dat & /@ RandomInteger[{0, 4}, {5}];
charts = DistributionChart[dat2, ChartStyle -> {Blue, Red, Green, Yellow, Brown},
     ImageSize -> 400, GridLines -> {None, Range[0, 6]}, PlotRange -> {0, 6},
     ChartElementFunction -> cEF[#2, #],
     PlotLabel -> (Style[Row[{"bandwith : ", #2, "  max extra bw : ", #1}] , 20])] &;

Grid@Partition[charts @@@ Tuples[{{{0, 0}, {1, 1}}, {.2, .1, .05} }],3]

enter image description here

See also: this answer on a similar issue with DistributionChart.

$\endgroup$
  • $\begingroup$ This solution works best for my data. Bandwith is a very good option to tweak the look of the resulting plots and keeps everything between min and max. Thank you very much :)! $\endgroup$ – Kardashev3 Oct 5 '14 at 18:22
  • $\begingroup$ @Kardashev3, my pleasure. Thank you for the Accept. $\endgroup$ – kglr Oct 5 '14 at 19:23
9
$\begingroup$

Both, DistributionChart and SmoothHistogram are models using a "smooth kernel density estimate".

Consider the simplest case with two points only:

DistributionChart[{0, 1}, GridLines -> Automatic]

enter image description here

SmoothHistogram[{0, 1}, GridLines -> Automatic]

enter image description here

For your data we get

dat = Flatten[{RandomReal[1., 10000], RandomReal[2., 2000]}];

DistributionChart[dat, GridLines -> Automatic]

enter image description here

SmoothHistogram[dat, GridLines -> Automatic]

enter image description here

Again, because of the smoothing, some data exceed the upper and lower limits.

An alternative without smoothing is the classic Histogram:

enter image description here

Update

With less data

dat = Flatten[{RandomReal[1., 1000], RandomReal[2., 200]}];

you also could employ

DistributionChart[dat,
 GridLines -> Automatic,
 ChartElementFunction -> "PointDensity",
 BarSpacing -> None]

enter image description here

$\endgroup$
  • $\begingroup$ Nice explanation! $\endgroup$ – Dr. belisarius Sep 25 '14 at 9:08
  • $\begingroup$ What is odd is that DistributionChart doesn't seem to allow a definition of a Kernel and a bandwidth, as in SmoothHistogram. (Does it?) The problem could be solved by choosing a smaller bandwidth. $\endgroup$ – rhermans Sep 25 '14 at 9:53
  • $\begingroup$ Thank you for the explanation. Now I understand the result of the plot. PointDensity would indeed work for a smaller sample. $\endgroup$ – Kardashev3 Oct 5 '14 at 18:18
6
$\begingroup$

You can show the histogram as per eldo's classic histogram above by using HistogramDensity for the ChartElementFunction. Some datasets with outliers plot better with transformed data (such as log), but perhaps not this one.

dat1 = Flatten[{RandomReal[1., 10000], RandomReal[2., 2000]}];
cht1=Table[DistributionChart[dat1, PlotRange -> {0., 2.},ChartElementFunction-> (ChartElementDataFunction["HistogramDensity", "Bins" -> iCef]) ],{iCef,{"Scott",{"Log", "Scott"}}}]

enter image description here

Take a look at this questions for a ChartElementFunction Options: Control parameters of different styles of DistributionChart

$\endgroup$
  • $\begingroup$ HistogramDensity is indeed another good approach for the visualisation of such data. $\endgroup$ – Kardashev3 Oct 5 '14 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.