/* execute this script
 *
 * (1) wait for user between examples:
 *  demo ("rtest_statgraph.mac")
 *
 * (2) or noninteractively:
 *  batch ("rtest_statgraph.mac");
 *
 * draw_renderer is set to gnuplot.
 * On my Linux system, that causes Gnuplot to open a new window
 * for each plot. I think that's desirable, so that one can
 * inspect the plots afterwards.
 */

"Some preparations for the following plots." $

(load("descriptive"),
 load("distrib"),
 draw_renderer: 'gnuplot,
 cache_all_titles (['L]) :=
     block ([n: length (L)],
            for k thru n
                do title_cache[L[k]]: printf (false, "plot ~d/~d, ~a", k, n, sanitize (L[k]))),
 sanitize (s) := ssubst ("\\_", "_", s),
 cache_all_titles (
     "barsplot: Ages in biomed.data",
     "barsplot: Two random samples",
     "barsplot: Asking for something in four groups, separate bars",
     "barsplot: Asking for something in four groups, stacked bars",
     "barsplot: Random data, blue bars",
     "barsplot: Random data, red bars",
     "boxplot: Windspeed in knots from wind.data",
     "boxplot: Horizontal boxes",
     "histogram: pi digits from pidigits.data",
     "histogram + explicit: Random data from normal distribution, uniform bins",
     "histogram + explicit: Random data from normal distribution, nonuniform bins",
     "piechart: Digit frequencies in pi from pidigits.data",
     "scatterplot: 1-dimensional random data",
     "starplot: Two random samples"
 ));

barsplot(
                  col(m,2),
                  title        = title_cache ["barsplot: Ages in biomed.data"],
                  xlabel       = "years",
                  box_width    = 1/2,
                  fill_density = 3/4), m : read_matrix (file_search ("biomed.data"));


barsplot(
                  l1,l2,
                  title        = title_cache ["barsplot: Two random samples"],
                  box_width    = 1,
                  fill_density = 1,
                  bars_colors  = [black, grey],
                  frequency = relative,
                  sample_keys = ["A", "B"]),
                l1:makelist(random(10),k,1,50),
                l2:makelist(random(10),k,1,100);


barsplot(
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  title  = title_cache ["barsplot: Asking for something in four groups, separate bars"],
                  ylabel = "# of individuals",
                  groups_gap   = 3,
                  fill_density = 0.5,
                  ordering     = ordergreatp);

barsplot(
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  makelist([Yes, No, Maybe][random(3)+1],k,1,50),
                  title  = title_cache ["barsplot: Asking for something in four groups, stacked bars"],
                  ylabel = "# of individuals",
                  grouping     = stacked,
                  fill_density = 0.5,
                  ordering     = ordergreatp);

block([l1:makelist(random(10),k,1,50),
  l2:makelist(random(10),k,1,100),
  bp1, bp2],
  bp1 :
                  barsplot_description(
                   l1,
                   title = title_cache ["barsplot: Random data, blue bars"],
                   box_width = 1,
                   fill_density = 0.5,
                   bars_colors = [blue],
                   frequency = relative),
  bp2 :
                  barsplot_description(
                   l2,
                   title = title_cache ["barsplot: Random data, red bars"],
                   box_width = 1,
                   fill_density = 0.5,
                   bars_colors = [red],
                   frequency = relative),
  draw(gr2d(bp1), gr2d(bp2)));


boxplot(s2,
                  box_width  = 0.2,
                  title      = title_cache ["boxplot: Windspeed in knots from wind.data"],
                  xlabel     = "Stations",
                  color      = red,
                  line_width = 2), s2 : read_matrix(file_search("wind.data"));

boxplot (A,
         box_orientation = horizontal,
         title = title_cache ["boxplot: Horizontal boxes"]),
         A :     [[6, 4, 6, 2, 4, 8, 6, 4, 6, 4, 3, 2],
                  [8, 10, 7, 9, 12, 8, 10],
                  [16, 13, 17, 12, 11, 18, 13, 18, 14, 12]];


histogram (
                     s1,
                     nclasses     = 8,
                     title        = title_cache ["histogram: pi digits from pidigits.data"],
                     xlabel       = "digits",
                     ylabel       = "Absolute frequency",
                     fill_color   = grey,
                     fill_density = 0.6), s1 : read_list (file_search ("pidigits.data"));

block([m: 14, s: 2, s2],
  s2: random_normal(m, s, 1000),
  draw2d(
    grid   = true,
    title = title_cache ["histogram + explicit: Random data from normal distribution, uniform bins"],
    histogram_description(
      s2,
      nclasses     = 9,
      frequency    = density,
      fill_density = 0.5),
    xrange = [5, 25],
    explicit(pdf_normal(x,m,s), x, m - 3*s, m + 3* s)));

block([m: 14, s: 2, s2],
  s2: random_normal(m, s, 1000),
  draw2d(
    grid   = true,
    title = title_cache ["histogram + explicit: Random data from normal distribution, nonuniform bins"],
    histogram_description(
      s2,
      nclasses     = setify (makelist (float (quantile_normal (k/13, m, s)), k, 1, 12)),
      frequency    = density,
      fill_density = 0.5),
    xrange = [5, 25],
    explicit(pdf_normal(x,m,s), x, m - 3*s, m + 3* s)));

piechart(
                  s1,
                  xrange  = [-1.1, 1.3],
                  yrange  = [-1.1, 1.1],
                  title   = title_cache ["piechart: Digit frequencies in pi from pidigits.data"]),
                      s1 : read_list (file_search ("pidigits.data"));

scatterplot(
                  random_normal(0,1,200),
                  title      = title_cache ["scatterplot: 1-dimensional random data"],
                  xaxis      = true,
                  point_size = 2,
                  dimensions = [600,150])$

block([l1: makelist(random(10),k,1,50),
  l2: makelist(random(10),k,1,200)],
          starplot(
                  l1, l2,
                  title       = title_cache ["starplot: Two random samples"],
                  stars_colors = [blue,red],
                  sample_keys = ["1st sample", "2nd sample"],
                  star_center = [1,2],
                  star_radius = 4,
                  proportional_axes = xy,
                  line_width = 2 ));

stemplot(
                  random_normal(15, 6, 100),
                  leaf_unit = 0.1);
