A line chart, connects a series of data points using a line. Line chart also displays sequential values to help you identify trends. In business, Line chart is widely used to display the time series data.
Have a look at the line chart for Air passengers data set. This line chart clearly shows an upward trend for number of air passengers.
plot(AirPassengers)
Here is another line chart. Nile data set is about measurements of annual flow of the river Nile. You can see there is no apparent trend, however on an average the river flow from 1920 to 1960 has remained steady.
plot(Nile)
In both these examples, the X-axis is used to map the time (and the time can be in days, months, years and even in weeks) and Y-axis is used to map the time measurements.
In this article, we will learn about line charts. In particular we will discuss.
- How to create single line chart in R?
- How to customize the line chart?
- How to create multiple line charts in R?
Let’s get started!
How to create single line chart in R?
Let’s prepare a simulated share price data of a company. The price indicates closing price of a stock on the trading day.
price_closing <- c(1702.40, 1677.60, 1666.00,
1684.60, 1679.80, 1669.65,
1693.20, 1693.70, 1729.25,1686.75)
Let’s generate time series object
my_date <- as.Date(c("16/1/2024","15/1/2024","14/1/2024","13/1/2024",
"12/1/2024","11/1/2024","10/1/2024","09/1/2024",
"08/1/2024","07/1/2024"),'%d/%m/%Y')
Let’s create a data frame
# column wise bind two objects to create data frame
price_closing_df <- data.frame(my_date,price_closing)
head(price_closing_df)
We will use plot()
function to generate line plot. To create line plots, we only need to set type = "l"
when calling plot()
function.
plot(x = price_closing_df$my_date, y = price_closing_df$price_closing,type="o",xlab= "Date",
ylab="clsoing share price")
The following values are possible for type
- type=”p” for points
- type=”l” for lines
- type=”b” for both points and lines
- type=”s” for steps
- type=”n” does not produce any points or lines
- type=”h for histogram line vertical line
How to customize the line chart
In the plot
function, lty
is used to specify the line type of a line plot. The following chart shows the preview of six line types supported by R:
lty_values <- 1:6
plot(lty_values, type="n",axes=FALSE,ann=FALSE)
abline(h=lty_values,lty=lty_values,lwd=2)
mtext(lty_values,side=2,at=lty_values)
title("Line Types(lty)")
The abline()
is used to draw the horizontal lines with different line types but of equal line width (lwd
=2). The mtext()
function is used to draw the text on margin. In the plot
function, type=n
will produce empty canvas with proper axes ranges and axes=FALSE,ann=FALSE
turns off axes labels and chart annotations.
Let’s produce a line chart again, this time with different line type. lty=2
will produce dotted line chart as below.
plot(x = price_closing_df$my_date, y = price_closing_df$price_closing,type="o",lty=2,xlab= "Date",
ylab="clsoing share price")
We can change the color of a line chart using col
argument.
plot(x = price_closing_df$my_date, y = price_closing_df$price_closing,type="o",lty=1,xlab= "Date",
ylab="clsoing share price",col="blue")
The following example demonstrates how abline()
can be useful to draw auxiliary lines in a plot. Suppose we want to draw the mean value and the range of closing share price. We can easily draw these auxiliary lines with different line types and colors.
plot(x = price_closing_df$my_date, y = price_closing_df$price_closing,type="o",lty=1,xlab= "Date",
ylab="clsoing share price",col="blue")
abline(h=mean(price_closing_df$price_closing),lty=2,col="red",lwd=2)
abline(h=range(price_closing_df$price_closing),lty=2,
col="brown",lwd=2)
How to create multiple line charts?
we can use lines()
function to draw more than one line on the same chart. So let’s generate simulated data of high stock price.
# Generate simulated data of high stock price of a company on trading day.
price_high <- c(1708.95, 1686.75, 1668.80, 1689.00,
1692.50, 1692.00, 1706.20, 1729.25,
1731.00, 1700.50)
price_closing_df1 <- data.frame(my_date,price_closing,price_high)
head(price_closing_df1)
We will first generate line chart using plot
function and add second line chart over it using lines
function and apply different colors to distinguish between the two lines.
plot(x = price_closing_df1$my_date, y = price_closing_df1$price_closing,type="o",lty=1,xlab= "Date",
ylab="clsoing share price",col="blue",ylim=c(1600,1750))
# add second line on the same plot generated by preceding code
lines(x=price_closing_df1$my_date,y=price_closing_df1$price_high,type="o",col="red")
We can also create a legend at the bottom left corner using colors mentioned in the line chart.
Summary
Line charts are useful to show the trends in time series data. In this article we learned:
- How to create line chart using
plot
function. - Using
plot
function we can generate various types of plots usingtype
argument - How to customize the line chart to display additional information, such as mean and median values?
- How to create multi-series line charts with a legend