3d Graphics Package

Posted on  by
  • Scatter plots
  • Line plots
  1. 3d Graphics Download
  2. 3d Graphic Designer Free Download

About Blender is the free and open source 3D creation suite. It supports the entirety of the 3D pipeline—modeling, rigging, animation, simulation, rendering, compositing and motion tracking, video editing and 2D animation pipeline. SketchUp is a premier 3D design software that truly makes 3D modeling for everyone, with a simple to learn yet robust toolset that empowers you to create whatever you can imagine.


In my previous articles, I already described how to make 3D graphs in R using the package below:

  • scatterplot3d, non interactive
  • scatter3d, interactive
  • rgl, interactive

To close the discussion about 3D, in this tutorial I’ll describe the impressive plot3D package and its extension plot3Drgl package.

plot3D, from Karline Soetaert, is an R package containing many functions for 2D and 3D plotting: scatter3D, points3D, lines3D,text3D, ribbon3d, hist3D, etc.

In addition to the x, y (and z) values, an additional data dimension can be represented by a color variable (argument colvar).

This “4D” plot (x, y, z, color) with a color legend is not (easily) possible using the packages mentioned above (scatterplot3d, scatter3d, rgl).

The package plot3Drgl allows to plot easily the graph generated with plot3D in openGL, as made available by package rgl. This is described at the end of the present article.

We’ll use the iris data set in the following examples :

iris data set gives the measurements of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

Functions for scatter plots and texts in 2D and 3D

The function below will be used:


  • x, y, z: vectors of point coordinates
  • colvar: a variable used for coloring
  • col: color palette used for coloring the colvar variable
  • labels: the text to be written
  • add: logical. If TRUE, then the points will be added to the current plot. If FALSE a new plot is started
  • : additional persp arguments including xlim, ylim, zlim, xlab, ylab, zlab, main, sub, r, d, scale, expand, box, axes, nticks, tictype.



Note that:

  • points3D and lines3D are shorthand for scatter3D(…, type =“p”) and scatter3D(…, type = “l”), respectively.
  • points2D and lines2D are shorthand for scatter2D(…, type = “p”) and scatter2D(…, type =“l”), respectively.


Basic scatter plot

The argument clab is used to change the title of the color legend.

By default, the points are colored automatically using the variable Z

In the R code below:

  • colvar = NULL: avoids coloring by z variable
  • col = “blue”: changes point colors to blue
  • pch = 19: changes point shapes
  • cex = 0.5: changes the size of points

Change the type of the box around the plot

The argument bty is used. Allowed values are:

  • “f”: full box
  • “b”: default value. Only the back panels are visible
  • “b2”: back panels and grid lines are visible
  • “g”: grey background with white grid lines
  • “bl”: black background
  • “bl2”: black background with grey lines
  • “u”: means that the user will specify the arguments col.axis, col.panel, lwd.panel, col.grid, lwd.grid manually
  • “n”: no box will be drawn. This is the same as setting box = FALSE

The argument colkey = FALSE is used to remove the legend.

Color palettes

Several color palettes are available in plot3D package:

  • jet.col(n, alpha): generates the matlab-type colors. This is the default color palette used in plot3D
  • jet2.col(n, alpha): similar to jet.col() but lacks the deep blue colors
  • gg.col(n, alpha) and gg2.col(n, alpha) generates gg-plot-like colors
  • ramp.col(col = c(“grey”, “black”), n, alpha): creates color schemes by interpolation
  • alpha.col(col = “grey”, alpha): creates transparent colors

  • n: Number of colors to generate. Default value is 100
  • alpha: color transparency. Value in the range 0, 1. Default value is 1
  • col: Colors to interpolate


Change the color by groups

The colkey is customized (see ?colkey for more details):

Change the position of the legend

The argument side is used to specify the colkey position: 1: for bottom, 2: for left, 3: for top, 4: for right.

3D viewing direction

The arguments theta and phi can be used to define the angles for the viewing direction. theta is the azimuthal direction and phi the co-latitude.

The default values for theta and phi are 40.

Tick marks and labels

The arguments below can be used:

  1. ticktype: Possible values are
  • “simple” draws just an arrow parallel to the axis to indicate direction of increase
  • “detailed” draws normal ticks and labels
  1. nticks: the number of tick marks to draw on the axes. It has no effect if ticktype =“simple”.

Add points and text to an existing plot

The functions below can be used:

  • scatter3D(x, y, z,…, add = TRUE): Adds points
  • text3D(x, y, z, labels, …, add = TRUE): Adds texts
  1. Add points to an existing plot:
  1. Add texts to an existing plot:
Graphics

Vertical lines are useful to see clearly the x-y location of points.

Add confidence interval

The argument CI is used. It’s a list containing the parameters and values for the confidence intervals or NULL.


If CI is a list, it should contain at least the item x, y or z (latter for scatter3D).These should be 2-columned matrices, defining the left/right intervals.

Other parameters should be one of: alen = 0.01, lty = par(“lty”), lwd = par(“lwd”), col = NULL, to set the length of the arrow head, the line type and width, and the color.

If col is NULL, then the colors as specified by colvar are used.


3D fancy Scatter plot with small dots on basal plane

A helper function scatter3D_fancy() is used:

Fancy scatter plot:

Regression plane

The mtcars data will be used:

  1. Use the function lm() to compute a linear regression model: ax + by + cz + d = 0
  2. Use the argument surf in scatter3D() function to add a regression surface.

surf is a list specifying a (fitted) surface to be added on the scatter plot. The list should include at least x, y, z, defining the surface.

Other optional parameters can be specified in the surf argument including: colvar, col, NAcol, border, facets, lwd, resfac, clim, ltheta, lphi, shade, lighting, fit. (see ?surf3D for more details on these parameters)


  • Note that, by default colvar = z.

  • The argument fit should give the fitted z-values, in the same order as the z-values of the scatter points, for instance produced by predict(). When present, this will produce droplines from points to the fitted surface.


Note that, the function expand.grid(), in the R code above, creates a data frame from all combinations of factors

The function text3D() is used as follow:

The USArrests data sets will be used in the example below:


3d Graphics Download

Note that, in order to choose suitable ranges for zooming, you can display axis ranges as follow:


The function hist3D() is used:


  • z: Matrix containing the values to be plotted
  • x, y vectors with x and y values. x should be of length equal to nrow(z) and y should be equal to ncol(z)
  • colvar: the variable used for coloring. If present, it should have the same dimension as z.
  • col: color palette to be used for the colvar variable. By default a red-yellow-blue color scheme (?jet.col) is used
  • add: Logical. If TRUE, then the surfaces will be added to the current plot. If FALSE a new plot is started.


fancy 3D histograms

Create some data:

Basic 2D scatter plot:


  1. type: plot types. Allowed values are:

3d Graphic Designer Free Download

  • “b” to draw both points and line
  • “h” for vertical line
  • “l” for line only
  • “p” for points only
  1. bty: box type


2D scatter plot with confidence interval:

It’s also possible to draw arrows, segments and rectangles in a 3D or 2D plot using the functions below:


  • x0, y0, z0: coordinates of points from which to draw
  • x1, y1, z1: coordinates of points to which to draw. For arrows3D and segments3D, at least one must be supplied. For rect3D exactly one must be NULL.
  • colvar: The variable used for coloring.
  • col: color palette to be used for coloring. Default is red-yellow-blue color scheme.
  • add: Logical. If TRUE, then the arrows, segments, … will be added to the current plot. If FALSE a new plot is started.


Prepare the data: we want to plot 4 arrows starting from the point of coordinates c(x0, y0, z0) and ending at c(x1, y1, z1)

3D Arrows:

2D arrows:

Note that, segments3D() and segments2D() are very similar to arrows3D() and arrows2D() and you can play with them also.

3D rectangle: the R code below creates a rectangle with a transparent fill color (alpha = 0.5)

In the R code above, facets = FALSE, will remove the rectangle fill color.

2D rectangle:

To draw an interactive 3D plot the package plot3Drgl can be used.

The package plot3Drgl allows to plot the graph generated with plot3D in openGL, as made available by package rgl.

The simplest way is to do as follow:

  1. Create base R-graphics using plot3D package
  2. Then use the function plotrgl() to draw the same figure in rgl

The package rgl allows to interactively rotate, zoom the graphs. However it’s not yet possible to plot a colorkey


Note that, after creating the rgl plot you can use the functions below:

  • croprgl(xlim, ylim, zlim, …) to modify the ranges
  • cutrgl(…) to zoom in on a selected region of the plot. The current plot will be overwritten
  • uncutrgl(…) and uncroprgl(…) restore the original plot.

  • …: any arguments for par3d, open3d or material3d in rgl package.


This analysis has been performed using R software (ver. 3.1.2) and plot3D (ver. 1.0-2)

References:

  • Karline Soetaert. plot3D: Tools for plotting 3-D and 2-D data. http://cran.r-project.org/web/packages/plot3D/vignettes/plot3D.pdf

Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!



Recommended for You!




More books on R and data science

Recommended for you

This section contains best data science and self-development resources to help you on your path.

Coursera - Online Courses and Specialization

Data science

  • Course: Machine Learning: Master the Fundamentals by Standford
  • Specialization: Data Science by Johns Hopkins University
  • Specialization: Python for Everybody by University of Michigan
  • Courses: Build Skills for a Top Job in any Industry by Coursera
  • Specialization: Master Machine Learning Fundamentals by University of Washington
  • Specialization: Statistics with R by Duke University
  • Specialization: Software Development in R by Johns Hopkins University
  • Specialization: Genomic Data Science by Johns Hopkins University

Popular Courses Launched in 2020

  • Google IT Automation with Python by Google
  • AI for Medicine by deeplearning.ai
  • Epidemiology in Public Health Practice by Johns Hopkins University
  • AWS Fundamentals by Amazon Web Services

Trending Courses

  • The Science of Well-Being by Yale University
  • Google IT Support Professional by Google
  • Python for Everybody by University of Michigan
  • IBM Data Science Professional Certificate by IBM
  • Business Foundations by University of Pennsylvania
  • Introduction to Psychology by Yale University
  • Excel Skills for Business by Macquarie University
  • Psychological First Aid by Johns Hopkins University
  • Graphic Design by Cal Arts

Books - Data Science

Our Books

  • Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
  • Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
  • Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
  • R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
  • GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
  • Network Analysis and Visualization in R by A. Kassambara (Datanovia)
  • Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
  • Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)

Others

  • R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
  • Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
  • Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
  • Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
  • An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
  • Deep Learning with R by François Chollet & J.J. Allaire
  • Deep Learning with Python by François Chollet


Want to Learn More on R Programming and Data Science?
Follow us by EmailOn Social Networks:

Get involved :
Click to follow us on Facebook and Google+ :
Comment this article by clicking on 'Discussion' button (top-right position of this page)