3.4 Advanced Graphics


7255 grmove (grin, shf)
moves a graphical object by shf
7258 grrot (grin, rot)
rotates a graphical object by rot times 90 degree rotations
7261 grxline (x, v {, col})
generates a vertical line at v
7264 gryline (y, v {, col})
generates a horizontal line at v
7267 grcircle (radius {, col})
generates a circle or ellipse. The circle is centered at (0,0) and has the given radius.
7270 hls2rgb (hls)
generates RGB colors from the HLS color model
7273 rgb2hls (rgb)
generates HLS colors from the RGB color model
7276 createcolor (rgb)
sets a palette of colors


3.4.1 Moving and Rotating

The quantlets 7337 grmove and 7340 grrot allow the user to move graphic primitives without losing information including lines or line styles. 7343 grrot knows 4 counterclockwise rotations: 0 no rotation, 1 rotation by 90 degree, 2 rotation by 180 degree and 3 rotation by 270 degree. Since the rotation center is always (0,0), you should rotate first and then move the graphic primitive.

  library("plot")                ; loads library plot
  data = read ("bostonh")        ; reads Boston Housing data
  gro1 = grbox (data[,11])       ; boxplot of 11th variable
  gro2 = grbox (data[,13])       ; boxplot of 13th variable
  gro3 = grbox (data[,14])       ; boxplot of 14th variable
  gro1 = grrot (gro1, 1)         ; rotates first boxplot
  gro2 = grrot (gro2, 1)         ; rotates second boxplot
  gro2 = grmove (gro2, #(1.5,0)) ; moves second boxplot 1.5 right
  gro3 = grrot (gro3, 1)         ; rotates the third boxplot
  gro3 = grmove (gro3, #(3,0))   ; moves third boxplot 3.0 right
  plot(gro1, gro2, gro3)         ; shows the boxplots
7347 XLGgraph51.xpl

Figure: Parallel boxplots of different variables (PTRATIO, LSTAT, MEDV) of the Boston Housing data.
\includegraphics[scale=0.425]{grfig51}

We see that all three variables, pupil-teacher ratio (PTRATIO), percentage lower status people (LSTAT) and median house prices (MEDV), are skewed, especially the last one (median house prices).


3.4.2 Simple Predefined Graphic Primitives

For the convenience of the user, some simple graphic primitives are predefined: 7393 grxline which draws a line from $ (\min x_i, y)$ to $ (\max x_i, y)$, 7396 gryline which draws a line from $ (x, \min y_i)$ to $ (x, \max y_i)$, and 7399 grcircle which draws a circle.

Let's draw a two-dimensional standard normal distributed data set. Furthermore, we include the coordinate system axes as well as the circle which contains $ 95\%$ of the data of a bivariate standard normal distribution.

  library("graphic")        ; loads library graphic
  randomize(0)              ; initializes random generator
  x  = normal(200,2)        ; generates 200 bivariate data
  xl = grxline(0, x[,1])    ; computes horizontal axis
  yl = gryline(0, x[,2])    ; computes vertical axis
  cl = grcircle(2.44775)    ; computes circle with radius 2.44775
  d  = createdisplay(1,1)   ; creates display
  show(d,1,1,x,xl,yl,cl)    ; draws everything in one plot
7403 XLGgraph52.xpl

Figure: Coordinate axes, $ 95\%$ mass circle and random sample of a bivariate standard normal distribution.
\includegraphics[scale=0.425]{grfig52}

If we count the number of observations outside the circle we will find nine. Since we have generated $ 200$ points we would expect around ten ( $ 200 \times 0.05$) observations outside the circle.


3.4.3 Color Models

XploRe has eight standard colors ( 0 - black, 1 - blue, 2 - green, 3 - cyan, 4 - red, 5 - magenta, 6 - yellow and 7 - white ) which sit in the corners of the RGB cube. We support RGB colors with Red, Green and Blue ranging from 0 to 255. The number of available colors depends on your monitor and windows system.

However, the RGB system is not well suited to construct a path through the color space, e.g. for contour lines indicating maxima by red lines and minima by blue lines. The HLS color model is much better suited for that. Thus we provide the quantlets 7481 rgb2hls and 7484 hls2rgb to transform a specific color from a color model into another.

Note that if we want to use colors other than the standard ones then it's necessary to create the colors by 7487 createcolor , since your output device supports $ xx$-bit colors, but is not able to display them all simultaneously. Thus, we support a color palette of 256 colors (minus the 8 standard colors).

  library("plot")              ; loads the library plot
  h   = grid (120,26.6666,4)   ; creates grid in HLS double cone
  l   = 0.5.*matrix(rows(h))
  s   = matrix(rows(h))
  rgb = hls2rgb(h~l~s)         ; transfers HLS model to RGB model
  x0 = #(-3, -3)
  h  = #(0.2, 0.2)
  n  = #(31, 31)
  x  = grid(x0, h, n)          ; generates a bivariate grid
  f  = exp(-(x[,1]^2+x[,2]^2)/1.5)/(1.5*pi)  
                               ; computes density of bivariate 
                               ;    normal with correlation 0.5
  c  = 0.2*(1:4).*max(f)       ; contour lines as 10%,...,90% 
                               ;    times the maximum density
  createcolor(rgb)             ; generates the necessary colors
  gr = grcontour2(x~f, c, rgb) ; generates surface
  plot(gr)                     ; plots the surface
7491 XLGgraph53.xpl

Figure: Contour plot of the density of the standard bivariate normal distribution colored after a HLS color scheme.
\includegraphics[scale=0.425]{grfig53}

Note that the object rgb is really a $ 4\times 3$ matrix. The first vector represents the value for red, the second the value for blue and the last the value for green.