Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fraction in legend, multiple colors

Tags:

graph

r

I need to create a complex legend, which includes a fraction and the numerator and denominator are in different colors Legend.

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?!

like image 636
ECII Avatar asked Apr 09 '12 00:04

ECII


1 Answers

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)

enter image description here

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))

enter image description here

which I know is not what you really want, but just in case no better hack comes...

like image 193
Geek On Acid Avatar answered Sep 24 '22 12:09

Geek On Acid