plotdev: Handling and plotting plotting lists.

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/plot.struct.R

Description

S3 method plot.plist and function plotdev plot the plotting list to the current device. Changes can be made to the perspective view, to the lighting and shading, or to make colors transparent.

getplist and setplist retrieve and store information in the plotting list.

selectplist selects parts from the plotting list, based on a user-defined function.

Usage

1
2
3
4
5
6
getplist()
setplist(plist)
plotdev(...)
## S3 method for class 'plist'
 plot(x, ...) 
selectplist(plist, SS)

Arguments

x, plist

The plotting list as generated (invisibly) by any of the 3D plotting functions.

SS

Function which tests points for inclusion in the plotting list. It should take as argument three vectors (x, y, z) and return a vector of equal length that is either TRUE or FALSE, denoting whether the point should be selected or not.

...

Additional arguments to change the view or coloration. Supported arguments to change the view are : theta, phi, xlim, ylim, zlim, d, r, scale, expand. See perspbox, persp.

Supported arguments to change the lighting, or coloration are : ltheta, lphi, shade, lighting. See jet.col.

Details

All 3-D functions from package plot3D produce or update a plotting list that is local to the package. One can access this plotting list via getplist and setplist. The list is used to plot when, in a 3-D function, the argument plot is TRUE or via function plotdev.

When new 3-D objects are added to a plot, using the add argument of the plotting functions, then everything except the axes, is redrawn on top of what was already there. This means that several object will be drawn multiple times, and this may clutter the output. This may not be visible on your screen, but it may become apparent when exported. Use plotdev to create clean figures, where every object is drawn only once.

The plotting list can contain the following items:

For the item poly the elements are:

Value

Returns the updated plotting list.

Note

Once a 3D plot has been generated, a new device can be opened and plotdev used to plot also on this device.

plotdev and plot(getplist()) are the same.

In an extension package, plot3Drgl, a similar function, plotrgl, plots the graphs to the device opened with rgl. This allows interactive zooming, rotating, etc...

Author(s)

Karline Soetaert <karline.soetaert@nioz.nl>

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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# save plotting parameters                            
 pm   <- par("mfrow")
 pmar <- par("mar")

## ========================================================================
## The volcano
## ========================================================================

 par(mfrow = c(2, 2), mar = c(2, 2, 2, 2))

# The volcano at lower resolution
 x <- seq(1, nrow(volcano), by = 2)
 y <- seq(1, ncol(volcano), by = 2)
 V <- volcano[x,y]

 persp3D(z = V)

# rotate
 plotdev(theta = 0)

# light and transparence
 plotdev(lighting  = TRUE, lphi = 90, alpha = 0.6)  

# zoom
 plotdev(xlim = c(0.2, 0.6), ylim = c(0.2, 0.6), phi = 60) 
 
## ========================================================================
## Two spheres 
## ========================================================================

 par(mfrow = c(1, 1), mar = c(0, 0, 0, 0))

# create a sphere
 M  <- mesh(seq(0, 2*pi, length.out = 30),
            seq(0,   pi, length.out = 30))
 u  <- M$x ; v  <- M$y

 x <- cos(u)*sin(v)
 y <- sin(u)*sin(v)
 z <- cos(v)

 surf3D(x = 2*x, y = 2*y, z = 2*z, 
        colvar = NULL, lighting = TRUE, #plot = FALSE,
        facets = NA, col = "blue", lwd = 5)
 
 surf3D(x, y, z, colvar = NULL, lighting = TRUE, 
        col = "red", add = TRUE)

 names(getplist())

# plot with different view:
 plotdev(phi = 0)  
## Not run:   # will plot same 3-D graph to pdf
 pdf(file = "save.pdf")
 plotdev()
 dev.off()

## End(Not run)
             
## ========================================================================
## Two spheres and two planes 
## ========================================================================

 par(mar = c(2, 2, 2, 2))

# equation of a sphere
 M  <- mesh(seq(0, 2*pi, length.out = 100),                                     -
            seq(0,   pi, length.out = 100))
 u  <- M$x ; v  <- M$y

 x <- cos(u)*sin(v)
 y <- sin(u)*sin(v)
 z <- cos(v)

 surf3D(x, y, z, colvar = z, 
        theta = 45, phi = 20, bty = "b",
        xlim = c(-1.5, 1.5), ylim = c(-1, 2), 
        zlim = c(-1.5, 1.5), plot = FALSE)

# add a second sphere, shifted 1 unit to the right on y-axis; 
# no facets drawn for this sphere 
 surf3D (x, y+1, z, colvar = z, add = TRUE, 
         facets = FALSE, plot = FALSE)

# define a plane at z = 0
 Nx <- 100
 Ny <- 100
  
 x <- seq(-1.5, 1.5, length.out = Nx)
 y <- seq(-1, 2, length.out = Ny)

 image3D (x = x, y = y, z = 0, add = TRUE, colvar = NULL, 
          col = "blue", facets = TRUE, plot = FALSE)

# another, small plane at y = 0 - here x and y have to be matrices!
 x <- seq(-1., 1., length.out = 50)
 z <- seq(-1., 1., length.out = 50)
 
 image3D (x = x, y = 0, z = z, colvar = NULL, 
         add = TRUE, col = NA, border = "blue", 
         facets = TRUE, plot = TRUE)       

## Not run:   # rotate 
 for (angle in seq(0, 360, by = 10)) 
   plotdev(theta = angle)

## End(Not run)

## ========================================================================
## Zooming, rescaling, lighting,...
## ========================================================================

 par(mfrow = c(2, 2)) 

# The volcano
 x <- seq(1, nrow(volcano), by = 2)
 y <- seq(1, ncol(volcano), by = 2)
 V <- volcano[x,y]
# plot the volcano
 persp3D (x, y, z = V, colvar = V, theta = 10, phi = 20, 
          box = FALSE, scale = FALSE, expand = 0.3, 
          clim = range(V), plot = FALSE)

# add a plane (image) at z = 170; jetcolored, transparant: only border
 image3D(x, y, z = 170, add = TRUE, clim = range(V), 
         colvar = V, facets = NA, plot = FALSE, colkey = FALSE)

# add a contour (image) at z = 170; jetcolored, 
 contour3D(x, y, z = 170, add = TRUE, clim = range(V),
           colvar = V, plot = FALSE, colkey = FALSE)

# plot it  - 
 plot(getplist())   #  same as plotdev()

# plot but with different expansion
 plotdev(expand = 1)

# other perspective, and shading
 plotdev(d = 2, r = 10, shade = 0.3)
    
# zoom and rotate
 plotdev(xlim = c(10, 30), ylim = c(20, 30), phi = 50)

## ========================================================================
## Using setplist
## ========================================================================

 polygon3D(runif(3), runif(3), runif(3))
# retrieve plotting list
 plist <- getplist()
 names(plist)
 plist$poly
# change copy of plotting list
 plist$poly$col <- "red"
# update internal plotting list
 setplist(plist)
# plot updated list
 plotdev()
 

## ========================================================================
## Using selectplist
## ========================================================================

 polygon3D(runif(10), runif(10), runif(10), col = "red", 
   alpha = 0.2, plot = FALSE, ticktype = "detailed", 
   xlim = c(0,1), ylim = c(0, 1), zlim = c(0, 1))
 polygon3D(runif(10)*0.5, runif(10), runif(10), col = "yellow", 
   alpha = 0.2, plot = FALSE, add = TRUE)
 polygon3D(runif(10)*0.5+0.5, runif(10), runif(10), col = "green", 
   alpha = 0.2, plot = FALSE, add = TRUE)
 points3D(runif(10), runif(10), runif(10), col = "blue", 
   add = TRUE, plot = FALSE)
 segments3D(x0 = runif(10), y0 = runif(10), z0 = runif(10), 
   x1 = runif(10), y1 = runif(10), z1 = runif(10), 
   colvar = 1:10, add = TRUE, lwd = 3)

# retrieve plotting list
 plist <- getplist()

# selection function 
 SS <- function (x, y, z)  {
   sel <- rep(TRUE, length.out = length(x))
   sel[x < 0.5] <- FALSE
   return(sel)
 } 
# The whole polygon will be removed or kept.  
 plot(x = selectplist(plist, SS), 
   xlim = c(0, 1), ylim = c(0, 1), zlim = c(0, 1))

# restore plotting parameters
 par(mfrow = pm)
 par(mar = pmar)

Example output

 [1] "type"     "plt"      "persp"    "alpha"    "setlim"   "xlim"    
 [7] "ylim"     "zlim"     "scalefac" "mat"      "dot"      "imgnr"   
[13] "img"      "poly"    
png 
  2 
 [1] "type"     "plt"      "persp"    "alpha"    "setlim"   "xlim"    
 [7] "ylim"     "zlim"     "scalefac" "mat"      "dot"      "imgnr"   
[13] "img"      "poly"    
$x
          [,1]
[1,] 0.4928888
[2,] 0.3282655
[3,] 0.8370422
[4,]        NA

$y
          [,1]
[1,] 0.8664731
[2,] 0.1330346
[3,] 0.5489654
[4,]        NA

$z
          [,1]
[1,] 0.7481299
[2,] 0.8798684
[3,] 0.0974498
[4,]        NA

$col
[1] "grey"

$border
[1] NA

$lwd
[1] 1

$lty
[1] 1

$alpha
[1] NA

$isimg
[1] 0

$proj
[1] -4.619123

attr(,"class")
[1] "poly"

plot3D documentation built on May 22, 2021, 5:06 p.m.

Related to plotdev in plot3D...