[R] Superscript in legend without using expression function

Rolf Turner r.turner at auckland.ac.nz
Sat Feb 7 23:54:37 CET 2015


On 08/02/15 10:57, jgui001 wrote:
> I am plotting three sets of data on a single graph, and doing around 100+
> graphs.
> I can use the expression function to superscript the 2 but that seems to
> force me to manually put in the R squared values. Is there away around this?
>
> This code will show what it should look like this but with the 2
> superscripted
>
> r1<-c(0.59,0.9,0.6)
> plot(1:6)
> legend("topleft",
> legend=c(paste("G1 r=",r1[1]), paste("G2 r=",r1[2]), paste("G3 r=",r1[3])))

One way of accomplishing this is:

r1<-c(0.59,0.9,0.6)
l3 <- c(as.expression(bquote(G[1]~~ r^2 == .(r1[1]))),
         as.expression(bquote(G[2]~~ r^2 == .(r1[2]))),
         as.expression(bquote(G[3]~~ r^2 == .(r1[3]))))
plot(1:6)
legend("topleft",legend=l3)

Don't ask me to explain how this works.  I just hammered and hoped till 
the desired results were produced.

There are other ways, I think, some of which may be less prolix. Someone 
else may chime in and suggest a better way, but I think that the 
foregoing does what you want.

cheers,

Rolf Turner

-- 
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619



More information about the R-help mailing list