(一)、有时候数据太多太集中,散点图上的信息不容易看出来,例如:
> 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
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
| locations of grid lines at which the values in |
| a matrix containing the values to be plotted ( |
| number of contour levels desired iff |
| numeric vector of levels at which to draw contour lines. |
| a vector giving the labels for the contour lines. If |
|
|
| logical. Contours are labelled if |
| character string specifying where the labels will be located. Possible values are |
| if |
| x-, y- and z-limits for the plot. |
| logical indicating whether axes or a box should be drawn, see |
| color for the lines drawn. |
| line type for the lines drawn. |
| line width for the lines drawn. |
| logical. If |
| additional arguments to |
【2】Perspective Plots
Description
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
| locations of grid lines at which the values in |
| a matrix containing the values to be plotted ( |
| x-, y- and z-limits. These should be chosen to cover the range of values of the surface: see ‘Details’. |
| titles for the axes. N.B. These must be character strings; expressions are not accepted. Numbers will be coerced to character strings. |
| main and sub title, as for |
| angles defining the viewing direction. |
| the distance of the eyepoint from the centre of the plotting box. |
| a value which can be used to vary the strength of the perspective transformation. Values of |
| before viewing the x, y and z coordinates of the points defining the surface are transformed to the interval [0,1]. If |
| a expansion factor applied to the |
| the color(s) of the surface facets. Transparent colours are ignored. This is recycled to the (nx-1)(ny-1) facets. |
| the color of the line drawn around the surface facets. The default, |
| if finite values are specified for |
| the shade at a surface facet is computed as |
| should the bounding box for the surface be displayed. The default is |
| should ticks and labels be added to the box. The default is |
| character: |
| the (approximate) number of tick marks to draw on the axes. Has no effect if |
| additional graphical parameters (see |