Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing an "arrow" in the legend of an R plot

Tags:

plot

r

I have a "curve" and an upright positioned "arrow" in an R plot (see R code Below).

I was wondering if it might be possible to show an actual arrow in the "legend" rather than a smooth, straight line to distinguish my arrow from the curve?

Here is my R code:

curve(dnorm(x), -4, 4, lwd = 3)

arrows(2, 0, 2, .3, code = 2, lwd = 3, col = 'red4')

legend("topleft", legend = c("Curve", "Arrow"), lwd = 3, col = c(1, "red4"))
like image 245
rnorouzian Avatar asked Mar 27 '17 17:03

rnorouzian


1 Answers

Here's how to put a right arrow in your legend in place of the line. You have to access font = 5 with par to get an arrow symbol.

curve(dnorm(x), -4, 4, lwd = 3)
arrows(2, 0, 2, .3, code = 2, lwd = 3, col = 'red4')
legend("topleft", legend = c("Curve", "Arrow"), pch = c(NA, NA),
       lwd = 3, col = c(1, "red4"),
       lty = c(1, NA)) #normal legend. Do not plot line
par(font = 5) #change font to get arrows
legend("topleft", legend = c(NA, NA), pch = c(NA, 174),
       lwd = 3, col = c(1, "red4"),
       lty = c(1, NA), bty = "n") 
par(font = 1) #back to default

enter image description here

like image 156
Pierre Lapointe Avatar answered Oct 08 '22 11:10

Pierre Lapointe