[R] How to dynamically display title for a graph

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jan 4 21:10:20 CET 2010


On 04/01/2010 11:30 AM, noclue_ wrote:
> I have written  a function (see below) which encloses a boxplot.  My function
> "xbox" takes in a variable name (such as "age"), and do a boxplot. Now I
> would like to add a title for the graph dynamically displaying the variable
> name as part of title. 
>
> But, in reality, the title displays VALUES of my variable.
>
> Obviously, sprintf does not work. 
>
> Can somebody shed light on how to solve my problem in properly displaying a
> title ?
>
> Thanks.
>
> -----------------------------------------------------------------
>
>  xbox<- function(X)
>  {
>       boxplot(_expenses ~ X, data=my_tab,main=sprintf("title is %s",X))
>  }
>  
>  xbox(age)
>   


Use "deparse(substitute(X))" in the sprintf.  This says:  get the 
expression passed as X, and convert it to text.

Duncan Murdoch



More information about the R-help mailing list