Back to main page
Back to draw contents

Statistical graphics from package 'descriptive'

This section shows some examples on graphic functions defined in the descriptive package. These functions have been re-defined to take advange of the draw package. See the Maxima manual for the updated documentation.

Write first load(descriptive) to load the package. If draw is not in memory, it will be automatically loaded.


Playing with function histogram. File pidigits.data is read and its contents stored in list s1. Then the histogram is plotted.

s1 : read_list (file_search ("pidigits.data"))$

histogram (s1,
           'title        = "pi digits",
           'xlabel       = "digits",
           'ylabel       = "Absolute frequency",
           'fill_color   = grey,
           'fill_density = 0.6)$

Global variable draw_compound, defined in draw, is now set to false to skip plotting; we also store the output in variable scene. We can use later this variable to draw more complex scenes containing our histogram.

draw_compound:false$

scene: histogram (s1,
                  'title        = "pi digits",
                  'xlabel       = "digits",
                  'ylabel       = "Absolute frequency",
                  'fill_color   = grey,
                  'fill_density = 0.6);

This is now the contents of variable scene, with the histogram and its options:

[  title  = "pi digits",
   xlabel = "digits",
   ylabel = "Absolute frequency", 
   fill_color   = grey,
   fill_density = 0.6, 
   bars([0.45, 8, 0.9], [1.35, 8, 0.9], [2.25, 12, 0.9], [3.15, 12, 0.9], 
        [4.05, 10, 0.9], [4.95, 8, 0.9], [5.85, 9, 0.9], [6.75, 8, 0.9], 
        [7.65, 12, 0.9], [8.55, 13, 0.9]),
   xrange = [- 0.45, 9.45], 
   yrange = [- 0.65, 13.65]  ]

Suppose we want to plot this histogram together with a blue line. What we get is not exactly what we expected:

apply(draw2d, append([color=blue, explicit(2*x,x,-5,20)],
                     scene));

If we don't want the blue line behind the bars, change the order of the graphic objects. We also add the grid lines.

apply(draw2d, append(scene,
                     [color=blue, grid=true, xrange=auto, xtics=auto, explicit(2*x,x,-5,20)] ));

Don't forget to set draw_compound to its default value:

draw_compound:true $

Function barsplot is similar to histogram, but it is oriented to discrete and categorical variables. First we read file biomed.data, which contains a multivariate sample of size 100 and dimension 6: patient group (A or B), age and four blood continuous measures (see first record below). After that, we plot the barsplots for discrete components group and age.

(%i1) s3 : read_matrix (file_search ("biomed.data"))$
(%i2) s3[1];
(%o2)                  [A, 30, 167.0, 89.0, 25.6, 364]
(%i3) barsplot(col(s3,1),
               title        = "Groups of patients",
               xlabel       = "Group",
               ylabel       = "# of individuals",
               fill_color   = blue,
               fill_density = 0.5)$
(%i4) barsplot(col(s3,2),
               title        = "Ages",
               xlabel       = "years",
               fill_density = 0.3)$

If we want a multiplot with both diagrams, we need to set global variable draw_compound to false. The Postscript terminal is now chosen.

draw_compound: false$
bp1: barsplot(col(s3,1),
              title        = "Groups of patients",
              xlabel       = "Group",
              ylabel       = "# of individuals",
              fill_color   = blue,
              fill_density = 0.5)$
bp2: barsplot(col(s3,2),
              title        = "Ages",
              xlabel       = "years",
              fill_density = 0.3)$
draw(apply(gr2d, bp1),
     apply(gr2d, bp2),
     terminal=eps_color)$

Box-and-whishker diagrams can be build with the boxplot function.

s2 : read_matrix(file_search("wind.data"))$
boxplot(s2,
        title  = "Windspeed in knots",
        xlabel = "Stations")$

Let's change the widths of the boxes and some other things. Instead of sample numbers, we want to write the names of the stations; but since function boxplot overrides the xtics passed as arguments, we need to play with global variable draw_compound and specify the new tics after the box diagram. See how boxes widths are changed by means of option box_width, which is a specific boxplot option, not a general one from the draw package.

draw_compound: false $
bw: boxplot(s2,
            title      = "Windspeed in knots",
            xlabel     = "Stations",
            box_width  = 0.2, /* <== this is a specific boxplot option!!*/
            color      = red,
            line_width = 2)$
apply(draw2d,append(bw,[xtics={["CLA",1],["MUL",2],["CLO",3],["BEL",4],["MAL",5]}]))$

Scatter diagrams are drawn with function scatterplot.

Scatter diagram of a one-dimensional simulated Gaussian sample.

load(distrib)$
scatterplot(
  random_normal(0,1,200),
  xaxis      = true,
  point_size = 2,
  terminal   = eps,
  eps_width  = 10,
  eps_height = 2)$

Scatter diagram of a two-dimensional sample. Remember that file "wind.data" records a five-dimensional sample of length 100.

s2 : read_matrix (file_search ("wind.data"))$
scatterplot(
  submatrix(s2, 1,2,3),
  title      = "Data from stations #4 and #5",
  point_type = diamant,
  point_size = 2,
  color      = blue)$

Scatter diagram of a three-dimensional sample.

scatterplot(submatrix (s2, 1,2))$

Scatter diagram of a five-dimensional sample. See how options affect the different aspects of the plot; in particular, nclasses, which is a specific histogram directive.

scatterplot(
  s2,
  nclasses     = 6,   /* <== this is not a general draw option!! */
  fill_color   = blue,
  fill_density = 0.3,

  /* points options */
  color      = red,
  point_type = circle,
  point_size = 0.5,

  /* global option */
  xtics        = 5    /* <== tics every 5 units */    )$

A pair of examples on pie-charts. To change the center and/or the radius, use piechart specific options pie_center and pie_radius.

s1 : read_list (file_search ("pidigits.data"))$
piechart(s1)$

Let's fine-tune the previous diagram. Colours are now different, since they are randomly generated each time the function is called.

piechart(
  s1,
  xrange      = [-1.1, 1.3],
  yrange      = [-1.1, 1.1],
  axis_top    = false,
  axis_right  = false,
  axis_left   = false,
  axis_bottom = false,
  xtics       = none,
  ytics       = none,
  title       = "Digit frequencies in pi")$

Back to main page
Back to draw contents


by Mario Rodríguez Riotorto