In this article, you will learn to create 3D plots. Also, you will learn to add title, change viewing direction, and add color and shade to the plot.

R graphs support both two dimensional and three-dimensional plots for exploratory data analysis.There are R function like plot, barplot, pie are used to develop graphs in R language. R package like ggplot2 supports advance graphs functionalities. Types of Graphs in R. A variety of graphs is available in R, and the use is solely governed. Create Graphic with Multiple Plots. In Example 1, I’ll illustrate how to draw a graphic containing.



There are many functions in R programming for creating 3D plots. In this section, we will discuss on the persp() function which can be used to create 3D surfaces in perspective view.

This function mainly takes in three variables, x, y and z where x and y are vectors defining the location along x- and y-axis. The height of the surface (z-axis) will be in the matrix z. As an example,

Let’s plot a cone. A simple right circular cone can be obtained with the following function.

Now let’s prepare our variables.

We used the function seq() to generate vector of equally spaced numbers.

Then, we used the outer() function to apply the function cone at every combination of x and y.

Plot function r

Finally, plot the 3D surface as follows.

Adding Titles and Labeling Axes to Plot

We can add a title to our plot with the parameter main.

Similarly, xlab, ylab and zlab can be used to label the three axes.

Rotational angles

We can define the viewing direction using parameters theta and phi.

By default theta, azimuthal direction, is 0 and phi, colatitude direction, is 15.

Coloring and Shading Plot

Coloring of the plot is done with parameter col.

Plot Data In R

Similarly, we can add shading with the parameter shade.



R plots examples
  • PREVIOUS
    R Plot Color
Plotting with r © 2006-16 by Frank McCownPlots

The following is an introduction for producing simple graphs with the R Programming Language. Each example builds on the previous one. The areas in bold indicate new text that was added to the previous example. The graph produced by each example is shown on the right.

Jump to a section:

R Plots Examples

Line Charts

First we'll produce a very simple graph using the values in the car vector:
Let's add a title, a line to connect the points, and some color:
Now let's add a red line for trucks and specify the y-axis range directly so it will be large enough to fit the truck data:
Next let's change the axes labels to match our data and add a legend. We'll also compute the y-axis values using the max function so any changes to our data will be automatically reflected in our graph.
Now let's read the graph data directly from a tab-delimited file. The file contains an additional set of values for SUVs. We'll save the file in the C:/R directory (you'll use a different path if not using Windows).

autos.dat

We'll also use a vector for storing the colors to be used in our graph so if we want to change the colors later on, there's only one place in the file that needs to be modified. Finally we'll send the figure directly to a PNG file.
In this next example, we'll save the file to a PDF and chop off extra white space around the graph; this is useful when wanting to use figures in LaTeX. We'll also increase the line widths, shrink the axis font size, and tilt the x-axis labels by 45 degrees.

Bar Charts

Let's start with a simple bar chart graphing the cars vector:
Let's now read the auto data from the autos.dat data file, add labels, blue borders around the bars, and density lines:
Now let's graph the total number of autos per day using some color and show a legend:
Let's graph the total number of autos per day using a stacked bar chart and place the legend outside of the plot area:

Histograms

Let's start with a simple histogram graphing the distribution of the suvs vector:
Let's now read the auto data from the autos.dat data file and plot a histogram of the combined car, truck, and suv data in color.
Now change the breaks so none of the values are grouped together and flip the y-axis labels horizontally.
Now let's create uneven breaks and graph the probability density.
In this example we'll plot the distribution of 1000 random values that have the log-normal distribution.
Since log-normal distributions normally look better with log-log axes, let's use the plot function with points to show the distribution.

Pie Charts

Let's start with a simple pie chart graphing the cars vector:
Now let's add a heading, change the colors, and define our own labels:
Now let's change the colors, label using percentages, and create a legend:

Dotcharts

Let's start with a simple dotchart graphing the autos data:
Let's make the dotchart a little more colorful:

Misc

This example shows all 25 symbols that you can use to produce points in your graphs:

Plotting With R