Scatter Plots in R
- Get link
- X
- Other Apps
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:
| Hours | Marks |
|---|---|
| 2 | 45 |
| 4 | 55 |
| 5 | 60 |
| 7 | 75 |
| 9 | 90 |
hours <- c(2,4,5,7,9)
marks <- c(45,55,60,75,90)
plot(hours, marks)
Example 2: Adding Title and Labels
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
| pch | Symbol |
|---|---|
| 1 | Circle |
| 2 | Triangle |
| 3 | Plus |
| 4 | Cross |
| 15 | Square |
| 16 | Filled Circle |
| 17 | Filled Triangle |
| 18 | Diamond |
Example:
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
Example 9: Age vs Salary
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
Example 13: Multiple Colors
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
| Parameter | Purpose |
|---|---|
| x | x-axis values |
| y | y-axis values |
| main | Title |
| xlab | x-axis label |
| ylab | y-axis label |
| col | Color |
| pch | Point shape |
| cex | Point size |
| xlim | x-axis limits |
| ylim | y-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 Plot | Line 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.
- Get link
- X
- Other Apps





Comments
Post a Comment