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?