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

转载于:https://www.cnblogs.com/bangemantou/archive/2012/12/30/2839713.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值