36
Chapter 5
BASIC DATA VISUALIZATION
Data visualization is an essential and necessary part of statistical analysis. Data
visualization is a method of displaying data in graphical form, useful both for presenting
results and for analyzing data sets. The visualization tools that are appropriate for a given
data set depend on the type of problem being solved and therefore require feedback. In
this chapter, we will introduce some of the most commonly used plots in statistical analysis
to visualize data with examples.
5.1 Plots in 2D
5.1.1 Barplots
A bar chart is a graphical representation of data that presents categorical data with
rectangular bars. R uses the function barplot() to create bar charts with the following
syntaxe: barplot(H, xlab, ylab, main, names.arg, col). Where:
• H: a vector or matrix of numeric values.
• xlab: the label for the x-axis in a bar chart.
• ylab: the label for the y-axis in a bar chart.
• main: the title of the bar chart.
• names.arg: a vector of names appearing under each bar in a bar chart.
5.1.1.1 Example
- Example 01: The example illustrated in figure 5.1 shows how to plot a bar chart
using all parameters.
37
Figure 5.1: Example of a Bar plot of a vector in RStudio.
• It represents the Bar function with parameters.
• It presents the result of executing the function.
• It displays the Bar chart.
- Example 02:
Figure 5.2: Example of a Bar plot of a matrix in RStudio.
5.1.2 Pie Chart
A pie chart is a circular statistical graphic, which is divided into slices or “pie slices”
to illustrate numerical proportions. The function "pie" is used to plot the chart in R
software. The syntax is as follows: Syntax: pie(x, labels, radius, main, col, clockwise).
Where:
38
• x: a vector of numeric values.
• labels: the description of the slices in a pie chart.
• radius: the radius of the circle of the pie chart. (value between -1 and +1).
• main: the title of the pie chart.
• clockwise: the logical value which indicates whether the slices are drawn clockwise
or in the anti-clockwise direction.
• col: the colors of the pie in the graph.
Example:
Figure 5.3: Example of a pie plot in RStudio.
5.1.3 Histograms
The histogram can be created in R using "Hist" function where the syntax is as follow:
hist(v, main, xlab, xlim, ylim, breaks, col, border). where :
• v: a set of numerical values.
• main: the title of the chart.
• col: the color of the bars.
• xlab: the label for the horizontal axis.
• border: a border color of each bar.
39
• xlim: plotting values of the x-axis.
• ylim: plotting values of the y-axis.
• breaks: width of each bar.
5.1.3.1 Example
The example illustrated in figure 5.4 shows how to plot a histogram chart using all
parameters that can define the range of values that we need by using the xlim and ylim
parameters in the X-axis and Y-axis.
Figure 5.4: Example of a histogram plot in RStudio.
• It represents the histogram function with parameters.
• It presents the result of executing the function.
• It displays the histogram chart.
5.1.4 Scatterplots
A scatter plot is a set of dotted points to represent individual pieces of data in the
horizontal and vertical axis. There are many ways to create a scatterplot in R software.
The basic function is plot(x, y), where x and y are numeric vectors denoting the (x,y)
40
points to plot. the syntax is as follows: plot(x, y, main, xlab, ylab, xlim, ylim, axes).
where :
• x: the horizontal coordinates.
• y: the vertical coordinates.
• xlab: the label for the horizontal axis.
• ylab: the label for the vertical axis.
• main: the title of the chart.
• xlim: values of x.
• ylim: values of y.
• axes: drawn on the plot.
Example:
Figure 5.5: Example of a Scatter plot in RStudio.
5.2 Plots in 3D
The R program provides many functions that allow 3D drawing, adding a title, chang-
ing the display direction, and add color and shadow to the plot. To use any 3D plot
function, the user must install another package that contains the desired function. For
41
example, scatterplot3d package is used to plot to scatter. One of these functions is the
persp() function which is used to create 3D surfaces in perspective view. For more details,
consult the book [1].
5.3 Exercise
Using basic graphics
1. Create five vectors with 30 elements in each one.
2. Fill each vector with random values between (5 and 60).
3. Combine vectors as columns in a matrix.
4. Produce a histogram of the third column in the matrix using base R graphics.