|
The displays created in the previous examples used
either the default settings for
displaying the data points, or were modified via
setmask
. However,
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
show
itself, but rather by specific commands
that have to preceed
show
. These commands are
setmaskp
,
setmaskl
and
setmaskt
.
They are merged in the quantlet
setmask
.
To control the color, the graphical representation (symbol)
and the size of each data point, you must use
setmaskp
,
which must preceed
show
. That is, first you call
setmaskp
to specify color, graphical representation
and size of the data you want to plot. Then you call
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
You can replace setmaskp(data, 4, 3, 8) by
data = setmask(data, "red", "medium", "circle")
Note two remarks
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
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
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
To achieve the same result using
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)
Similarly, you may represent each data point by the same
graphical symbol (in which case the third argument of
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
setmaskp
has to be a column
vector of integers with the same number of rows as the data matrix).
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 data points, a triangle to the
next
data points,
a cross to the following
data points, and a rhombus to the last
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)
Again, we give the equivalent code using
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)
Similar to the way
setmaskp
handles color and graphical
representation, you can either give the
same size to all data points (fourth argument of
setmaskp
is a scalar integer)
or assign different sizes to different data points
(fourth argument of
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
.
You can
choose any integer among
, ...,
with
representing the smallest and
representing the largest possible size.
Here is an example that assigns (through the vector
size) a rather small size ()
to the first
data points and the maximum size
(
) to the last
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)
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)
Suppose you want to plot a data matrix and
connect some or all of the points by lines. Then you have to use
setmaskl
to specify
Use
setmaskl
first to specify these options,
then use
show
to actually generate the plot.
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
Again,
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
setmaskl
in general)
is the specification of the vector that
tells
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:
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~2will 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~6will connect the 1st and the 3rd points, then interrupt the line and continue it by connecting the 5th and the 6th points.
Does
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
The last five lines can be replaced by the following
code which uses
setmask
instead of
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.
To label the data points, you have to use
setmaskt
.
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
setmaskt
as an input. Color, position and size are
controlled by the following
integers:
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)
The equivalent code using
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
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)
Using
setmask
instead of
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)
The layout of the display is controlled by
setgopt
.
In order to work,
setgopt
has to be called
after you have called
show
.
Here is an example that uses three of
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)
First we have to tell
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
setgopt
.
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
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
setgopt
to create
the headline "default".
All other windows differ from this default window by altering a
single option of
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)
|