(一)、有時候數據太多太集中,散點圖上的信息不容易看出來,例如:
> x <- rnorm(10000, 0, 1)
> y <- rnorm(10000, 1, 100)
> plot (x, y)
(二)、這個最好借助於二維的密度估計來認識圖形。。首先使用MASS程序包中的二維核密度函數kde2d()來估計二維數據的密度函數,再利用函數contour()畫出密度的等高線曲線圖。
> library(MASS)
> z <- kde2d(x,y)
> plot (x, y)
> contour(z, col = 'red', drawlabel=FALSE, main="Density estimation :cont Plot")
(三)、三維透視圖亦可以很清晰的表示。
> persp(z, main = "Density estimation:perspective plot")
(四)、當直接用原來的數據有時候很難得到有意義的圖形事,可以對數值進行變換以得到意義的圖形,最常用的是對數變換、指數變換和更為一般的Box-Cox變換:
我們用程序包MASS中的數據集Animal來舉例:
> library(MASS)
> data(Animals)
> Animals
body brain
Mountain beaver 1.350 8.1
Cow 465.000 423.0
Grey wolf 36.330 119.5
Goat 27.660 115.0
Guinea pig 1.040 5.5
Dipliodocus 11700.000 50.0
Asian elephant 2547.000 4603.0
Donkey 187.100 419.0
。。。。。
Mole 0.122 3.0
Pig 192.000 180.0
> par(mfrow=c(1,2))
> plot(brain~ body, data=Animals)
> plot(log(brain)~ log(body), data=Animals)
——————————————————————————————————————————————————
【1】Display Contours
Description
(http://127.0.0.1:29685/library/graphics/html/contour.html)
Create a contour plot, or add contour lines to an existing plot.
Usage
contour(x, ...) ## Default S3 method: contour(x = seq(0, 1, length.out = nrow(z)), y = seq(0, 1, length.out = ncol(z)), z, nlevels = 10, levels = pretty(zlim, nlevels), labels = NULL, xlim = range(x, finite = TRUE), ylim = range(y, finite = TRUE), zlim = range(z, finite = TRUE), labcex = 0.6, drawlabels = TRUE, method = "flattest", vfont, axes = TRUE, frame.plot = axes, col = par("fg"), lty = par("lty"), lwd = par("lwd"), add = FALSE, ...)
Arguments
x,y |
locations of grid lines at which the values in |
z |
a matrix containing the values to be plotted ( |
nlevels |
number of contour levels desired iff |
levels |
numeric vector of levels at which to draw contour lines. |
labels |
a vector giving the labels for the contour lines. If |
labcex |
|
drawlabels |
logical. Contours are labelled if |
method |
character string specifying where the labels will be located. Possible values are |
vfont |
if |
xlim, ylim, zlim |
x-, y- and z-limits for the plot. |
axes, frame.plot |
logical indicating whether axes or a box should be drawn, see |
col |
color for the lines drawn. |
lty |
line type for the lines drawn. |
lwd |
line width for the lines drawn. |
add |
logical. If |
... |
additional arguments to |
【2】Perspective Plots
Description
(http://127.0.0.1:29685/library/graphics/html/persp.html)
This function draws perspective plots of a surface over the x–y plane. persp
is a generic function.
Usage
persp(x, ...) ## Default S3 method: persp(x = seq(0, 1, length.out = nrow(z)), y = seq(0, 1, length.out = ncol(z)), z, xlim = range(x), ylim = range(y), zlim = range(z, na.rm = TRUE), xlab = NULL, ylab = NULL, zlab = NULL, main = NULL, sub = NULL, theta = 0, phi = 15, r = sqrt(3), d = 1, scale = TRUE, expand = 1, col = "white", border = NULL, ltheta = -135, lphi = 0, shade = NA, box = TRUE, axes = TRUE, nticks = 5, ticktype = "simple", ...)
Arguments
x, y |
locations of grid lines at which the values in |
z |
a matrix containing the values to be plotted ( |
xlim, ylim, zlim |
x-, y- and z-limits. These should be chosen to cover the range of values of the surface: see ‘Details’. |
xlab, ylab, zlab |
titles for the axes. N.B. These must be character strings; expressions are not accepted. Numbers will be coerced to character strings. |
main, sub |
main and sub title, as for |
theta, phi |
angles defining the viewing direction. |
r |
the distance of the eyepoint from the centre of the plotting box. |
d |
a value which can be used to vary the strength of the perspective transformation. Values of |
scale |
before viewing the x, y and z coordinates of the points defining the surface are transformed to the interval [0,1]. If |
expand |
a expansion factor applied to the |
col |
the color(s) of the surface facets. Transparent colours are ignored. This is recycled to the (nx-1)(ny-1) facets. |
border |
the color of the line drawn around the surface facets. The default, |
ltheta, lphi |
if finite values are specified for |
shade |
the shade at a surface facet is computed as |
box |
should the bounding box for the surface be displayed. The default is |
axes |
should ticks and labels be added to the box. The default is |
ticktype |
character: |
nticks |
the (approximate) number of tick marks to draw on the axes. Has no effect if |
... |
additional graphical parameters (see |