Scatter Plots in R

 

Scatter Plots in R

Introduction

A scatter plot is a graph used to display the relationship between two numerical variables. Each observation is represented as a point whose coordinates correspond to the values of the two variables.

Scatter plots are widely used to:

  • Study relationships between variables.
  • Detect trends and patterns.
  • Identify clusters and outliers.
  • Examine correlations.
  • Visualize experimental and statistical data.

For example:

  • Height vs Weight
  • Age vs Salary
  • Study Hours vs Marks
  • Temperature vs Ice Cream Sales

In R, scatter plots are created using the plot() function.


Syntax

plot(x, y,
main,
xlab,
ylab,
col,
pch,
cex)

where

  • x : values on x-axis.
  • y : values on y-axis.
  • main : title.
  • xlab : x-axis label.
  • ylab : y-axis label.
  • col : point color.
  • pch : plotting symbol.
  • cex : point size.

Example 1: Simple Scatter Plot

Suppose the study hours and marks of students are:

HoursMarks
245
455
560
775
990
hours <- c(2,4,5,7,9)

marks <- c(45,55,60,75,90)

plot(hours, marks)

Example 2: Adding Title and Labels

plot(hours,
marks,
main="Study Hours vs Marks",
xlab="Hours Studied",
ylab="Marks")




Example 3: Changing Color

plot(hours,
marks,
col="blue")

Available colors:

  • red
  • blue
  • green
  • black
  • orange
  • purple

Example 4: Changing Symbols

plot(hours,
marks,
pch=16)

Common Values of pch

pchSymbol
1Circle
2Triangle
3Plus
4Cross
15Square
16Filled Circle
17Filled Triangle
18Diamond

Example:

plot(hours,
marks,
pch=17,
col="red")



Example 5: Increasing Point Size

plot(hours,
marks,
pch=16,
cex=2)

cex=2 doubles the size of the points.




Example 6: Adding Grid

plot(hours,
marks,
pch=19)

grid()

Example 7: Adding Labels to Points

names <- c("A","B","C","D","E")

plot(hours,
marks,
pch=19)

text(hours,
marks,
labels=names,
pos=3)

The labels appear above the points.




Example 8: Height vs Weight

height <- c(150,160,165,170,180)

weight <- c(50,60,65,70,85)

plot(height,
weight,
main="Height vs Weight",
xlab="Height",
ylab="Weight",
pch=16,
col="blue")




Example 9: Age vs Salary

age <- c(22,25,30,35,40)

salary <- c(25000,35000,50000,70000,90000)

plot(age,
salary,
pch=19,
col="green")



Example 10: Adding a Regression Line

hours <- c(2,4,5,7,9)
marks <- c(45,55,60,75,90)

plot(hours,
marks,
pch=16)

model <- lm(marks ~ hours)

abline(model,
col="red",
lwd=2)

This draws the best-fitting straight line.




Example 11: Customizing the Plot

plot(hours,
marks,
main="Study Hours vs Marks",
xlab="Hours",
ylab="Marks",
col="blue",
pch=18,
cex=1.5)



Example 12: Using Data Frames

student <- data.frame(
Hours=c(2,4,5,7,9),
Marks=c(45,55,60,75,90)
)

plot(student$Hours,
student$Marks,
pch=19,
col="red")




Example 13: Multiple Colors

hours <- c(2,4,5,7,9)

marks <- c(45,55,60,75,90)

plot(hours,
marks,
col=c("red","blue","green","orange","purple"),
pch=19,
cex=2)




Example 14: Specifying Axis Limits

plot(hours,
marks,
xlim=c(0,10),
ylim=c(0,100))

Example 15: Plotting Random Data

x <- rnorm(100)

y <- rnorm(100)

plot(x,
y,
pch=16,
col="blue")

This is often used to study distributions and clusters.



Correlation Patterns

Positive Correlation

As x increases, y increases.





Example:

  • Height vs Weight
  • Study Hours vs Marks

Negative Correlation

As x increases, y decreases.




Example:

  • Speed vs Travel Time

No Correlation

Randomly scattered points.

•     •




Important Parameters

ParameterPurpose
xx-axis values
yy-axis values
mainTitle
xlabx-axis label
ylaby-axis label
colColor
pchPoint shape
cexPoint size
xlimx-axis limits
ylimy-axis limits

Additional Functions

Add Text

text(x,y,labels)

Add Grid

grid()

Add Regression Line

abline(model)

Fit Linear Model

lm(y~x)

Applications of Scatter Plots

  • Correlation analysis
  • Machine learning datasets
  • Regression analysis
  • Height vs Weight studies
  • Stock market analysis
  • Experimental measurements
  • Population studies
  • Age vs Salary analysis
  • Scientific research

Difference Between Line Graph and Scatter Plot

Scatter PlotLine Graph
Shows relationship between variables        Shows trends over time
Data represented as points        Points connected by lines
Order not important        Order important
Created using plot(x,y)        Created using plot(x,y,type="l")
Used for correlation analysis        Used for trend analysis

Conclusion

A scatter plot is one of the most important graphical tools in R for visualizing the relationship between two numerical variables. By customizing parameters such as col, pch, and cex, and adding regression lines using abline(), scatter plots become powerful tools for correlation analysis, statistical modeling, and machine learning.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1