Open In App

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:



Creating a Basic Step Line Plot




# 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

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).




# Customize line appearance
plot(x, y, type = "s", lwd = 2, col = "red", lty = 2)

Output:

Step Line Plot in R

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:




# 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

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:




# 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




# 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:

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:




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:

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.


Article Tags :