Data points are shown as a series of horizontal and vertical steps using step line plots, sometimes referred to as step plots or stair plots, which are a style of data visualisation used in R and other data analysis tools. These charts are especially helpful for displaying data, such as time series or cumulative data, that changes dramatically at precise times. In this post, we'll look at how to make step-line graphs in R, alter how they look, and analyse the data they display.
Key Features of Step Line Plots:
The Key Features of Step Line Plots are as follows:
- Horizontal Steps: In a step-line plot, the data points are connected by horizontal and vertical lines, creating a series of steps. Each horizontal step represents a constant value of the variable being plotted.
- Vertical Steps: Vertical steps occur at the transition points between data points. These vertical steps indicate that the value of the variable has changed abruptly at that point. The vertical step can be either upward or downward, depending on the change in the variable.
- Data Points: Although step line plots primarily consist of lines, you can also add data points to the plot to highlight specific values or observations. Data points are often represented as small symbols at the data points' coordinates.
Creating a Basic Step Line Plot
R
# Create random data
x <- 1:10
y <- cumsum(runif(10))
# Create a step line plot
plot(x, y, type = "s", lwd = 2, col = "blue", main = "Step Line Plot Example",
xlab = "X-axis", ylab = "Y-axis")
Output:
Step Line Plot in R- x represents the x-axis values.
- y represents the y-axis values.
- type = "s" specifies that we want to create a step line plot.
- lwd = 2 sets the line width to 2.
- col = "blue" sets the line color to blue.
- main, xlab, and ylab are used to add a title and axis labels to the plot.
Customizing the Step Line Plot
Step line plots can be customized in various ways to enhance their appearance and convey information effectively. Here are some common customization options:
Line Appearance
You can control the appearance of the step lines by adjusting parameters like lwd (line width), col (line color), and lty (line type).
R
# Customize line appearance
plot(x, y, type = "s", lwd = 2, col = "red", lty = 2)
Output:
Step Line Plot in R- The step line plot is made using the plot() function. To make a step plot, we provide type = "s", set the line width (lwd) to 2, and use the colour red (col = "red"). We also set the line type (lty) to 2, which results in dashed lines.
Adding Data Points
We can add data points to the step line plot using the points() function. This is particularly useful when we want to highlight specific data points:
R
# Add data points to the step line plot
plot(x, y, type = "s", lwd = 2, col = "blue")
points(x, y, pch = 16, col = "red")
Output:
Step Line Plot in R- The first step line plot is made using the plot() function. In order to generate a step plot, we define type = "s", set the line width (lwd) to 2, and use the colour blue (col = "blue"). Thus, the step line is produced.
- The points() function is used to add data points to the graphic. These data points are superimposed on top of the step line as red circles (pch = 16). The data points' colour was set to red (col = "red").
Adding Grid Lines
To add grid lines to the plot, use the grid() function. You can customize the appearance of grid lines using the lty and col parameters:
R
# Create random data
x <- 1:10
y <- cumsum(runif(10))
# Create a step line plot
plot(x, y, type = "s", lwd = 2, col = "blue", main = "Step Line Plot Example",
xlab = "X-axis", ylab = "Y-axis")
# Add grid
grid()
Output:
Step Line Plot in R
Create multiple step lines and add a legend
R
# Create multiple step lines and add a legend
x <- 1:10
y1 <- cumsum(runif(10))
y2 <- cumsum(runif(10))
plot(x, y1, type = "s", lwd = 2, col = "blue", ylim = c(0, max(y1, y2)))
lines(x, y2, type = "s", lwd = 2, col = "red")
legend("topright", legend = c("Line 1", "Line 2"), col = c("blue", "red"), lwd = 2)
Output:

- We generate two sets of y-values, y1 and y2, using the cumsum() function. These represent cumulative sums of random uniform values.
- We use the plot() function to create the initial plot for y1. We specify type = "s" to create a step plot, set the line width (lwd) to 2, and use the color blue (col = "blue"). We also set the y-axis limit (ylim) to ensure both lines fit within the plot.
- We add a second step line for y2 using the lines() function. This time, we use the color red (col = "red").
- To differentiate between the two lines, we add a legend to the top-right corner of the plot using the legend() function. We provide labels for each line ("Line 1" and "Line 2"), specify their respective colors and line widths, and position the legend using "topright"
Obtain Historical Stock Data
To create this example, we need to obtain historical stock price data for Apple Inc. we can use packages like quantmod to fetch this data from Yahoo Finance or other sources. Install the package if you haven't already:
R
install.packages("quantmod")
library(quantmod)
# Fetch Apple Inc. stock data for the past year
getSymbols("AAPL", from = Sys.Date() - 365, to = Sys.Date())
# Extract the closing prices from the data
apple_stock <- AAPL$AAPL.Close
# Create a date sequence for the x-axis
dates <- index(apple_stock)
# Create a step line plot
plot(dates, apple_stock, type = "s", lwd = 2, col = "blue",
xlab = "Date", ylab = "Stock Price (USD)",
main = "Apple Inc. Stock Price (Past Year)")
# Add grid lines
grid(lty = 3, col = "gray")
# Highlight significant events with vertical lines
abline(v = as.Date(c("2023-01-03", "2023-03-01", "2023-06-01", "2023-09-01")),
lty = 2, col = "red")
# Add data points at significant events
points(as.Date(c("2023-01-03", "2023-03-01", "2023-06-01", "2023-09-01")),
apple_stock[c(1, 60, 125, length(apple_stock))], pch = 19, col = "red")
# Add a legend
legend("topleft", legend = "Significant Events", col = "red", pch = 19, lty = 2)
# Add a horizontal line at the initial stock price
abline(h = apple_stock[1], lty = 2, col = "green")
# Annotate the initial stock price
text(dates[1], apple_stock[1], paste("Initial Price: $", round(apple_stock[1], 2)),
pos = 4, col = "green", cex = 0.8)
Output:

- We fetch real historical stock price data for Apple Inc.
- We create a step line plot with a thick blue line to represent daily closing prices.
- Grid lines are added to improve readability.
- Vertical red dashed lines and red data points are used to highlight significant events (e.g., earnings reports, product launches).
- A legend is added to explain the significance of the red lines and data points.
- A green dashed horizontal line represents the initial stock price, and its value is annotated on the plot.
Conclusion
In conclusion, stair plots or step line plots in R are useful for showing data with clear transitions or cumulative changes. They provide a clear illustration of how values change over time, and you may customise them to suit your needs. For time series data, cumulative data, and emphasising noteworthy occurrences or patterns in a variety of applications, including financial analysis, these graphs are helpful. Understanding step line plots will improve your data storytelling and visualisation skills in R.
Similar Reads
Step Line Plot Using R
Step line plots, also known as step plots or step charts, are a type of data visualization used to display data points that change abruptly at specific time intervals or discrete data points. They are particularly useful for showing changes over time in a visually intuitive manner. In this article,
6 min read
R - Stem and Leaf Plots
Stem and Leaf plot is a technique of displaying the frequencies with which some classes of values may occur. It is basically a method of representing the quantitative data in the graphical format. The stem and leaf plot retains the original data item up to two significant figures unlike histogram. T
6 min read
Scatter plots in R Language
A scatter plot is a set of dotted points representing individual data pieces on the horizontal and vertical axis. In a graph in which the values of two variables are plotted along the X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them. R - Scatter plots We can
4 min read
Plot Only Text in R
In this article, we will discuss how to plot only text in the R programming language. Method 1: Using par() function with the mar argument In this approach to plot only text, the user needs to call the in-built function par function with the mar argument to simply plot the empty plot in the R progra
3 min read
Line types in R
R is a popular language used by data analysts and scientists to visualize data. One of the key features of R Programming Language plotting capabilities is the ability to customize how lines appear in our plots. Line types in R have so many features in this article we will discuss all of them. What A
4 min read
Line Plot using ggplot2 in R
In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. We are going to use the R package ggplot2 which has several layers in it. First, you need to install the ggplot2 package if it is not previously installed in R Stu
6 min read
Plot Function In R
Data visualization is a crucial aspect of data analysis, allowing us to gain insights and communicate findings effectively. In R, the plot() function is a versatile tool for creating a wide range of plots, including scatter plots, line plots, bar plots, histograms, and more. In this article, we'll e
3 min read
Line Plot in R with Error Bars
A line plot is a graphical representation of a series of data. Line plots are widely used in data visualization. In we use ggplot2( ) and plot( ) function to create line plot. Error bars act as a visual enhancement that help us to see variability of the plotted data. Error Bars are used to show stan
2 min read
Set Axis Limits of Plot in R
In this article, we will be looking at the approach to set the axis limits of the plot in R programming language. Axis limit of the plot basically refers to the scaling of the x-axis and the y-axis of the given plot. Using xlim and ylim arguments of the plot function In this approach to set the axi
3 min read
How to Plot the Linear Regression in R
In this article, we are going to learn to plot linear regression in R. But, to plot Linear regression, we first need to understand what exactly is linear regression. What is Linear Regression?Linear Regression is a supervised learning model, which computes and predicts the output implemented from th
8 min read