1. Basic Plotting


1487 plot (x1 {, x2 {, ... {x5}}})
plots the data sets x1, ..., x5
1490 line (x1 {, x2 {, ... {x5}}})
plots the lines sets x1, ..., x5
y = 1493 setmask (x, opt1 {, opt2 {, ... {opt9}}})
modifies a data set for plotting
disp = 1496 createdisplay (r,c)
creates a display disp
1499 show (disp, i, j, x1 {, x2 {, ... {, xn}}})
plots the data sets x1, ..., xn in the display disp d


1.1 Plotting a Data Set

The quantlet 1504 plot plots up to five bivariate data sets in a scatter plot. Let us load a data set, the Boston Housing data file bostonh.dat (see Data Sets). It consists of 14 variables and we select the last two (percentage of lower status people, LSTAT, and median house price for owner occupied homes, MEDV) which are the 13th and 14th columns of the data set. Each observation represents one school district in the Boston metropolitan area.

  library ("plot")               ; loads library plot
  data = read ("bostonh")        ; reads Boston Housing data
  x = data[,13:14]               ; selects columns 13 and 14
  plot(x)                        ; plots data set
1512graph21.xpl

The plot in Figure 1 indicates a decreasing relationship between both variables.

Figure: Plot of the 13th (LSTAT) and 14th variable (MEDV) of the Boston Housing data.
\includegraphics[scale=0.6]{grfig21.ps}


1.2 Plotting a Function

We can plot mathematical functions, e.g. the sine. First we create an equidistant grid from 0 to $2\pi$ with 100 grid points.

  library("plot")                ; loads library plot
  xmin = 0                       ; grid minimum
  xmax = 2*pi                    ; grid maximum
  n    = 100                     ; number of grid points
  x = xmin + (xmax-xmin)/(n-1) .* (0:n-1)  ; generates grid  
  y = sin(x)                     ; computes sin(x)
  plot(x~y)                      ; plots data set
1531graph22.xpl

Note that both x and y are vectors. The input for 1536 plot, x~y, is a matrix composed of two vectors. The object x in the first example is a 506 x 2 matrix.

Figure: Plot of the sine curve in $[0,2\pi ]$.
\includegraphics[scale=0.6]{grfig22.ps}


1.3 Plotting Several Functions

We continue with plotting three functions $\sin(x)$, $\sin(3x)$ and $\sin(6x)$. Our last program has to be modified to

  library("plot")                ; loads library plot
  xmin = 0                       ; grid minimum 
  xmax = 2*pi                    ; grid maximum
  n    = 100                     ; number of grid points
  x  = xmin + (xmax-xmin)/(n-1) .* (0:n-1)  ; generates grid
  y1 = sin(x)                    ; computes sin(x)
  y2 = sin(3.*x)                 ; computes sin(3x)
  y3 = sin(6.*x)                 ; computes sin(6x)
  plot(x~y1, x~y2, x~y3)         ; plots data sets
1553graph23.xpl

Figure: Plot of three sine curves in $[0,2\pi ]$.
\includegraphics[scale=0.6]{grfig23.ps}


1.4 Coloring Data Sets

Since by default the points are shown as circles it is difficult to distinguish the three functions. We may easily color them with the quantlet 1571 setmask.

  library("plot")                ; loads library plot
  xmin = 0                       ; grid minimum 
  xmax = 2*pi                    ; grid maximum
  n  = 100                       ; number of grid points
  x  = xmin + (xmax-xmin)/(n-1) .* (0:n-1)  ; generates grid
  y1 = sin(x)                    ; computes sin(x)
  y2 = sin(3.*x)                 ; computes sin(3x)
  y3 = sin(6.*x)                 ; computes sin(6x)
  z1 = setmask(x~y1, "red")      ; colors sin(x) red
  z2 = setmask(x~y2, "green")    ; colors sin(x) green
  z3 = setmask(x~y3, "blue")     ; colors sin(x) blue
  plot(z1, z2, z3)               ; plots data sets
1575graph24.xpl

Figure: Plot of three colored sine curves in $[0,2\pi ]$.
\includegraphics[scale=0.6]{grfig24.ps}


1.5 Plotting Lines from Data Sets

Even now, it is not easy to recognize the underlying functions. We can connect the data points with the quantlet 1593 line instead of coloring the data points.

  library("plot")                ; loads library plot
  xmin = 0                       ; grid minimum 
  xmax = 2*pi                    ; grid maximum
  n    = 100                     ; number of grid points
  x  = xmin + (xmax-xmin)/(n-1) .* (0:n-1)  ; generates grid
  y1 = sin(x)                    ; computes sin(x)
  y2 = sin(3.*x)                 ; computes sin(3x)
  y3 = sin(6.*x)                 ; computes sin(6x)
  line(x~y1, x~y2, x~y3)         ; connects data points
1597graph25.xpl

Figure: Plot of three sine curves as lines in $[0,2\pi ]$.
\includegraphics[scale=0.6]{grfig25.ps}

Let us now color the lines to distinguish them better. We again use the quantlets 1613 setmask and 1616 plot.

  library("plot")                ; loads library plot
  xmin = 0                       ; grid minimum 
  xmax = 2*pi                    ; grid maximum
  n    = 100                     ; number of grid points
  x  = xmin + (xmax-xmin)/(n-1) .* (0:n-1)  ; generates grid
  y1 = sin(x)                    ; computes sin(x)
  y2 = sin(3.*x)                 ; computes sin(3x)
  y3 = sin(6.*x)                 ; computes sin(6x)
  plot(x~y1, x~y2, x~y3)         ; plots the data sets
  z1 = setmask(x~y1, "line", "red")   ; red line for sin(x)
  z2 = setmask(x~y2, "line", "green") ; green line for sin(3x)
  z3 = setmask(x~y3, "line", "blue")  ; blue line for sin(6x)
  plot(z1, z2, z3)               ; plots lines
1620graph26.xpl

Figure: Plot of three colored sine curves as lines in $[0,2\pi ]$.
\includegraphics[scale=0.6]{grfig26.ps}


1.6 Several Plots

Sometimes we want to compare several plots of data sets with each other; e.g. imagine a scatter-plot matrix. In most graphical user interfaces, we have windows available. However, we do not want to compose a scatter-plot matrix from a set of single windows. Since we are allowed to move the windows around, we would be able to hide one plot of a scatter-plot matrix. Thus we allow subwindows in a window. In our terminology, a window is called a display and the nonoverlapping subwindows are called graphics ports or plots.

In one of our last examples we plotted the points for three different sine curves. Let us now modify our example such that we plot each of the curves in a single port.

First we need to create a display which consists of several windows. The command 1638 createdisplay in the example below creates a display with 2 plots vertically and 3 plots horizontally.

(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
To identify our display among several displays we need a unique name. The assignment of the result of createdisplay(2,3) to disp identifies our display uniquely.

Then we create six sine curves with different frequencies. With the command 1641 show, we show the sine curves in the different plots of the display disp. Since we may have more than one display, the first parameter of 1644 show is the display name disp, the next two parameters describe which plot of the display is used for plotting, and the following parameter(s) are the data sets which will be plotted in the plot.

  disp = createdisplay(2,3)      ; display with 6 windows
  xmin = 0                       ; grid minimum 
  xmax = 2*pi                    ; grid maximum
  n    = 100                     ; number of grid points
  x  = xmin + (xmax-xmin)/(n-1) .* (0:n-1)  ; generates grid
  y1 = sin(x)                    ; computes sin(x)
  y2 = sin(2.*x)                 ; computes sin(2x)
  y3 = sin(3.*x)                 ; computes sin(3x)
  y4 = sin(4.*x)                 ; computes sin(4x)
  y5 = sin(5.*x)                 ; computes sin(5x)
  y6 = sin(6.*x)                 ; computes sin(6x)
  show (disp, 1, 1, x~y1)        ; shows sine curve x~sin(x)
  show (disp, 1, 2, x~y2)        ; shows sine curve x~sin(2x)
  show (disp, 1, 3, x~y3)        ; shows sine curve x~sin(3x)
  show (disp, 2, 1, x~y4)        ; shows sine curve x~sin(4x)
  show (disp, 2, 2, x~y5)        ; shows sine curve x~sin(5x)
  show (disp, 2, 3, x~y6)        ; shows sine curve x~sin(6x)
1648graph27.xpl

Figure: Plot of six sine curves, each in its own plot, in $[0,2\pi ]$.
\includegraphics[scale=0.6]{grfig27.ps}

It seems that we used a completely different technique from before to generate our plots. However, you may already guess that plot(x) which we have used before consists mainly of

  d = createdisplay(1,1)
  show(d,1,1,x)
Note that in contrast to all other variables in XploRe, displays are always global variables. This is because displays should survive the execution end of a quantlet.



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