10
$\begingroup$

Sometimes it is nice not just to show the median value of binned data, but also the quartiles thereof. However this doesn't seem to be so straightforward using ListLinePlot. What is a good approach to making binned, quartile, ListLinePlots?

Example below: enter image description here

Update

Code available here

$\endgroup$
6
  • 1
    $\begingroup$ You can find the quartile curves using Quantile Regression. See also the Quantile Regression posts in MSE like this one. $\endgroup$ Commented Mar 23, 2017 at 3:28
  • $\begingroup$ @AntonAntonov I have tried that with a list of tuples and didn't get any output.... $\endgroup$
    – SumNeuron
    Commented Mar 23, 2017 at 12:41
  • $\begingroup$ Well it is not supposed to be used on lists of tuples, but lists of pairs, each pair seen as {time, value}. $\endgroup$ Commented Mar 23, 2017 at 12:53
  • $\begingroup$ @AntonAntonov my apologies for not clarifying, the tuples are effectively two values {bin, value}, so it should work.... $\endgroup$
    – SumNeuron
    Commented Mar 23, 2017 at 12:59
  • 1
    $\begingroup$ Well, you have not provided data in both your question and your answer, so I cannot tell you what is wrong. Were you able to reproduce the QR computations in this answer? Both success and failure would give you hints why that QR package does not work for you. $\endgroup$ Commented Mar 23, 2017 at 13:40

1 Answer 1

6
$\begingroup$

Code

The code is publicly available here.


I am answering my own question based off of something I worked out. I highly doubt that it is the best way to make quartile plots, but it works.

The code provided has a function that makes the BinnedQuartilePlot for either a Dataset, list of Datasets (assumes you need the same keys for all of them), list of pairs, and list of lists of pairs. Two examples are below:

BinnedQuartilePlot[dataset, "keyToBin", "keyOfValues", "Bins" -> 15]

enter image description here

BinnedQuartilePlot[{dataset, dataset2}, "keyToBin", "keyOfValues",
                   "Bins" -> 15, "SwatchLegend" -> True, 
                   "Legends" -> {"Data1", "Data2"}]

enter image description here

I'll just go over the single Dataset case here.

Single Dataset

Suppose you have a dataset, data, from which you care about two values, x1 and x2.

Then we can extract the two values we care about by:

extractedPairs = Normal@data[All, {#x1, #x2}&];

Given the nature of x1, we want to bin x1. One way to bin the data (although one could extract this from the Histogram suite of functions) is as follows:

(* Set helper parameters *)
min = Min[First@First@SortBy[extractedPairs, First]];
max = Max[First@Last@SortBy[extractedPairs, First]];
bins = 10;
binStep = (max - min) / bins;

(* Bin values *)
Table[
  Cases[listOfPairs, 
    {x_ /; Round@((x - minBin)/binStep) == (bin - minBin)/binStep, y_} -> {bin, y}, 
    Infinity
  ], 
 {bin, minBin, maxBin, binStep}
]

Since we want the quartiles of the bins, we can just apply Quartiles and then Transpose the results to prepare them for ListLinePlot:

binnedQuartiles = Transpose /@ Quartiles /@ binnedValues;

This gives us our quartiles as paired triples e.g.

{{{bin1, q1}, {bin1, q2}, {bin1, q3}},
 {{bin2, q1}, {bin2, q2}, {bin2, q3}}, ...}

For list plot we need just the first pairs, second pairs, and the third pairs in their own respective lists, which we can make using:

listLineBinnedQuartiles = 
  Table[Flatten@Take[#, {i}] & /@ binnedQuartiles, {i, 1, 3}];

Thus finely we can make our quartile plot:

ListLinePlot[listLineBinnedQuartiles, 
 PlotStyle -> getNQuartileColors[1], Filling -> {1 -> {2}, 3 -> {2}}, 
 FillingStyle -> Opacity[0.05]]

enter image description here

But all of this is encapsulated in BinnedQuartilePlot in the linked code.

Multiple Datasets

This requires minor changes to the code to work on lists of Datasets rather than just a single one. These changes are quite simple and easy to spot, e.g. to updated extractedPairs, it becomes

extractedPairs = Normal@#[All, #x1, #x2} &] & /@ listOfDatasets;

Notes

There is (albeit poorly) simulated data with the linked code. Feel free to use your own.

There are a host of color functions as well. This is due mainly to needing to make custom PlotStyles for the quartiles and then obtaining the same colors for the SwatchLegend. If you don't like "Pastel" use the Option "ColorScheme" to change it to whatever you like.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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