- Scatter plots
- Line plots
I have an assignment with a graph that requires an R squared value. I know how to do it, I just don't know what it means or more importantly WHY I need it. If someone could explain (in normal people language) that would be awesome:). Very simple creating beautiful charts - that's what graphR. The purpose of graphR. Is to automatize and simplify the analysis of RVTools exports and to give a visual presentation of the information contained within one Excel export.
A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable. R uses the function barplot to create bar charts. R can draw both vertical and Horizontal bars in the bar chart. Graph of r = sin 2θ This curve is a favorite; a similar curve appears in the homework. We’ll plot a few points (sin 2θ, θ) to get an idea of what the graph of this curve looks like.
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:
- ticktype: Possible values are
- “simple” draws just an arrow parallel to the axis to indicate direction of increase
- “detailed” draws normal ticks and labels
- 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
- Add points to an existing plot:
- Add texts to an existing plot:
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:
- Use the function lm() to compute a linear regression model: ax + by + cz + d = 0
- 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:
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:
- type: plot types. Allowed values are:
- “b” to draw both points and line
- “h” for vertical line
- “l” for line only
- “p” for points only
- 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:
- Create base R-graphics using plot3D package
- 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
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
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
Graph Reader
- 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
Graph-ruled Paper
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:
Graph Reflection
Click to follow us on Facebook and Google+ :
Comment this article by clicking on 'Discussion' button (top-right position of this page)