Line Graph in R
- Get link
- X
- Other Apps
Line Graph in R
Introduction
A line graph is used to display the relationship between two variables by connecting data points with lines. It is particularly useful for showing:
- Trends over time
- Growth or decline
- Continuous data
- Comparisons between multiple datasets
Common applications include:
- Stock prices
- Monthly sales
- Population growth
- Temperature variation
- Experimental observations
In R, line graphs are created using the plot() function with the parameter:
type="l"
where "l" stands for line.
Syntax
plot(x, y,
type="l",
main,
xlab,
ylab,
col,
lwd)
where
- x : x-axis values
- y : y-axis values
- type="l" : line graph
- main : title
- xlab : x-axis label
- ylab : y-axis label
- col : line color
- lwd : line width
Example 1: Simple Line Graph
Suppose the sales of a company over five years are:
| Year | Sales |
|---|---|
| 2020 | 15 |
| 2021 | 18 |
| 2022 | 22 |
| 2023 | 25 |
| 2024 | 30 |
year <- c(2020,2021,2022,2023,2024)
sales <- c(15,18,22,25,30)
plot(year,
sales,
type="l")
This produces a simple line graph.
Example 2: Adding Title and Labels
Example 3: Changing Line Color
Available colors:
- red
- blue
- green
- black
- orange
- purple
Example 4: Changing Line Width
plot(year,
sales,
type="l",
col="red",
lwd=3)
lwd=3 produces a thicker line.
Example 5: Plotting Points and Lines
plot(year,
sales,
type="b",
pch=16,
col="blue")
Here
b= both points and linespch=16= filled circles
Example 6: Different Line Styles
plot(year,
sales,
type="l",
lty=2)
Common values of lty:
| lty | Style |
|---|---|
| 1 | Solid |
| 2 | Dashed |
| 3 | Dotted |
| 4 | Dot-Dash |
| 5 | Long Dash |
Example:
Example 7: Multiple Line Graphs
Suppose sales of two products are:
year <- c(2020,2021,2022,2023,2024)
productA <- c(10,15,20,25,30)
productB <- c(12,18,24,28,35)
Example 8: Monthly Temperature Variation
Example 9: Plotting Mathematical Functions
Plot y = x²
Plot y = sin(x)
Example 10: Adding Grid
plot(year,
sales,
type="l",
lwd=3)
grid()
The grid improves readability.
Example 11: Adding Points
Example 12: Adding Text Labels
plot(year,
sales,
type="b")
text(year,
sales,
labels=sales,
pos=3)
This displays sales values above each point.
Example 13: Customized Graph
Important Parameters
| Parameter | Purpose |
|---|---|
| type="l" | Line graph |
| col | Color |
| lwd | Width of line |
| lty | Style of line |
| main | Title |
| xlab | X-axis label |
| ylab | Y-axis label |
| pch | Point symbol |
| ylim | Y-axis limits |
| xlim | X-axis limits |
| grid() | Add grid |
| points() | Add points |
| lines() | Add additional lines |
| text() | Add labels |
| legend() | Add legend |
Applications of Line Graphs
- Population growth
- Stock market trends
- Monthly sales analysis
- Temperature monitoring
- Experimental data
- Machine learning loss curves
- Accuracy curves
- Sensor measurements
- Time-series analysis
Conclusion
A line graph is one of the most commonly used plots in R for representing trends and continuous data. Using the plot() function with type="l", along with lines(), points(), legend(), and customization parameters such as col, lwd, and lty, highly informative graphs can be created for scientific and statistical analysis.
- Get link
- X
- Other Apps

Comments
Post a Comment