I need to create a complex legend, which includes a fraction and the numerator and denominator are in different colors .
Is it possible to create such a legend with (base) R?
Since I have to add this legend in several plots different plots, I do not want to manually program the text, but be able to add it automatically as the legend. It does not necessary have to be a legend (although it would be convenient), however I don't want to have to manually enter the coordinates of each element.
Any ideas?!
Assuming that you have something like:
d=1:10
plot(d,type="l")
and you wouldn't need different colors for numerator and denominator, you could do this with one-liner (including text location hint by @CarlWitthoft):
text(0.5*max(d), 0.9*max(d), expression(Result == frac(Green, Blue)), cex=1.5)
but there is no straightforward way to change colour of numerator and denominator. So the clunky way is to customize each element separately:
text(0.4*max(d), 0.9*max(d), "Result =", cex=1.5)
text(0.55*max(d), 0.93*max(d), "Green", col="green", cex=1.5)
text(0.55*max(d), 0.87*max(d), "Blue", col="blue", cex=1.5)
segments(0.5*max(d), 0.9*max(d), 0.6*max(d), 0.9*max(d))
which I know is not what you really want, but just in case no better hack comes...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With