3.5 Graphic Commands


7592 setmaskp (data, color, layout, size)
influences the layout, size and color of data points
7595 setmaskl (data, lines, color, type, thickness)
influences the layout, size, type and color between data points
7598 setmaskt (data, labels, color, direction, size)
influences the appearance of text at the data points
7601 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 7604 setmask . However, 7607 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 7610 show itself, but rather by specific commands that have to preceed 7613 show . These commands are 7616 setmaskp , 7619 setmaskl and 7622 setmaskt . They are merged in the quantlet 7625 setmask .


3.5.1 Controlling Data Points

To control the color, the graphical representation (symbol) and the size of each data point, you must use 7695 setmaskp , which must preceed 7698 show . That is, first you call 7701 setmaskp to specify color, graphical representation and size of the data you want to plot. Then you call 7704 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
7708 XLGgraph61.xpl

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

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

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

Note two remarks


3.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 7800 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 7803 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
7807 XLGgraph62.xpl

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

To achieve the same result using 7819 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)


3.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 7903 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 7906 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)
7910 XLGgraph63.xpl

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

Again, we give the equivalent code using 7922 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)


3.5.4 Size of Data Points

Similar to the way 8015 setmaskp handles color and graphical representation, you can either give the same size to all data points (fourth argument of 8018 setmaskp is a scalar integer) or assign different sizes to different data points (fourth argument of 8021 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)
8025 XLGgraph64.xpl

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

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)


3.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 8159 setmaskl to specify

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

8168 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
8172 XLGgraph65.xpl

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

Again, 8184 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 8187 setmaskl in general) is the specification of the vector that tells 8190 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 8193 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
8197 XLGgraph66.xpl

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

The last five lines can be replaced by the following code which uses 8209 setmask instead of 8212 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.


3.5.6 Label of Data Points

To label the data points, you have to use 8392 setmaskt . 8395 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 8398 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)
8402 XLGgraph67.xpl

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

The equivalent code using 8414 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 8417 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)
8421 XLGgraph68.xpl

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

Using 8433 setmask instead of 8436 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)


3.5.7 Title and Axes Labels

The layout of the display is controlled by 8549 setgopt . In order to work, 8552 setgopt has to be called after you have called 8555 show .

Here is an example that uses three of 8558 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)
8562 XLGgraph69.xpl

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

First we have to tell 8574 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 3.1 lists all the available options available for 8577 setgopt .


3.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 8691 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 8694 setgopt to create the headline "default". All other windows differ from this default window by altering a single option of 8697 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)
8701 XLGgraph6A.xpl

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


Table: Parameter of 8713 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