5. Graphic Commands


2430 setmaskp (data, color, layout, size)
influences the layout, size and color of data points
2433 setmaskl (data, lines, color, type, thickness)
influences the layout, size, type and color between data points
2436 setmaskt (data, labels, color, direction, size)
influences the appearance of text at the data points
2439 setgopt (d,row,col, optname,optval,..., optnameN,optvalN)
influences several parameters of plots and displays

The displays created in the previous examples used either the default settings for displaying the data points, or were modified via 2442 setmask. However, 2445 setmask is a rather slow quantlet and it might be much quicker to use the built-in commands. We will show you how to change their color, graphical representation and size, and how to connect and label the data points. All these tasks are not performed by 2448 show itself, but rather by specific commands that have to preceed 2451 show. These commands are 2454 setmaskp, 2457 setmaskl and 2460 setmaskt. They are merged in the quantlet 2463 setmask.


5.1 Controlling Data Points

To control the color, the graphical representation (symbol) and the size of each data point, you must use 2468 setmaskp, which must preceed 2471 show. That is, first you call 2474 setmaskp to specify color, graphical representation and size of the data you want to plot. Then you call 2477 show to actually plot the data.


  x  = 1:100                    ; creates variable x that takes 

                                ;    on the values 1, ..., 100

  y  = sin(x/20)+uniform(100)/5 ; creates y-variable as sin(x/20) 

                                ;    and uniform error

  data = x~y                    ; puts x, y together to form data

  ;

  ; now we call setmaskp to specify the layout for each point of 

  ; the matrix, all data points are colored red (=4), shown as 

  ; circles (=3) and have the default size (=8)

  ;

  setmaskp(data, 4, 3, 8)       ; calls setmaskp

  d = createdisplay(1, 1)       ; creates the display

  show(d, 1, 1, data)           ; use show puts data into display

2481graph61.xpl

Figure: Coloring of data points by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig61.ps}

You can replace setmaskp(data, 4, 3, 8) by


  data = setmask(data, "red", "medium", "circle")

Note two remarks


5.2 Color of Data Points

Each color is represented by an integer: 0 - black, 1 - blue, 2 - green, 3 - cyan, 4 - red, 5 - magenta, 6 - yellow and 7 - white. You may assign the same color to each data point. In this case the second argument of 2511 setmaskp has to be a single integer as in the previous example.

Alternatively, you can assign different colors to different points, in which case the second argument of 2514 setmaskp has to be a column vector of integers with the same number of rows as the data matrix. Here is an example with different colors for different points:


  x  = 1:100                    ; creates variable x that takes 

                                ;    on the values 1, ..., 100

  y  = sin(x/20)+uniform(100)/5 ; creates y-variable as sin(x/20) 

                                ;    and uniform error

  data = x~y                    ; puts x, y together to form data

  ;

  ; now we will create a vector of integers that assigns the 

  ; color red (=4) to the first 50 data points and the color 

  ; magenta (=5) to the next 50 data points

  ;

  color = 4*matrix(50)|5*matrix(50)

  ;

  ; now we call setmaskp; as in the previous example, all points

  ; of data are shown as circles (=3) and have the default

  ; size (=8)

  ;  

  setmaskp(data, color, 3, 8)

  d = createdisplay(1, 1)       ; creates the display

  show(d, 1, 1, data)           ; use show puts data into display

2518graph62.xpl

Figure: Different coloring of data points by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig62.ps}

To achieve the same result using 2534 setmask, you have to create the string vector that is the equivalent to the numerical vector color.


  library("plot")

  mycolor = string("red", 1:50) | string("magenta", 51:100)

  data    = setmask(data, mycolor)

  d       = createdisplay(1,1)

  show (d, 1, 1, data)


5.3 Symbol of Data Points

Similarly, you may represent each data point by the same graphical symbol (in which case the third argument of 2539 setmaskp has to be a single integer), or you may represent different data points by different graphical symbols (in which case the third argument of 2542 setmaskp has to be a column vector of integers with the same number of rows as the data matrix).

Here is the assignment of integers to graphical symbols:
0 - empty, 1 - point, 2 - rectangle, 3 - circle, 4 - triangle, 5 - x-symbol, 6 - rhombus, 7 - filled rectangle, 8 - filled circle, 9 - filled rhombus,
[3] 10 - filled triangle, 11 - cross, 12 - star, 13 - grid, 14 - different cross.

Here is an example that assigns (through the vector layout) a circle to the first 25 data points, a triangle to the next 25 data points, a cross to the following 25 data points, and a rhombus to the last 25 data points.


  x      = 1:100

  y      = sin(x/20) +uniform(100, 1) /10

  data   = x~y

  color  = 4*matrix(50)|5*matrix(50)

  layout = 3*matrix(25)|4*matrix(25)|5*matrix(25)|6*matrix(25)

  setmaskp(data, color, layout, 8)

  d     = createdisplay(1, 1)

  show(d, 1, 1, data)

2546graph63.xpl

Figure: Different appearance of data points by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig63.ps}

Again, we give the equivalent code using 2562 setmask:


  library("plot")

  x       = 1:100

  y       = sin(x/20) +uniform(100, 1) /10

  data    = x~y

  mycolor = string("red", 1:50)|string("magenta", 51:100)

  mystyle = string("circle", 1:25)|string("triangle", 26:50)

  mystyle = mystyle|string("xsymbol",51:75)

  mystyle = mystyle|string("rhomb",76:100)

  data    = setmask(data, mycolor, mystyle)

  d       = createdisplay(1, 1)

  show(d, 1, 1, data)


5.4 Size of Data Points

Similar to the way 2567 setmaskp handles color and graphical representation, you can either give the same size to all data points (fourth argument of 2570 setmaskp is a scalar integer) or assign different sizes to different data points (fourth argument of 2573 setmaskp is a column vector of integers that has the same number of rows as the data matrix). The default size is assigned to the integer 8. You can choose any integer among 1, ..., 15 with 1 representing the smallest and 15 representing the largest possible size.

Here is an example that assigns (through the vector size) a rather small size (4) to the first 50 data points and the maximum size (15) to the last 50 data points:


  x      = 1:100

  y      = sin(x/20) + uniform(100, 1)/10

  data   = x~y

  color  = 4*matrix(50)|5*matrix(50)

  layout = 3*matrix(25)|4*matrix(25)|5*matrix(25)|6*matrix(25)

  size   = 4*matrix(50)|15*matrix(50)

  setmaskp(data, color, layout, size)

  d      = createdisplay(1, 1)

  show(d, 1, 1, data)

2577graph64.xpl

Figure: Different appearance of data points by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig64.ps}

Equivalently, you can replace the last five lines by


  library("plot")

  x       = 1:100

  y       = sin(x/20) + uniform(100, 1)/10

  data    = x~y

  mycolor = string("red", 1:50)|string("magenta", 51:100)

  mystyle = string("circle", 1:25)|string("triangle", 26:50)

  mystyle = mystyle|string("xsymbol" ,51:75)

  mystyle = mystyle|string("rhomb", 76:100)

  mysize  = string("small", 1:50)|string("huge", 51:100)

  data    = setmask(data, mycolor, mystyle, mysize)

  d       = createdisplay(1, 1)

  show(d, 1, 1, data)


5.5 Connection of Data Points

Suppose you want to plot a data matrix and connect some or all of the points by lines. Then you have to use 2595 setmaskl to specify

Use 2598 setmaskl first to specify these options, then use 2601 show to actually generate the plot.

2604 setmaskl is very flexible and allows you to specify in detail which points to connect and which kind of line to use. Here is a simple example that connects all points of a data matrix with a solid blue line:


  randomize(666)          ; sets seed for random number generator

  n = 6                   ; sample size

  x = 4:(3+n)             ; generates x-variable as 4,...,9

  y = 2*x+normal(n)       ; generates y-variable

  z = x~y                 ; composes data matrix

  d = createdisplay(1, 1) ; creates display

  ;

  ; now we will create a row vector pm that tells setmaskl to 

  ; connect all 6 points of the data matrix in the same order 

  ; as they appear

  ;

  pm    = (1:n)'          ; generates row vector 1,...,6

  color = 1               ; blue line

  art   = 1               ; solid line

  thick = 5               ; thick line

  setmaskl(z, pm, color ,art ,thick) ; call setmaskl

  show(d, 1, 1, z)        ; call show to plot the data

2608graph65.xpl

Figure: Generating lines by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig65.ps}

Again, 2624 setmask provides a convenient, more intuitive alternative:


  library("plot")

  randomize(666)

  n = 6

  x = 4:(3+n)

  y = 2 *x +normal(n)

  z = x~y

  z = setmask(z, "line", "blue", "thick", "solid")

  show(d, 1, 1, z)

The crucial step in the previous example (and in the use of 2627 setmaskl in general) is the specification of the vector that tells 2630 setmaskl which points to connect (it is called pm in the example). We will give more examples for possible specifications of pm shortly. However, before we do that, we want to give the assignments of integers to color, appearance and thickness of a line:

color:
0 - black, 1 - blue, 2 - green, 3 - cyan, 4 - red, 5 - magenta, 6 - yellow, 7 - white
appearance:
0 - invisible, 1 - solid, 2 - finely dashed, 3 - less finely dashed, 4 - even less finely dashed , and so on
thickness:
0 will produce the thinnest line and increasing integers up to 15 produce lines of increasing thickness

Now back to specifying pm. Suppose that in the previous example you want to connect only the 1st, 3rd and 6th points with a line. Then you have to generate pm as follows:


  pm = 1~3~6

The points to be connected do not have to be in increasing order. For instance,


  pm = 1~3~6~2

will connect the 1st point with the 3rd, the 3rd with the 6th and finally the 6th with the 2nd.

Suppose you want to draw a line, interrupt it and continue it again. Interrupting is done by including a 0 (zero):


  pm = 1~3~0~5~6

will connect the 1st and the 3rd points, then interrupt the line and continue it by connecting the 5th and the 6th points.

Does 2633 setmaskl allow to draw several lines? Yes, but in this case pm has to be specified as a matrix where each row corresponds to a line. For instance


  pm = (1~3~5)|(2~4~6)

will connect the 1st, 3rd and 5th points with a line and the 2nd, 4th and 6th points with a different line. Note that if you want to draw two lines, you may also specify color, appearance and thickness for both lines -- as shown in the following complete example:

  randomize(666)          ; sets seed for random number generator

  n = 6                   ; sample size

  x = 4:(3+n)             ; generates x-variable as 4,...,9

  y = 2*x+normal(n)       ; generates y-variable

  z = x~y                 ; composes data matrix

  pm    = (1~3~5)|(2~4~6) 

  color = 1|4             ; sets color of first line to blue (=1)

                          ;    and of second line to red (=4)

  art   = 1|2             ; first line will be solid (=1), 

                          ;    second line will be dashed (=2)

  setmaskl(z, pm, color, art) 

                          ; calls setmaskl

  d = createdisplay(1, 1) ; creates a display

  show(d, 1, 1, z)        ; calls show to plot data

2637graph66.xpl

Figure: Mixed lines and data points by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig66.ps}

The last five lines can be replaced by the following code which uses 2653 setmask instead of 2656 setmaskl:


  library("plot")

  randomize(666)

  n  = 6

  x  = 4:(3+n)

  y  = 2*x+normal(n)

  z  = x~y

  pm = (1~3~5)|(2~4~6)

  c  = "blue"|"red"

  t  = "thin"|"thick"

  a  = "solid"|"dotted"

  z  = setmask(z, "line", pm, c, t, a)

  d = createdisplay(1, 1)

  show(d, 1, 1, z)

Of course, by adding more rows to the pm matrix, you can draw more than two lines.


5.6 Label of Data Points

To label the data points, you have to use 2661 setmaskt. 2664 setmaskt allows you to control the color, the position (relative to the associated data point) and the font size of the label. The labels themselves have to be collected in a text vector and given to 2667 setmaskt as an input. Color, position and size are controlled by the following integers:

color:
0 - black, 1 - blue, 2 - green, 3 - cyan, 4 - red, 5 - magenta, 6 - yellow, 7 - white
position:
-1 - no label at all, 0 - a centered label, 3 - a label to the right of the point, 6 - a label below the point, 9 - a label to the left of the point,
[3]12 - a label above the point. Note that currently the positions are only considered for PostScript output.
font size:
font sizes 12 to 16 are supported.

Here is a very simple example where each label gets the same color, position and size:


  x     = 1:6

  x     = x~x

  text  = "Point1"|"Point2"|"Point3"|"Point4"|"Point5"|"Point6"

  color = 1

  position = 3

  size  = 16

  setmaskt(x, text, color, position, size)

  d     = createdisplay(1, 1)

  show(d, 1, 1, x)

2671graph67.xpl

Figure: Labeling of data points by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig67.ps}

The equivalent code using 2687 setmask is given by


  library("plot")

  x = 1:6

  x = x~x

  mytext = "Point1"|"Point2"|"Point3"|"Point4"|"Point5"|"Point6"

  x = setmask(x, "points","text",mytext,"blue","right","medium")

  d = createdisplay(1, 1)

  show(d, 1, 1, x)

If you want to assign different colors, positions, or sizes to different labels, you must specify the respective arguments of 2690 setmaskt as column vectors of integers. Here is an illustrative example where the text of the labels has been chosen to match their positions as specified by the vector position:


  x    = 1:6

  x    = x~x

  text     = "Right"|"Under"|"Left"|"Over"|"Center"|"No"

  color    = 1|2|3|4|5|6

  position = 3|6|9|12|0|(-1)

  size     = 12|13|14|15|16|16

  setmaskt(x, text, color,position, size)

  d    = createdisplay(1, 1)

  show(d, 1, 1, x)

2694graph68.xpl

Figure: Labeling of data points at different positions by graphic commands or quantlets.
\includegraphics[scale=0.6]{grfig68.ps}

Using 2710 setmask instead of 2713 setmaskt, we can write:


  x = 1:6

  x = x~x

  mytext  = "Right"|"Under"|"Left"|"Over"|"Center"|"No"

  mycolor = "blue"|"green"|"cyan"|"red"|"magenta"|"yellow"

  mypos   = "right"|"below"|"left"|"above"|"center"|"request"

  mysize  = "small"|"medium"|"medium"|"large"|"large"|"huge"

  x = setmask(x, "points", "text",mytext,mycolor,mysize,mypos)

  d = createdisplay(1, 1)

  show(d, 1, 1, x)


5.7 Title and Axes Labels

The layout of the display is controlled by 2718 setgopt. In order to work, 2721 setgopt has to be called after you have called 2724 show.

Here is an example that uses three of 2727 setgopt's seventeen options:


  x    = 1:100

  y    = sqrt(x)

  data = x~y

  d    = createdisplay(1, 1)

  show(d, 1, 1, data)

  title  = "Plot of Sqrt(x)"

  ylabel = "y = sqrt(x)"

  setgopt(d, 1, 1,"title",title, "xlabel","x", "ylabel",ylabel)

2731graph69.xpl

Figure: Example for manipulating a title and axes in a display.
\includegraphics[scale=0.6]{grfig69.ps}

First we have to tell 2747 setgopt to which display (di) and to which window of that display (1 ,1) it ought to apply the requested layout options. Then we specify the name of the option we want to modify and -- immediately following the name of the option -- the value we want that option to take on. For instance, we used the option name title and the option value "Plot of Sqrt(x)" to produce the headline of our display. Similarly, the option name "ylabel" followed by the option value "y = sqrt(x)" produced the label of the vertical axis.

Table 1 lists all the available options available for 2750 setgopt.


5.8 Axes Layout

In the following example we illustrate how the xlim/ylim, the xmajor/ymajor and the xoffset/yoffset options control the layout of the axes. Specifically, we create a four window display and use 2755 show to put the same data matrix into each window. Then we call to change the layout in each display. In the upper left window we use the default settings for the axes options and we use 2758 setgopt to create the headline "default". All other windows differ from this default window by altering a single option of 2761 setgopt. This way you can see the ``isolated'' effect of that option relative to the default settings:


  x  = 1:100

  y  = sin(x /20) +uniform(100, 1) /10

  d = createdisplay(2, 2)

  show(d, 1, 1, x~y)

  show(d, 1, 2, x~y)

  show(d, 2, 1, x~y)

  show(d, 2, 2, x~y)

  ;

  ; now we use setgopt to give the upper left window the 

  ; headline "default"

  ;

  setgopt(d, 1, 1, "title", "default")

  ;

  ; note that the title tells you exactly what we have 

  ; done in each of the other windows

  ;

  title12 = "ylim = (-4) | 4, xlim = 0 | 50"

  setgopt(d,1,2,"title",title12,"ylim",(-4)|4,"xlim",0|50)

  title21 = "ymajor = 0.3, xmajor = 15"

  setgopt(d,2,1,"title",title21,"ymajor",0.3, "xmajor",15)

  title22 = "yoffset = 13 | 13, xoffset = 20 | 20"

  setgopt(d,2,2,"title",title22,"yoffset",13|13,"xoffset",20|20)

2765graph6A.xpl

Figure: Example for manipulating plots in a display.
\includegraphics[scale=0.6]{grfig610.ps}


Table: Parameter of 2781 setgopt.
Option Description Option value
"title" change headline a string
"xlim" change limits of x-axis vector that contains two values, e.g. 0|10
"ylim" change limits of y-axis vector that contains two values, e.g. 0|10
"xoffset" change the width of axis border vector that contains two values to change the right and left widths of axis border (in %)
"yoffset" change the height of axis border vector that contains two values to change the upper and lower height of axis border (in %)
"xvalue" change the values m and k of the transformation m+k*x vector that contains one value for m and one value for k , e.g. 0|1 for 0+1*x
"yvalue" change the values m and k of m+k*y vector that contains one value for m and one value for k , e.g. 0|1 for 0+1*y
"xorigin" change origin for tickmark of x-axis scalar
"yorigin" change origin for tickmark of y-axis scalar
"xmajor" change major for tickmark of x-axis scalar
"ymajor" change major for tickmark of y-axis scalar
"xlabel" change label of x-axis a string
"ylabel" change label of y-axis a string
"rotpoint" change rotation point a rotation point vector
"rotcos" change rotation matrix a symmetric orthogonal matrix
"scal" change scale matrix a diagonal scale matrix
"transl" change translation vector a translation vector



Method and Data Technologies   MD*TECH Method and Data Technologies
  http://www.mdtech.de  mdtech@mdtech.de