Line Graph in R

 

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

plot(year,
sales,
type="l",
main="Annual Sales",
xlab="Year",
ylab="Sales")




Example 3: Changing Line Color

plot(year,
sales,
type="l",
col="blue")


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 lines
  • pch=16 = filled circles



Example 6: Different Line Styles

plot(year,
sales,
type="l",
lty=2)

Common values of lty:

ltyStyle
1Solid
2Dashed
3Dotted
4Dot-Dash
5Long Dash

Example:

plot(year,
sales,
type="l",
col="green",
lwd=3,
lty=2)



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)
plot(year,
productA,
type="l",
col="blue",
ylim=c(0,40),
xlab="Year",
ylab="Sales",
main="Sales Comparison")

lines(year,
productB,
col="red")

legend("topleft",
legend=c("Product A","Product B"),
col=c("blue","red"),
lty=1)




Example 8: Monthly Temperature Variation

month <- 1:12

temp <- c(28,29,31,34,35,33,
32,31,30,29,28,27)

plot(month,
temp,
type="l",
col="red",
lwd=2,
xlab="Month",
ylab="Temperature",
main="Monthly Temperature")
grid()



Example 9: Plotting Mathematical Functions

Plot y = x²

x <- seq(-5,5,0.1)

y <- x^2

plot(x,
y,
type="l",
col="blue",
main="y=x^2")




Plot y = sin(x)

x <- seq(0,2*pi,0.1)
y <- sin(x)


plot(x,
y,
type="l",
col="red")
grid()



Example 10: Adding Grid

plot(year,
sales,
type="l",
lwd=3)

grid()

The grid improves readability.


Example 11: Adding Points

year <- c(2020,2021,2022,2023,2024)
sales <- c(15,18,22,25,30)
plot(year,
sales,
type="l")

points(year,
sales,
pch=19,
col="red")



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

year <- c(2020,2021,2022,2023,2024)

sales <- c(12,16,20,28,35)

plot(year,
sales,
type="b",
col="darkgreen",
pch=18,
lwd=3,
main="Company Sales",
xlab="Year",
ylab="Sales")

grid()



Important Parameters

ParameterPurpose
type="l"Line graph
colColor
lwdWidth of line
ltyStyle of line
mainTitle
xlabX-axis label
ylabY-axis label
pchPoint symbol
ylimY-axis limits
xlimX-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.

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