0% found this document useful (0 votes)
25 views38 pages

RCourse Lecture52 Statistics BivariateScatterPlots

The document discusses different types of scatter plots that can be used to visualize the relationship between two or three variables. It provides examples of using R to create scatter plots, matrix scatter plots, scatter plots with a smooth curve fitted, and three-dimensional scatter plots. Various plot types and options for customizing plots are demonstrated.

Uploaded by

kavithanjali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views38 pages

RCourse Lecture52 Statistics BivariateScatterPlots

The document discusses different types of scatter plots that can be used to visualize the relationship between two or three variables. It provides examples of using R to create scatter plots, matrix scatter plots, scatter plots with a smooth curve fitted, and three-dimensional scatter plots. Various plot types and options for customizing plots are demonstrated.

Uploaded by

kavithanjali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Foundations of R Software

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.

How to know the variables are related?

How to know the degree of relationship between the two variables?

Graphical procedures – Two dimensional plots, three dimensional


plots etc.

Quantitative procedures – Correlation coefficients, contingency


tables, Chi‐square statistic, linear regression, nonlinear regression
etc.
3
Association of two variables:
How to judge or graphically summarize the association of two
variables?
X, Y : Two variables
n pairs of observations are available as (x1,y1), (x2,y2),…,(xn,yn)

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

We will study about the direction and degree of linear


relationships.

Two aspects – graphical and quantitative


7
Bivariate plots:
Provide first hand visual information about the nature and
degree of relationship between two variables.

Relationship can be linear or nonlinear.

We discuss several types of plots through examples.

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"

“o" for both ‘overplotted’ “s" for stair steps.

“h" for ‘histogram’ like (or ‘high‐density’) vertical lines


9
Bivariate scatter plots:
Plot command
x, y: Two data vectors
plot(x, y)
plot(x, y, type)

Get more details from help: help("type")


Other options:
main an overall title for the plot.
suba sub title for the plot.
xlab title for the x axis.
ylab title for the y axis.
aspthe y/x aspect ratio. 10
Bivariate scatter plots:
Example
Data on marks obtained by 20 students out of 500 marks and the
number of hours they studied per week are recorded as follows:

We know from experience that marks obtained by students increase


as the number of hours increase.
Marks 337 316 327 340 374 330 352 353 370 380
Number of 23 25 26 27 30 26 29 32 33 34
hours per
week

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:

x, y: Two data vectors

Various type of plots are


possible to draw.

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.

scatter.smooth produces a scatter plot and adds a smooth


curve to the scatter plot.

22
Scatter plots with smooth curve:
scatter.smooth is based on the concept of LOESS which is a
locally weighted scatterplot smoothing method.

LOESS is used for local polynomial regression fitting.

Fit a polynomial surface determined by one or more numerical


predictors, using local fitting.

Use help("scatter.smooth") to get more details.

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),...)

x, y x and y arguments provide the x and y coordinates for the


plot.
span smoothness parameter for LOESS.
degree degree of local polynomial used.
family if "gaussian" fitting is by least-squares, and if family =
"symmetric" a re-descending M estimator is used.
xlab label for x axis.
ylab label for y axis.
ylim the y limits of the plot.
24
Scatter plots with smooth curve:
Example
Data on marks obtained by 20 students out of 500 marks and the
number of hours they studied per week are recorded as follows:

We know from experience that marks obtained by students increase


as the number of hours increase.
Marks 337 316 327 340 374 330 352 353 370 380
Number of 23 25 26 27 30 26 29 32 33 34
hours per
week

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

scatter.smooth(x,y) provides scatter plot with smooth curve


Example: scatter.smooth(hours, marks)

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

Need a package scatterplot3d

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

persons are recorded as follows. We would like to create a 3

dimensional plot for this data.


Person No. Height (Cms.) Weight (Kg.) Age (Years)
1 100 30 10
2 125 35 15
3 145 50 20
4 160 65 30
5 170 70 35

30
Three dimensional scatter plot:
scatterplot3d() Plots a three dimensional (3D) point cloud

install.packages("scatterplot3d")

library(scatterplot3d)

height = c(100, 125, 145, 160, 170)

weight = c(30, 35, 50, 65, 70)

age = c(10, 15, 20, 30, 35)

31
Three dimensional scatter plot:
scatterplot3d(height, weight, age)

32
Three dimensional scatter plot:
Direction of the figure can be changed.

scatterplot3d(height, weight, age, angle = 120)

33
Three dimensional scatter plot:
Colours of points can be changed.
scatterplot3d(height, weight, age, color="red")

34
More functions:
• contour() for contour lines

• dotchart() for dot charts (replacement for bar charts)

• image() pictures with colors as third dimension

• mosaicplot() mosaic plot for (multidimensional) diagrams

of categorical variables (contingency tables)

• persp() perspective surfaces over the x–y plane

35
More functions:
Example of perspective plot

persp() perspective surfaces over the x–y plane


x = seq(-10, 10, length= 30)
y = x
f =function(x,y){r = sqrt(x^2+y^2);10*sin(r)/r}
z = outer(x, y, f)
z[is.na(z)] = 1
op = par(bg = "white")

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

You might also like