public auto statFunction(FUNC, T)(
FUNC func,
T min,
T max,
size_t precision = 50)
if(isCallable!FUNC)
Create Aes based on a function
Parameters
func | Function |
min | x coordinate to start from |
max | x coordinate to end at |
precision | Number of points to calculate |
Examples
/// http://blackedder.github.io/ggplotd/images/function.png
import std.random : uniform;
import std.typecons : Tuple; import ggplotd.stat : statFunction;
import ggplotd.ggplotd : GGPlotD;
import ggplotd.geom : geomLine, geomPoint;
import ggplotd.range : mergeRange;
auto f = (double x) { return x / (1 + x); };
auto aes = statFunction(f, 0.0, 10);
auto gg = GGPlotD().put(geomLine(aes));
// Generate some noisy points
auto f2 = (double x) { return x / (1 + x) * uniform(0.75, 1.25); };
auto aes2 = f2.statFunction(0.0, 10, 25);
// Show points in different colour
auto withColour = Tuple!(string, "colour")("aquamarine").mergeRange(aes2);
gg = gg.put(withColour.geomPoint);
gg.save("function.png");
private auto binID(T)(
T value,
T min,
T max,
T width)
Binning
public auto statHist(AES)(
AES aesRaw,
size_t noBins = 0)
Create Aes that specifies the bins to draw an histogram
Parameters
aesRaw | Data that the histogram will be based on |
noBins | Optional number of bins. On a value of 0 the number of bins will be chosen automatically. |
Returns
Range that holds rectangles representing different bins
public auto statHist2D(AES)(
AES aesRange,
size_t noBinsX = 0,
size_t noBinsY = 0)
Create Aes that specifies the bins to draw an histogram
Parameters
aesRange | Data that the histogram will be based on |
noBinsX | Optional number of bins for x axis. On a value of 0 the number of bins will be chosen automatically. |
noBinsY | Optional number of bins for y axis. On a value of 0 the number of bins will be chosen automatically. |
Returns
Range that holds rectangles representing different bins
public auto statDensity(AES)(AES aesRange)
Calculate kernel density for given data
Parameters
aesRange | Data that the histogram will be based on |
Returns
InputRange that holds x and y coordinates for the kernel
Example
import std.stdio : writeln;
import std.algorithm : map;
import std.array : array;
import std.random : uniform;
import std.range : chain, iota, repeat, walkLength;
import ggplotd.aes : Aes;
auto xs = iota(0,100,1)
.map!((i)=>uniform(0,0.75)+uniform(0.25,1))
.array;
auto dens = statDensity( Aes!( typeof(xs), "x")( xs ) );
auto dim = dens.walkLength;
assertGreaterThan( dim, 1 );
// Test that grouping leads to longer (twice as long) result
auto cols = chain("a".repeat(50),"b".repeat(50) );
auto dens2 = statDensity( Aes!(typeof(cols), "colour", typeof(xs), "x")( cols, xs ) );
assertGreaterThan( dens2.walkLength, 1.9*dim );
assertLessThan( dens2.walkLength, 2.1*dim );
// Test that colour is passed through (merged)
assertEqual( dens2.front.colour.length, 1 );
// Test single point
xs = [1];
dens = statDensity( Aes!( typeof(xs), "x")( xs ) );
assertEqual(dens.walkLength, 3);
public auto statDensity2D(AES)(AES aesRange)
Calculate kernel density for given x and y data
Parameters
aesRange | Data that the histogram will be based on |
Returns
Range of ranges that holds polygons for the kernel
Example
import std.algorithm : map;
import std.array : array;
import std.random : uniform;
import std.range : iota, walkLength;
import ggplotd.aes : Aes;
auto xs = iota(0,500,1).map!((x) => uniform(0.0,5)+uniform(0.0,5))
.array;
auto ys = iota(0,500,1).map!((y) => uniform(1.0,1.5)+uniform(1.0,1.5))
.array;
auto aes = Aes!(typeof(xs), "x", typeof(ys), "y")( xs, ys);
auto sD = statDensity2D( aes );
assertGreaterThan( sD.walkLength, 1150 );
assertLessThan( sD.walkLength, 1450 );
assertEqual( sD.front.walkLength, 3 );
// One value
xs = [1];
ys = [2];
aes = Aes!(typeof(xs), "x", typeof(ys), "y")( xs, ys);
sD = statDensity2D( aes );
assertGreaterThan(sD.walkLength, 0);
Functions
autostatFunction | Create Aes based on a function | |
private, autobinID | Binning | |
autostatHist | Create Aes that specifies the bins to draw an histogram | |
autostatHist2D | Create Aes that specifies the bins to draw an histogram | |
autostatDensity | Calculate kernel density for given data | |
autostatDensity2D | Calculate kernel density for given x and y data |