Basic Plot in R
Basic Plot in R
Introduction
Visualization is one of the most powerful features of R. The plot() function is the most fundamental graphics function in R and is used to create a variety of graphs such as:
- Scatter plots
- Line graphs
- Points plots
- Combined line and point plots
The exact graph produced by plot() depends on the type of data supplied.
Syntax
plot(x, y,
main,
xlab,
ylab,
col,
pch,
type,
lwd)
where
- x : x-axis values
- y : y-axis values
- main : title of the graph
- xlab : x-axis label
- ylab : y-axis label
- col : color
- pch : plotting symbol
- type : type of graph
- lwd : line width
1. Simple Scatter Plot
Suppose we have marks of five students.
marks <- c(75,85,90,60,95)
plot(marks)
Output:
Adding Title and Labels
2. Scatter Plot Between Two Variables
age <- c(18,19,20,21,22)
marks <- c(70,75,85,90,95)
plot(age,
marks,
main="Age vs Marks",
xlab="Age",
ylab="Marks")
This produces a scatter plot.
3. Plot Types
The argument type determines how data is displayed.
Points (type="p")
(Default)
plot(age, marks, type="p")
Line Plot (type="l")
Both Points and Lines (type="b")
Histogram-like Vertical Lines (type="h")
Step Plot (type="s")
4. Colors
Use the col parameter.
plot(age,
marks,
col="red")
Other colors:
"blue"
"green"
"yellow"
"orange"
"black"
"purple"
5. Plot Symbols (pch)
plot(age,
marks,
pch=16)
Common symbols:
Example:
6. Line Width
plot(age,
marks,
type="l",
lwd=3)
Larger lwd means thicker lines.
7. Changing Axis Limits
plot(age,
marks,
xlim=c(15,25),
ylim=c(50,100))
8. Adding Grid
plot(age,
marks)
grid()
9. Adding More Points
plot(age,
marks)
points(20,80,
col="red",
pch=19)
Adds a point at (20,80).
10. Adding Lines
11. Adding Text
plot(age,
marks)
text(age,
marks,
labels=marks,
pos=3)
Output:
Each point is labeled with its mark.
12. Multiple Curves on Same Graph
13. Plotting Mathematical Functions
14. Sine Function
15. Using Sequences
Important Parameters
| Parameter | Purpose |
|---|---|
| main | Title |
| xlab | X-axis label |
| ylab | Y-axis label |
| col | Color |
| pch | Point symbol |
| type | Plot type |
| lwd | Line width |
| xlim | X-axis range |
| ylim | Y-axis range |
| cex | Point size |
| grid() | Add grid |
| text() | Add labels |
| points() | Add points |
| lines() | Add curves |
| legend() | Add legend |
Common Values of type
| Type | Meaning |
|---|---|
| "p" | Points |
| "l" | Lines |
| "b" | Both |
| "o" | Overplotted |
| "h" | Vertical lines |
| "s" | Stair steps |
| "n" | Empty plot |
Applications of plot()
- Scatter plots
- Mathematical function graphs
- Growth trends
- Sales analysis
- Population studies
- Experimental data visualization
- Time-series analysis
Conclusion
The plot() function is the foundation of graphics in R. By changing parameters such as type, col, pch, and lwd, a variety of plots can be produced. Additional functions like lines(), points(), text(), and legend() can be used to enrich the graph. Mastering plot() is essential before learning advanced graphics packages such as ggplot2.













Comments
Post a Comment