RCourse Lecture52 Statistics BivariateScatterPlots
RCourse Lecture52 Statistics BivariateScatterPlots
Lecture 52
Graphics
:::
Bivariate and Three Dimensional Scatter Plots
Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur
1
Association of two variables:
Example
• Number of hours of study affect the marks obtained in an
examination.
• Electricity/power consumption increases when the weather
temperature increases.
• Weight of infants and small children increases as their height
increases under normal circumstances.
2
Association of two variables:
The observations on both the variables are related to each other.
4
Scatter plots:
Plot the paired observations in a single graph, called as scatter
plot.
Scatter plot reveals the nature and trend of possible relationship.
Relationships : Linear or nonlinear.
y y
x x
Linear relationship between X and Y Nonlinear relationship between X and Y
5
Strength and trend of relationships:
y y
x x
Fig. 1: Strong positive linear relationship Fig. 2: Strong negative linear relationship
y y
x x
Fig. 3: Moderate positive linear relationship Fig. 4: Moderate negative linear relationship
6
Strength and trend of relationships:
y
x
Fig. 5: No clear relationship
8
Bivariate scatter plots:
Plot command:
x, y: Two data vectors
plot(x, y)
plot(x, y, type)
type
“p" for points “l" for lines
“b" for both “c" for the lines part alone of “b"
Marks 384 398 413 428 430 438 439 479 460 450
Number of 35 38 39 42 43 44 45 46 44 41
hours per
week 11
Bivariate scatter plots:
Example
marks =
c(337,316,327,340,374,330,352,353,370,380,384,39
8,413,428,430,438,439,479,460,450)
hours =
c(23,25,26,27,30,26,29,32,33,34,35,38,39,42,43,4
4,45,46,44,41)
12
Bivariate scatter plots:
Example
plot command:
plot(x, y)
plot(hours, marks)
13
Bivariate scatter plots:
Example
plot(hours, marks, "l")
“l" for lines,
14
Bivariate scatter plots:
Example
plot(hours, marks, "b")
“b" for both – line and point
15
Bivariate scatter plots:
Example
plot(hours, marks, "o")
“o" for both ‘overplotted’
16
Bivariate scatter plots:
Example
plot(hours, marks, "h")
“h" for ‘histogram’ like (or ‘high‐density’) vertical lines
17
Bivariate scatter plots:
Example
plot(hours, marks, "s")
“s" for stair steps.
18
Bivariate scatter plots:
Example
plot(hours, marks, xlab="Number of weekly
hours", ylab="Marks obtained", main="Marks
obtained versus Number of hours per week")
19
Matrix Scatter plot:
The command pairs() allows the simple creation of a matrix of
scatter plots.
Example
pairs(cbind(hours, marks))
20
Matrix Scatter plot
Example: Adding labels and colour
pairs(cbind(hours, marks), labels=c("Study
hours", "Marks obtained"), col="red" )
21
Scatter plots with smooth curve:
Suppose two variables are related.
A scatter plot along with a fitted line will provide information on
the trend or relationship between them.
22
Scatter plots with smooth curve:
scatter.smooth is based on the concept of LOESS which is a
locally weighted scatterplot smoothing method.
23
Scatter plots with smooth curve:
scatter.smooth(x, y = NULL, span = 2/3, degree
= 1, family = c("symmetric","gaussian"), xlab =
NULL, ylab = NULL, ylim = range(y, pred$y,
na.rm = TRUE),...)
Marks 384 398 413 428 430 438 439 479 460 450
Number of 35 38 39 42 43 44 45 46 44 41
hours per
week 25
Scatter plots with smooth curve:
Example
marks =
c(337,316,327,340,374,330,352,353,370,380,384,39
8,413,428,430,438,439,479,460,450)
hours =
c(23,25,26,27,30,26,29,32,33,34,35,38,39,42,43,4
4,45,46,44,41)
26
Scatter plots with smooth curve:
Example
27
Scatter plots with smooth curve:
Example: Add more options
scatter.smooth(hours, marks, lpars = list(col =
"red", lwd = 3, lty = 3))
28
Three dimensional scatter plot:
scatterplot3d(x,y,z)
Plots a three dimensional (3D) point cloud of the data in x,y and z
install.packages("scatterplot3d")
library(scatterplot3d)
29
Three dimensional scatter plot:
Example
The data on height (in cms.), weight (in kg.) and age (in years) of 5
30
Three dimensional scatter plot:
scatterplot3d() Plots a three dimensional (3D) point cloud
install.packages("scatterplot3d")
library(scatterplot3d)
31
Three dimensional scatter plot:
scatterplot3d(height, weight, age)
32
Three dimensional scatter plot:
Direction of the figure can be changed.
33
Three dimensional scatter plot:
Colours of points can be changed.
scatterplot3d(height, weight, age, color="red")
34
More functions:
• contour() for contour lines
35
More functions:
Example of perspective plot
36
More functions:
Example of perspective plot
persp(x,y,z, theta=30, phi=30, expand=0.5, col=
"lightblue")
37
More functions:
Example of perspective plot
persp(x, y, z, theta = 30, phi = 30, expand =
0.5, col = "lightblue", ltheta = 120, shade =
0.75, ticktype = "detailed", xlab = "X", ylab =
"Y", zlab = "Sinc( r )")
38