i

R Programming Complete Tutorial

R Data Visualization

One of R’s power is visualization.Graphics should be key in analyzing the data.  We don't understand numbers intuitively like we do images. Graphs are easy to produce for data exploration.R allows practicing a wide variety of statistical and graphical techniques like linear and nonlinear modeling, time-series analysis, classification, standard statistical tests, clustering, etc.

Properties of R Graphics:

  • We can directly create plots from R code.

  • Replication and modification are very easy.

  • Reproducibility

  • R provides a good variety of  packages:  ggplot2, plotly, ggedit, graphics

Create first graph:

We can create a graph in R just using the plot function.

In this example, we have assigned a vector of 10 elements in the x variable. In y, we are storing the square value of x. So in y, we have 1, 4, 9, 16,…., 100.

If we plot a graph using x  and y, it will display a line like the below graph.

Graph Output:

Plot  data:

In this section, we will discuss the plot () function in detail.

Syntax of plot () Function:

The mandatory part is the R objects, based on which we will create our graph. In this example, we are using two vectors x and y as a mandatory part. 

In the main, we will define the overall title of the graph. Xlab and ylab are for the title of the x-axis and y-axis, respectively. col is used for the color of the plot, and col.main is for the color of the title. cex.axis is used to control the ratio of the axis label. We will explain type, lty, and pch separately.

type argument:

We have some alternatives for the type argument. If we use "p", the graph will be displayed using point. If we use " l ", it will be a line. If we want to display both points and lines, we have to use "b". I have attached the full list in the below section.

lty argument:

lty represents line type. In plot function, we use seven kinds of lines. We can use the name or associated number to set the value in lty argument.

pch argument:

pch represents numeric values (from 0 to 25), specifying the point symbols (or shapes). We have 25 point shapes, and we can set any of them just using the specific number.