Module 6:
Data Visualization
Lahoucine Ech-chatir Data Management & Analysis with R 145
Introduction
The R Graphics environment is extremely powerful and is particularly oriented
to the scientific visualization of data. R provides numerous functions dedicated
to graphics management both at a basic level and with specific packages.
These outputs are easily accessible, fully programmable and exportable in the
most common graphic formats (png, gif, bmp,…).
[Link]
[Link]
Lahoucine Ech-chatir Data Management & Analysis with R 146
1
Introduction
The four main graphical environments are:
Base Graphics
- R Base Graphics (package:graphics)
- grid package
Advanced Graphics
- lattice package
- ggplot2 package
Lahoucine Ech-chatir Data Management & Analysis with R 147
Base Graphics
The most important high-level functions for basic graphics are:
• plot() : generic x-y plotting
• barplot() : bar plots
• Boxplot() : box and whisker plot
• Hist() : histograms
• Pie() : pie charts
• Dotchart() : cleveland dot plots
• Qqnorm(), qqline(), qqplot() : distribution comparison plots
• Pairs(), coplot() : display of multivariant data
• ….
Lahoucine Ech-chatir Data Management & Analysis with R 148
2
Base Graphics
Base Graphics – Scatter Plots
# Load the data and name it Iris
Iris <- [Link](“[Link]")
View(Iris)
Lahoucine Ech-chatir Data Management & Analysis with R 149
Base Graphics
Base Graphics – Scatter Plots
In order to plot the columns 2 and 3 using a scatter plot, we can use the plot()
function.
#plot the scatter plot
plot(Iris[,2], Iris[,3])
Lahoucine Ech-chatir Data Management & Analysis with R 150
3
Base Graphics
Base Graphics – All pairs
#plot all pairs of the columns from 2 to 5
pairs(Iris[,2:5])
Lahoucine Ech-chatir Data Management & Analysis with R 151
Base Graphics
Base Graphics – Plot Labels and Text
#Plot Labels and Text
plot(Iris[,2], Iris[,3],
pch=20,
col="blue",
main="Scatter")
text(Iris[,2]+0.03,
Iris[,3],
rownames(Iris))
The text() function adds the label
in the specified x-y coordinates to plot.
Lahoucine Ech-chatir Data Management & Analysis with R 152
4
Base Graphics
Base Graphics – Plotting
characters (pch)
The pch parameter defines the type of
point to be drawn with a numerical value.
For instance, pch=8 will draw an asterisk
instead of the filled circle, whose color is
defined by the parameter col.
The main parameter defines the title of
the plot.
Lahoucine Ech-chatir Data Management & Analysis with R 153
Base Graphics
Base Graphics – Plot Parameters
The plot() function is equipped with many input parameters that allow an extensive
customization of the graph.
In this regard, consult the guide:
help(plot)
# graphical parameters
help(par)
# all graphic objects will have the properties expressed in the par() function
op <- par(mar=c(7,7,7,7), bg="lightyellow")
# the graphical properties expressed in the plot function, on the other hand, have a local
validity
plot(Iris[,2], Iris[,3], type="p", col="darkblue", [Link]=1.2,
[Link]=1.2, [Link]=1.2, [Link]=1, lwd=1, pch=3, xlab="x
label", ylab="y label", main="Main Title", sub="Sub Title")
grid(3, 3, lwd = 2)
Lahoucine Ech-chatir Data Management & Analysis with R 154
5
Base Graphics
Base Graphics – Plot Parameters
Lahoucine Ech-chatir Data Management & Analysis with R 155
Base Graphics Op <- par(mar=c(7,7,7,7), bg="lightyellow")
Base Graphics – Plot Parameters
The mar parameter is a numerical vector of 4 elements that defines the space between the
axes and the edge of the graph in accordance with the syntax:
• c(bottom,left,top,right)
• If not expressed, the default values are: c(5.1,4.1,4.1,2.1)
The bg parameter defines the background colour of the plot (check colors())
[Link]() delete all the graphical properties stored with par() including graphic objects
Lahoucine Ech-chatir Data Management & Analysis with R 156
6
Base Graphics
Base Graphics – Plot Parameters
type indicates which type of graph is to be
drawn
cex is a numeric value that sets the size of
text and symbols
To the numbers in the axes ([Link]),
To the axes labels ([Link]),
To the title of the plot ([Link])
To the subtitle of the plot ([Link])
col checks the colors of the symbols
As for the cex parameter there are: [Link],
[Link], [Link], [Link]
plot(Iris[,2], Iris[,3], type="p", col="darkblue", [Link]=1.2,
[Link]=1.2, [Link]=1.2, [Link]=1, lwd=1, pch=3, xlab="x label",
ylab="y label", main="Main Title", sub="Sub Title")
grid(3, 3, lwd = 2)
Lahoucine Ech-chatir Data Management & Analysis with R 157
Base Graphics
Base Graphics – abline()
The abline() function adds a line in the
current plot.
plot(Iris[,2], Iris[,3])
# I perform the linear regression using lm()
where Iris[,2] is the regressor and Iris[,3] is the
dependent variable
myline <- lm(Iris[,3]~Iris[,2]);
# add the regression line
abline(myline, lwd=2, lty=5)
lty indicates the type of line to be drawn
and it is an integer: 1: solid - 2: dashed - 3:
dotted 4: dotdash- 5: longdash - 6: twodash
Lahoucine Ech-chatir Data Management & Analysis with R 158
7
Base Graphics
Base Graphics – Line Plot
#line plot – single dataset
plot(Iris[,2], type="l", lwd=1,
col="blue")
# plot two lines in the same graphical device:
[Link](c(1,1)) #dataset 1
plot(Iris[,2], type="l", lwd=1,
ylim=c(4,8), col="blue")
screen(1, new=FALSE) #dataset 2
plot(Iris[,3], type="l", lwd=1,
col="red", xaxt="n", yaxt="n",
ylab="", xlab="", main="",
bty="n")
Lahoucine Ech-chatir Data Management & Analysis with R 159