|
The quantlet
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 B.4).
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
We can plot mathematical functions, e.g. the sine.
First we create an equidistant
grid from 0 to 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
x~y
, is a matrix composed of
two vectors. The object x in the first example
is a
We continue with plotting three functions
,
and
.
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
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
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
Even now, it is not easy to recognize the underlying functions.
We can connect the data points with the quantlet
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
Let us now color the lines to distinguish them better.
We again use the quantlets
setmask
and
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
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
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) |
Then we create six sine curves with different frequencies. With the
command
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
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)
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.