R作圖 等高線、三維透視圖和數據變換圖


(一)、有時候數據太多太集中,散點圖上的信息不容易看出來,例如:

> 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 are measured. These must be in ascending order. By default, equally spaced values from 0 to 1 are used. If x is a list, its components x$x and x$y are used for x and y, respectively. If the list has component z this is used for z.

z

a matrix containing the values to be plotted (NAs are allowed). Note that x can be used instead of z for convenience.

nlevels

number of contour levels desired iff levels is not supplied.

levels

numeric vector of levels at which to draw contour lines.

labels

a vector giving the labels for the contour lines. If NULL then the levels are used as labels, otherwise this is coerced by as.character.

labcex

cex for contour labelling. This is an absolute size, not a multiple of par("cex").

drawlabels

logical. Contours are labelled if TRUE.

method

character string specifying where the labels will be located. Possible values are "simple", "edge" and "flattest" (the default). See the ‘Details’ section.

vfont

if NULL, the current font family and face are used for the contour labels. If a character vector of length 2 then Hershey vector fonts are used for the contour labels. The first element of the vector selects a typeface and the second element selects a fontindex (see text for more information). The default is NULL on graphics devices with high-quality rotation of text and c("sans serif", "plain") otherwise.

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 plot.default.

col

color for the lines drawn.

lty

line type for the lines drawn.

lwd

line width for the lines drawn.

add

logical. If TRUE, add to a current plot.

...

additional arguments to plot.window, title, Axis and box, typically graphical parameters such as cex.axis.

 

【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 are measured. These must be in ascending order. By default, equally spaced values from 0 to 1 are used. If x is a list, its components x$x and x$y are used for x and y, respectively.

z

a matrix containing the values to be plotted (NAs are allowed). Note that x can be used instead of z for convenience.

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 title.

theta, phi

angles defining the viewing direction. theta gives the azimuthal direction and phi the colatitude.

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 d greater than 1 will lessen the perspective effect and values less and 1 will exaggerate it.

scale

before viewing the x, y and z coordinates of the points defining the surface are transformed to the interval [0,1]. If scale is TRUE the x, y and z coordinates are transformed separately. If scale is FALSE the coordinates are scaled so that aspect ratios are retained. This is useful for rendering things like DEM information.

expand

a expansion factor applied to the z coordinates. Often used with 0 < expand < 1 to shrink the plotting box in the z direction.

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, NULL, corresponds to par("fg"). A value of NA will disable the drawing of borders: this is sometimes useful when the surface is shaded.

ltheta, lphi

if finite values are specified for ltheta and lphi, the surface is shaded as though it was being illuminated from the direction specified by azimuth ltheta and colatitude lphi.

shade

the shade at a surface facet is computed as ((1+d)/2)^shade, where d is the dot product of a unit vector normal to the facet and a unit vector in the direction of a light source. Values of shade close to one yield shading similar to a point light source model and values close to zero produce no shading. Values in the range 0.5 to 0.75 provide an approximation to daylight illumination.

box

should the bounding box for the surface be displayed. The default is TRUE.

axes

should ticks and labels be added to the box. The default is TRUE. If box is FALSE then no ticks or labels are drawn.

ticktype

character: "simple" draws just an arrow parallel to the axis to indicate direction of increase; "detailed" draws normal ticks as per 2D plots.

nticks

the (approximate) number of tick marks to draw on the axes. Has no effect if ticktype is "simple".

...

additional graphical parameters (see par).


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM