Step Line Plot in R

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.