Générer des graphiques 3D - Cours Université de Sfa Marc Girondot Januar 24, 2013 Générer des données 3D > librar(fields) > essai <- function(, ) return (^2+^2+runif(1, min=1, ma=3)) > essai(1, 2) [1] 6.968858 > <- seq(from=-10, to=10, b=0.5) > <- seq(from=-10, to=10, b=0.5) > z <- outer(,, essai) > str(z) num [1:41, 1:41] 202 192 183 174 166... 1
Utilisation de image.plot La fonction image.plot est disponible dans le package fields. Draws image plot with a legend strip for the color scale based on either a regular grid or a grid of quadrilaterals. Description This function combines the R image function with some automatic placement of a legend. This is done b splitting the plotting region into two parts. Putting the image in one and the legend in the other. It also allows for plotting quadrilateral cells in the image format that often arise from regular grids transformed with a map projection. Usage image.plot(..., Arguments add = FALSE, nlevel = 64, horizontal = FALSE, legend.shrink = 0.9, legend.width = 1.2, legend.mar = ifelse(horizontal, 3.1, 5.1), legend.lab = NULL, graphics.reset = FALSE, bigplot = NULL, smallplot = NULL, legend.onl = FALSE, col = tim.colors(nlevel), lab.breaks = NULL, ais.args = NULL, legend.args = NULL, midpoint=false, border=na, lwd=1.0)... The usual arguments to the image function as,,or z or as a list with,,z as components. One can also include a breaks argument for an unequal color scale with color scale boundaries at the breaks (see eample below). If a quadrilatera l grid, arguments must be eplicitl, and z with, and being matrices of dimension equal or one more than z giving the grid locations. The basic concept is that the coordinates of and still define a grid but the image cells are general quadrilaterals rather than being restricted to rectangles. See details below as to how one handles whether the quads are specified b their vertices or b their midpoints. add If true add image and a legend strip to the eisting plot. nlevel Number of color levels used in legend strip legend.shrink 2
Amount to shrink the size of legend relative to the full height or width of the plot. legend.width Width in characters of the legend strip. Default is 1.2, a little bigger that the width of a character. legend.mar Width in characters of legend margin that has the ais. Default is 5.1 for a vertical legend and 3.1 for a horizontal legend. legend.lab Label for the ais of the color legend. Default is no label as this is usual evident from the plot title. graphics.reset If FALSE (default) the plotting region ( plt in par) will not be reset and one can add more information onto the image plot. (e.g. using functions such as points or lines.) If TRUE will reset plot parameters to the values before entering the function. horizontal If false (default) legend will be a vertical strip on the right side. If true the legend strip will be along the bottom. bigplot Plot coordinates for image plot. If not passed these will be determined within the function. smallplot Plot coordinates for legend strip. If not passed these will be determined within the function. Be sure to leave room for the ais labels. For eample, if the legend is on the right side smallplot= c(.85,.9,0,1) will leave (.1 in plot coordinates) for the ais labels to the right of the color strip. This argument is useful for drawing a plot with the legend that is the same size as the plots without legends. legend.onl If TRUE just add the legend to a the plot in the plot region defined b the coordinates in smallplot. In the absence of other information the range for the legend is determined from the zlim argument. col Color table to use for image ( see help file on image for details). Default is a pleasing range of 64 divisions suggested b Tim Hoar and is similar to the MATLAB (TM) jet color scheme. 3
lab.breaks If breaks are supplied these are tet string labels to put at each break value. This is intended to label ais on a transformed scale such as logs. ais.args Additional arguments for the ais function used to create the legend ais. (See eample below adding a log scaling.) legend.args Arguments for a complete specification of the legend label. This is in the form of list and is just passed to the mtet function. Usuall this will not be needed. (See eample below.) midpoint This option for the case of unequall spaced grids with and being matrices of grid point locations. If FALSE (default) the quadralaterals will be etended to surround the z locations as midpoints. If TRUE z values will be averaged to ield a midpoint value and the original grid points be used to define the quadralaterals. (See help on pol.image for details). In most cases midpoint should be FALSE to preserve eact values for z and let the grid polgons be modified. border This onl works if and are matrices - if NA the quadralaterals will have a border color that is the same as the interior color. Otherwise this specifies the color to use. lwd Line width of borders around piels. This might need to be set less than 1.0 to avoid visible rounding of the piel corners. 4
> image.plot(,, z, col=tim.colors(16)) 200 150 100 50 > image.plot(,, z, col=tim.colors(128)) 200 150 100 50 5
> image.plot(,, z, col=heat.colors(16)) 200 150 100 50 > image.plot(,, z, col=heat.colors(128)) 200 150 100 50 6
> image.plot(,, z, col=rainbow(16)) 200 150 100 50 > image.plot(,, z, col=rainbow(128)) 200 150 100 50 7
Utilisation de persp persp est disponible dans le package graphics. Perspective Plots Description This function draws perspective plots of a surface over the - plane. persp is a generic function. Usage persp(,...) ## Default S3 method: persp( = seq(0, 1, length.out = nrow(z)), = seq(0, 1, length.out = ncol(z)), z, lim = range(), lim = range(), zlim = range(z, na.rm = TRUE), lab = NULL, lab = NULL, zlab = NULL, main = NULL, sub = NULL, theta = 0, phi = 15, r = sqrt(3), d = 1, scale = TRUE, epand = 1, col = "white", border = NULL, ltheta = -135, lphi = 0, shade = NA, bo = TRUE, aes = TRUE, nticks = 5, ticktpe = "simple",...) Arguments, locations of grid lines at which the values in z are measured. These must be in ascending order. B default, equall spaced values from 0 to 1 are used. If is a list, its components $ and $ are used for and, respectivel. z a matri containing the values to be plotted (NAs are allowed). Note that can be used instead of z for convenience. lim, lim, zlim -, - and z-limits. These should be chosen to cover the range of values of the surface: see 'Details'. lab, lab, zlab titles for the aes. N.B. These must be character strings; epressions are not accepted. Numbers will be coerced to character strings. 8
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 eepoint from the centre of the plotting bo. d a value which can be used to var the strength of the perspective transformation. Values of d greater than 1 will lessen the perspective effect and values less and 1 will eaggerate it. scale before viewing the, and z coordinates of the points defining the surface are transformed to the interval [0,1]. If scale is TRUE the, and z coordinates are transformed separatel. If scale is FALSE the coordinates are scaled so that aspect ratios are retained. This is useful for rendering things like DEM information. epand a epansion factor applied to the z coordinates. Often used with 0 < epand < 1 to shrink the plotting bo in the z direction. col the color(s) of the surface facets. Transparent colours are ignored. This is reccled to the (n-1)(n-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 b 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 ield 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 approimation to dalight illumination. 9
bo should the bounding bo for the surface be displaed. The default is TRUE. aes should ticks and labels be added to the bo. The default is TRUE. If bo is FALSE then no ticks or labels are drawn. ticktpe character: "simple" draws just an arrow parallel to the ais to indicate direction of increase; "detailed" draws normal ticks as per 2D plots. nticks the (approimate) number of tick marks to draw on the aes. Has no effect if ticktpe is "simple".... additional graphical parameters (see par). Couleur des facettes fonction du niveau La commande z[-1, -1] permet de prendre tous les points sauf le point de coordonnée 1, 1. Dans z, il a la couleur au point d intersection et. Si on travaille sur des surfaces, il a donc une surface de moins que d intersection à la fois sur et sur. Dans zfacet, il a donc la valeur de z au centre de l élément. La fonction cut() permet de discrétiser un objet continu. 10
z z Effet du paramètre theta > persp(,, z, theta=0) > persp(,, z, theta=30) 11
z z > persp(,, z, theta=45) > persp(,, z, theta=90) 12
z Effet du paramètre phi > persp(,, z, phi=0) z > persp(,, z, phi=15) 13
> persp(,, z, phi=45) z 14
z z Graphique d une couleur homogène avec ou sans ombrage > persp(,, z, phi=15, col = "lightblue") > persp(,, z, phi=15, col = "lightblue", shade=0.5) 15
z > nrz <- nrow(z) > ncz <- ncol(z) > nbcol <- 128 > zfacet <- (z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz])/4 > facetcol <- cut(zfacet, nbcol) > color <- heat.colors(nbcol) > persp(,, z, col=color[facetcol], phi=15, theta=0) > persp(,, z, col=color[facetcol], phi=90, theta=0, d=100) 16 z
z Couleur des facettes selon un arc en ciel > color <- rainbow(nbcol) > persp(,, z, col=color[facetcol], phi=15, theta=0) > persp(,, z, col=color[facetcol], phi=90, theta=0, d=100) z 17
z Couleur des facettes selon un dégradé du bleu vers le vert > jet.colors <- colorramppalette( c("blue", "green") ) > color <- jet.colors(nbcol) > persp(,, z, col=color[facetcol], phi=15, theta=0) > persp(,, z, col=color[facetcol], phi=90, theta=0, d=100) 18 z
Représentation grâce à levelplot levelplot du package lattice permet aussi de représenter une surface avec des niveau de couleur proportionnel à une valeur. > librar("lattice") > grid <- epand.grid(=, =) > levelplot(z~*, grid, cuts = 64, lab="", lab="", main="", colorke = TRUE, region = TRU 200 5 150 0 100 5 50 0 5 0 5 19