How to include arrow symbols in R plot legend?

I’m working on a visualization project in R and I need help adding arrow symbols to my legend. I’ve created a plot with two curves and several red arrows that show the differences between data points. The arrows are an important part of my analysis, but I can’t figure out how to represent them in the legend.

Here’s my sample data:

time_vals <- seq(0.1, 10, 0.1)
base_data <- c(2.5e-12, 0.25, 42.1, 220.5, 485.2, 695.8, 812.3, 865.4, 871.2, 848.9, 806.1, 762.4, 719.6, 678.2, 638.5, 600.3, 564.1, 529.7, 497.8, 468.1, 440.4, 414.5, 390.2, 367.4, 346.0, 325.8, 306.7, 288.6, 271.4, 255.0, 239.4, 224.5, 210.2, 196.5, 183.4, 170.8, 158.7, 147.1, 135.9, 125.2, 114.9, 105.0, 95.5, 86.4, 77.6, 69.2, 61.1, 53.4, 46.0, 38.9)
adjusted_data <- c(3.1e-12, 0.31, 51.8, 271.3, 597.2, 840.1, 984.2, 1048.3, 1055.1, 1029.8, 979.5, 926.4, 873.2, 822.1, 773.4, 727.0, 683.5, 642.7, 604.3, 568.1, 534.0, 501.8, 471.4, 442.6, 415.3, 389.4, 365.0, 341.8, 319.8, 298.9)

And here’s how I create the plot:

par(mar=c(5,5,3,2))
plot(time_vals[1:30], adjusted_data, type="l", lty=2, lwd=2, 
     xlab="Distance (km)", ylab="Concentration (μg/m³)", 
     col="black", cex.axis=1.2, cex.lab=1.2)
lines(time_vals[1:30], base_data[1:30], lty=1, lwd=2, col="black")

# Add red arrows showing differences
arrow_positions <- c(5, 10, 15, 20, 25)
for(i in arrow_positions) {
  arrows(time_vals[i], base_data[i], time_vals[i], adjusted_data[i], 
         col="red", lwd=2, length=0.1, angle=20)
}

# Current legend (missing arrows)
legend("topright", c("Upper bound", "Lower bound"), 
       lty=c(2,1), lwd=c(2,2), col=c("black","black"), cex=1.1)

The red arrows are crucial for showing the magnitude of differences between my two datasets. How can I add these arrows as a third element in my legend so viewers understand what the red arrows represent?

Had this exact issue last year when making plots with directional indicators. Use the pch parameter with Unicode arrow characters in your legend function. Add a third legend entry like this:

legend("topright", c("Upper bound", "Lower bound", "Difference"), 
       lty=c(2,1,NA), lwd=c(2,2,NA), col=c("black","black","red"), 
       pch=c(NA,NA,"↑"), cex=1.1)

The pch="↑" creates the arrow symbol. Set lty=NA and lwd=NA for the arrow entry since it doesn’t need line properties. Or use pch=24 for a filled triangle pointing up - might render better across different systems than Unicode characters. Worked great for my environmental data where arrows showed flow directions.

Use points() to draw a custom arrow in the legend. Add the points() call after your legend with coordinates that match where you want it. Try points(x_pos, y_pos, pch="→", col="red", cex=1.1) where x_pos and y_pos are your legend coordinates. Takes some tweaking to get the positioning right, but it works great once you dial it in.

The segments() function with a custom legend works great for this. Make your legend normally, then add a segments() call right after to manually draw a small arrow in the legend area. Use something like segments(x0, y0, x1, y1, col=“red”, lwd=2) then arrows(x1, y1, x1, y1+offset, col=“red”, length=0.05). You’ll need to play with the coordinates to get the positioning right in your legend box. This method gives way more control over how arrows look than pch symbols, especially when you want your legend arrow to match your plot arrows perfectly - same thickness and style.