Plot3d: Plot3d generates a 3-dimensional plot using the package...

Description Usage Arguments Examples

View source: R/Plot3d.R

Description

Plot3d generates a 3-dimensional plot of points using the package rgl. A grid of points for the x and z variables and an optional factor variable is kept as an object in order to allowing plotting fitted surfaces that are functions of the x and z variables and the optional factor.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
## S3 method for class 'formula'

Plot3d(formula = attr(data, "formula"),
    data = sys.parent(),
    groups = NULL,
    subset = NULL,
    xlab = names(dd)[2],
    ylab = names(dd)[1],
    zlab = names(dd)[3],
    verbose = 0,
    col = c("blue", "green", "orange", "magenta", "cyan", "red", "yellow", "gray"),
    surface = FALSE,
    fit = "smooth",
    surface.col = col,
    lines.col = "gray",
    lines.lwd = 1,
    ...)

## S3 method for class 'lm'

  Plot3d(fit, ...)

## S3 default method

  Plot3d(x, y, z, xlab, ylab, zlab, groups = NULL, ...)

Arguments

formula

a two sided formula of the form y ~ x + z | [g] where y is the variable plotted on the vertical axis, x is plotted on the horizontal axis and z is plotted along the axis coming out of the screen. g is interpreted as a factor and the index of its levels is used to select the value of col used.

data

a data frame in which formula is evaluated.

groups

a variable used to join points belonging to the same group by lines

subset

is a condition to select a subset of data.

xlab, ylab, zlab

labels for respective axes

verbose
col
surface
fit
surface.col
lines.col
lines.lwd
...

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
## Not run: 
    library(p3d)
    data(Smoking)
    head(Smoking)
    rownames(Smoking) = Smoking$Country

    Init3d(family = 'serif', cex = 1.5)
    Plot3d( LE ~ CigCon + HealthExpPC | Continent, Smoking)
    Axes3d()
    Identify3d(pad=1)


    fit = lm( LE ~ CigCon + log(HealthExpPC) +I(CigCon^2) + I(log(HealthExpPC)^2) + I(CigCon*log(HealthExpPC)), Smoking)
    Fit3d( fit )
    fitl <- lm( LE ~ CigCon + HealthExpPC, Smoking)
    Fit3d( fitl, col = 'pink')
# HEpCap is highly 'skewed': dense on left, long tail on right
    require( lattice )
    densityplot( Smoking$HealthExpPC )

# Useful to use a transformation to make spread more even
#  e.g. log
# First make sure all values are positive:

    sort( Smoking$HealthExpPC)

# Do log transformation:

    Smoking$LogHE <- log(Smoking$HealthExpPC)    # create log HE

    densityplot( Smoking$LogHE )

# Also usefult to have categories:

    Smoking$HECat <- cut(Smoking$LogHE, 7)       # create categories
    summary(Smoking)

    Plot3d( LE ~ CigCon + LogHE |HECat, Smoking )  # condition on level of HEpC
    Axes3d()
    Ell3d()
    Identify3d(pad=1)

# Simple regression

    fit.lin <- lm( LE ~ CigCon, Smoking)
    Fit3d( fit.lin )
    fit.lin2 <- lm(LE ~ CigCon, subset(Smoking, Continent != "Africa"))
    Fit3d( fit.lin2)
    Pop3d(4)

# Use multiple regression (advanced version with quadratic surface)

    fit = lm( LE ~ CigCon + I(LogHE^2) +I(CigCon^2) + I(LogHE^2) + I(CigCon*LogHE), Smoking)
    Fit3d( fit, col = 'red' )
    Pop3d(2)

# refit omitting Africa:

    fit.na = lm( LE ~ CigCon + I(LogHE^2) +I(CigCon^2) + I(LogHE^2)
               + I(CigCon*LogHE), Smoking, subset = Continent != "Africa")
    Fit3d( fit.na, col = 'red' )

# Marginal relationship

    Pop3d(4)    # pop twice for each fit
    fit.quad <- lm( LE ~ CigCon + I(CigCon^2) , Smoking)
    Fit3d( fit.quad, col = 'green')

#  A quadratic surface within each Continent (overfitting?!)

    fit2 = lm( LE ~ Continent*(CigCon + HealthExpPC +I(CigCon^2) +
            I(HealthExpPC^2) + I(CigCon*HealthExpPC)), Smoking)
    Fit3d( fit2 )

#  For a 2D view of the y versus x

    view3d(0,0,0)   # note the first parameter, theta, rotates clockwise in the vertical axis
                    # from the initial position given by view3d(0,0,0); the second
                    # parameter, phi, tilts the graph around a horizontal screen axis
                    # towards the viewer.  The third parameter, fov, affects the
                    # amount of 'perspective'. fov = 0 yields an orthographic
                    # projection

## End(Not run)

p3d documentation built on May 2, 2019, 5:25 p.m.