[R] Superscript in legend without using expression function

160 views
Skip to first unread message

jgui001

unread,
Feb 7, 2015, 5:21:06 PM2/7/15
to r-h...@r-project.org
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])))



--
View this message in context: http://r.789695.n4.nabble.com/Superscript-in-legend-without-using-expression-function-tp4702929.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Rolf Turner

unread,
Feb 7, 2015, 5:56:08 PM2/7/15
to jgui001, r-h...@r-project.org
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

David Winsemius

unread,
Feb 7, 2015, 10:20:29 PM2/7/15
to Rolf Turner, jgui001, r-h...@r-project.org

On Feb 7, 2015, at 2:54 PM, Rolf Turner wrote:

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

This might be a bit more compact:

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

I didn't see an indication that there were supposed to be subscripted numerals after the "G"'s

Initialy I tried just:

lapply(1:3, function(n) bquote( G*.(n)~r^2==.(r1[n])))

But this didn't have the proper mode, which was solved by wrapping in as.espression thus returning the correctly constructed expression vector:

expression(G * 1L ~ r^2 == 0.59,
G * 2L ~ r^2 == 0.9,
G * 3L ~ r^2 == 0.6)

This could also be delivered slightly less economically with this editing of your effort:

as.expression(c( bquote(G[1]~~ r^2 == .(r1[1])),
bquote(G[2]~~ r^2 == .(r1[2])),
bquote(G[3]~~ r^2 == .(r1[3]))
)
)

I'm not sure but I think that expressions can be vectors but I don't think that there are "call vectors", only call lists.


> 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
>
> ______________________________________________
> R-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

Gabor Grothendieck

unread,
Feb 8, 2015, 7:10:57 PM2/8/15
to jgui001, r-h...@r-project.org
On Sat, Feb 7, 2015 at 4:57 PM, jgui001 <j.gui...@auckland.ac.nz> 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])))

Replace the legend statement with:

leg <- as.list(parse(text = sprintf("G%d~r^2=%.2f", 1:3, r1)))
legend("topleft", legend = leg)



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

jgui001

unread,
Feb 10, 2015, 1:37:34 PM2/10/15
to r-h...@r-project.org
Cheers Guys it worked!




--
View this message in context: http://r.789695.n4.nabble.com/Superscript-in-legend-without-using-expression-function-tp4702929p4703036.html
Sent from the R help mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages