Using + to extend the plot for compatibility to ggplot2 in R
auto aes = Aes!(string[], "x", string[], "y", string[], "colour")(["a", "b", "c", "b"], ["x", "y", "y", "x"], ["b", "b", "b", "b"]); auto gg = GGPlotD(); gg + geomLine(aes) + scale(); gg.save( "test6.png");
/// http://blackedder.github.io/ggplotd/images/noise.png import std.array : array; import std.math : sqrt; import std.algorithm : map; import std.range : repeat, iota; import std.random : uniform; // Generate some noisy data with reducing width auto f = (double x) { return x/(1+x); }; auto width = (double x) { return sqrt(0.1/(1+x)); }; auto xs = iota( 0, 10, 0.1 ).array; auto ysfit = xs.map!((x) => f(x)); auto ysnoise = xs.map!((x) => f(x) + uniform(-width(x),width(x))).array; auto aes = Aes!(typeof(xs), "x", typeof(ysnoise), "y", string[], "colour" )( xs, ysnoise, ("a").repeat(xs.length).array ); auto gg = GGPlotD().put( geomPoint( aes ) ); gg.put( geomLine( Aes!(typeof(xs), "x", typeof(ysfit), "y" )( xs, ysfit ) ) ); // auto ys2fit = xs.map!((x) => 1-f(x)); auto ys2noise = xs.map!((x) => 1-f(x) + uniform(-width(x),width(x))).array; gg.put( geomLine( Aes!(typeof(xs), "x", typeof(ys2fit), "y" )( xs, ys2fit) ) ) .put( geomPoint( Aes!(typeof(xs), "x", typeof(ys2noise), "y", string[], "colour" )( xs, ys2noise, ("b").repeat(xs.length).array) ) ); gg.save( "noise.png" );
/// http://blackedder.github.io/ggplotd/images/hist.png import std.array : array; import std.algorithm : map; import std.range : repeat, iota; import std.random : uniform; auto xs = iota(0,25,1).map!((x) => uniform(0.0,5)+uniform(0.0,5)).array; auto aes = Aes!(typeof(xs), "x")( xs ); auto gg = GGPlotD().put( geomHist( aes ) ); auto ys = (0.0).repeat( xs.length ).array; auto aesPs = aes.mergeRange( Aes!(double[], "y", double[], "colour" ) ( ys, ys ) ); gg.put( geomPoint( aesPs ) ); gg.save( "hist.png" );
/// http://blackedder.github.io/ggplotd/images/filled_hist.svg import std.array : array; import std.algorithm : map; import std.range : repeat, iota, chain; import std.random : uniform; auto xs = iota(0,50,1).map!((x) => uniform(0.0,5)+uniform(0.0,5)).array; auto cols = "a".repeat(25).chain("b".repeat(25)); auto aes = Aes!(typeof(xs), "x", typeof(cols), "colour", double[], "fill" )( xs, cols, 0.45.repeat(xs.length).array); auto gg = GGPlotD().put( geomHist( aes ) ); gg.save( "filled_hist.svg" );
Boxplot example
/// http://blackedder.github.io/ggplotd/images/boxplot.svg import std.array : array; import std.algorithm : map; import std.range : repeat, iota, chain; import std.random : uniform; auto xs = iota(0,50,1).map!((x) => uniform(0.0,5)+uniform(0.0,5)).array; auto cols = "a".repeat(25).chain("b".repeat(25)).array; auto aes = Aes!(typeof(xs), "x", typeof(cols), "colour", double[], "fill", typeof(cols), "label" )( xs, cols, 0.45.repeat(xs.length).array, cols); auto gg = GGPlotD().put( geomBox( aes ) ); gg.save( "boxplot.svg" );
/// http://blackedder.github.io/ggplotd/images/hist3D.svg import std.array : array; import std.algorithm : map; import std.range : repeat, iota; import std.random : uniform; auto xs = iota(0,100,1).map!((x) => uniform(0.0,5)+uniform(0.0,5)).array; auto ys = iota(0,100,1).map!((y) => uniform(0.0,5)+uniform(0.0,5)).array; auto aes = Aes!(typeof(xs), "x", typeof(ys), "y")( xs, ys); auto gg = GGPlotD().put( geomHist3D( aes ) ); gg.save( "hist3D.svg" );
Changing axes details
/// http://blackedder.github.io/ggplotd/images/axes.svg import std.array : array; import std.math : sqrt; import std.algorithm : map; import std.range : iota; // Generate some noisy data with reducing width auto f = (double x) { return x/(1+x); }; auto width = (double x) { return sqrt(0.1/(1+x)); }; auto xs = iota( 0, 10, 0.1 ).array; auto ysfit = xs.map!((x) => f(x)).array; auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", typeof(ysfit), "y" )( xs, ysfit ) ) ); // Setting range and label for xaxis gg.put( xaxisRange( 0, 8 ) ).put( xaxisLabel( "My xlabel" ) ); assertEqual( gg.xaxis.min, 0 ); // Setting range and label for yaxis gg.put( yaxisRange( 0, 2.0 ) ).put( yaxisLabel( "My ylabel" ) ); assertEqual( gg.yaxis.max, 2.0 ); assertEqual( gg.yaxis.label, "My ylabel" ); // change offset gg.put( xaxisOffset( 0.25 ) ).put( yaxisOffset( 0.5 ) ); // Change Margins gg.put( Margins( 60, 60, 40, 30 ) ); // Set a title gg.put( title( "And now for something completely different" ) ); assertEqual( gg.title.title, "And now for something completely different" ); // Saving on a 500x300 pixel surface gg.save( "axes.svg", 500, 300 );
Polygon
/// http://blackedder.github.io/ggplotd/images/polygon.png auto gg = GGPlotD().put( geomPolygon( Aes!( double[], "x", double[], "y", double[], "colour" )( [1,0,0], [ 1, 1, 0 ], [1,0.1,0] ) ) ); gg.save( "polygon.png" );
Setting background colour
/// http://blackedder.github.io/ggplotd/images/background.svg import ggplotd.theme; auto gg = GGPlotD().put( background( RGBA(0.7,0.7,0.7,1) ) ); gg.put( geomPoint( Aes!( double[], "x", double[], "y", double[], "colour" )( [1,0,0], [ 1, 1, 0 ], [1,0.1,0] ) ) ); gg.save( "background.svg" );
Other data type
/// http://blackedder.github.io/ggplotd/images/data.png import std.array : array; import std.math : sqrt; import std.algorithm : map; import std.range : repeat, iota; import std.random : uniform; struct Point { double x; double y; } // Generate some noisy data with reducing width auto f = (double x) { return x/(1+x); }; auto width = (double x) { return sqrt(0.1/(1+x)); }; auto xs = iota( 0, 10, 0.1 ).array; auto points = xs.map!((x) => Point(x, f(x) + uniform(-width(x),width(x)))); auto gg = GGPlotD().put( geomPoint( points ) ); gg.save( "data.png" );