Skip to content
Back to formatted view

Raw Message

Message-ID: <4581DD7D.3060802@stats.uwo.ca>
Date: 2006-12-14T23:25:49Z
From: Duncan Murdoch
Subject: legend/plotmath/substitute problem
In-Reply-To: <20061214220550.GA11605@gsf.de>

On 12/14/2006 5:05 PM, Philipp Pagel wrote:
> 	Dear R Experts,
> 
> I am trying to produce a legend for a series of plots which are
> generated in a loop. The legend is supposed to look like this:
> 
> 2000: gamma=1.8
> 
> where gamma is replaced by the greek letter and both the year and the
> value of gamma are stored in variables.
> 
> Everything works fine as long as I have only one data series:
> 
> year = 2001
> g = 1.9
> plot(1)
> legend('top', legend=substitute(paste(year, ': ', gamma, '=', g), list(year=year, g=g)) )
> 
> 
> My problem starts, when I want to put more than one series of data in
> the plot and accordingly need one legend row per data series:
> 
> year1 = 2001
> year2 = 2005
> g1 = 1.9
> g2 = 1.7
> plot(1)
> legend('top', 
> 	legend=c(
> 		substitute(paste(year, ': ', gamma, '=', g), list(year=year1, g=g1)),
> 		substitute(paste(year, ': ', gamma, '=', g), list(year=year2, g=g2))
> 	)
> )
> 
> This obviously does not produce the desired result. Apparently, I am not
> generating a list of expressions, as intended. So I thought, maybe R uses a
> variety of the recycling rule here and tried:

The problem is that legend wants an expression, but substitute() isn't 
returning one, it's returning a call, and c(call1,call2) produces a list 
of two calls, not an expression holding two calls.  So the following 
would work, but there might be something more elegant:

year1 = 2001
year2 = 2005
g1 = 1.9
g2 = 1.7
plot(1)
legend('top',
	legend=c(
		as.expression(substitute(paste(year, ': ', gamma, '=', g), 
list(year=year1, g=g1))),
		as.expression(substitute(paste(year, ': ', gamma, '=', g), 
list(year=year2, g=g2)))
	)
)

Duncan Murdoch

> 
> year = c(2001, 2005)
> g = c(1.9, 1.7)
> plot(1)
> legend('top',
>     legend=list(
>         substitute(paste(year, ': ', gamma, '=', g), list(year=year, g=g)),
>     )
> )
> 
> No succes, either...
> 
> I have read and re-read the documentation for legend, expression, substitute
> and plotmath but can't figure it out. Even drinking a cup of tea prepared from
> fine-cut man page printouts didn't lead to satori.
> 
> I'm probably missing something simple. Any hints are highly appreciated.
> 
> Thanks
> 	Philipp
>