'How to show labels inside a chart with R-plot?

I have the following chart in R:

n = 1:50
f = log2(n)
g = n
h = n*log2(n)
k = n^2
z = 2^n
w = factorial(n)
plot(n, f, ylim=c(0,200), t='l', col=3)
lines(n, g, t='l', col=2)
lines(n, h, t='l', col=3)
lines(n, k, t='l', col=4)
lines(n, z, t='l', col=5)
lines(n, w, t='l', col=6)

enter image description here

Is there a way to show the names/labels of each of the functions similar to the following figure:

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source