R Line Graph

A line graph is a chart that is used to display information in the form of a series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from beginning to end. The graph represents different values as it can move up and down based on the suitable variable.

R – Line Graphs

The plot() function in R is used to create the line graph.

Syntax: plot(v, type, col,main, xlab, ylab)

Parameters:
v: This parameter is a contains only the numeric values
type: This parameter has the following value: 
    “p” : This value is used to draw only the points.
    “l” : This value is used to draw only the lines.
    “o”: This value is used to draw both points and lines
xlab: This parameter is the label for x axis in the chart.
ylab: This parameter is the label for y axis in the chart.
main: This parameter main is the title of the chart.
col: This parameter is used to give colors to both the points and lines.

Line Color

The line color is black by default. To change the color, use the col parameter:

Example
plot(1:10, type="l", col="blue")

Line Width

To change the width of the line, use the lwd parameter (1 is default, while 0.5 means 50% smaller, and 2 means 100% larger):

Example
plot(1:10, type="l", lwd=2)

Line Styles

The line is solid by default. Use the lty parameter with a value from 0 to 6 to specify the line format.

For example, lty=3 will display a dotted line instead of a solid line:

Example
plot(1:10, type="l", lwd=5, lty=3)

Available parameter values for lty:0 removes the line
1 displays a solid line
2 displays a dashed line
3 displays a dotted line
4 displays a "dot dashed" line
5 displays a "long dashed" line
6 displays a "two dashed" line

# Create the data for the chart.
x <- c(101,102,103,104,105)
y <-c(40,60,70,80,45)

plot(x,y,col='red',type='l',lwd=2,lty=3)
Output:

Creating a Simple Line Graph

# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)

# Plot the line graph
plot(v, type = "l")

output:

Adding Title, Color and Labels in Line Graphs in R

Approach: To create a colored and labeled line chart. Take all parameters which are required to make line chart by giving a title to the chart and add labels to the axes.
We can add more features by adding more parameters with more colors to the points and lines.

# Create the data for the chart.
x <- c(101,102,103,104,105)
y<-c(40,60,70,80,45)

# Plot the line graph
plot(x,y, type = "o", col = "green",
xlab = "Subject Code", ylab = "Mark",
main = "Subject/Mark")

Output:

Multiple Lines

To display more than one line in a graph, use the plot() function together with the lines() function:

Example
line1 <- c(1,2,3,4,5,10)
line2 <- c(2,5,7,8,9,10)

plot(line1, type = "l", col = "blue")
lines(line2, type="l", col = "red")
Output:

Example: Line graphs
# Create the data for the chart.
sc <- c(101,102,103,104,105)
s1 <-c(40,60,70,80,45)
s2 <-c(50,60,75,60,35)
s3 <-c(45,65,80,75,55)

plot(sc,s1,col='red',type='l',lwd=2,main="sub/mark",xlab='sub',ylab='marks')
lines(sc,s2,col='green',type='l',lwd=2)
lines(sc,s3,col='blue',type='l',lwd=2)

Output:



Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types