[R] Titles changing when a plot is redrawn

Yihui Xie xie at yihui.name
Thu Oct 6 05:49:24 CEST 2011


I think the problem is your str1 is an unevaluated expression and will
change with the value of i. You should be able to get a fixed title by
this:

par(mfrow = c(2, 1))
for (i in 1:2) {
    x <- 1:100
    rmse <- sin(x/5)  # fake data
    plot(x, rmse, main = substitute(list(RMSE(theta), i == z), list(z = i)))
}

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



On Wed, Oct 5, 2011 at 10:01 PM, John Nolan <jpnolan at american.edu> wrote:
> I ran into a problem with titles on graphs.  I wanted a graph with
> multiple subplots, with each having a title that involved both
> a Greek letter and an identifier for each graph.  Below is a
> simplified version of code to do this.  The graph appears fine,
> with the first graph having "i=1" in the title, and the second
> graph having "i=2" in the title.  However, when I resize the graph,
> the plot titles change, with both showing "i=2". The titles also
> change when I save the plot to a file using the "File" menu,
> then "Save as" in Windows.  Is this what should happen?  I
> always thought that titles are static once the graph is
> drawn, and couldn't change.
>
> The problem occurs on some version of R, but not on others.
> It does occur with the latest version of R:
>> str(R.Version())
> List of 13
>  $ platform      : chr "i386-pc-mingw32"
>  $ arch          : chr "i386"
>  $ os            : chr "mingw32"
>  $ system        : chr "i386, mingw32"
>  $ status        : chr ""
>  $ major         : chr "2"
>  $ minor         : chr "13.2"
>  $ year          : chr "2011"
>  $ month         : chr "09"
>  $ day           : chr "30"
>  $ svn rev       : chr "57111"
>  $ language      : chr "R"
>  $ version.string: chr "R version 2.13.2 (2011-09-30)"
>
> The problem also occurs on:  R 2.13.0 on Win32
>  and Mac (R 2.12.0, x86_64-apple-darwin9.8.0)
> The problem DOES NOT occur under R 2.10.0 on Win32.
>
> If the code below is bracketed with pdf("test.pdf")
> and dev.off(), the correct labels appear in the file.
> This behavior doesn't seem to appear if there is only
> one plot.
>
> My guess is that the titles are being reevaluated when
> the plot is redrawn, and since the value of i is 2 when
> the redraw occurs, both labels get set to "i=2".  I guess
> "Save as" forces a redraw because a dialog box pops up?
>
> If could be that this behavior is what is intended, and that
> somewhere between R 2.10.0 and R 2.13.2 an old bug was fixed.
> Or this behavior is not what was intended, and a bug was
> introduced.  If the former, this should be explained to the user
> somewhere.  If the latter, can someone track it down and fix?
>
> John Nolan
>
> #-------------------------------------------------
> par(mfrow=c(2,1))
> for (i in 1:2) {
>  x <- 1:100
>  rmse <- sin(x/5)  # fake data
>  plot(x,rmse)
>  str1 <- bquote( paste("RMSE(",theta,"), ",i==.(i)  ))
>  title( str1 )
> }
> #-------------------------------------------------
>
>
>  ...........................................................................
>
>  John P. Nolan
>  Math/Stat Department
>  227 Gray Hall
>  American University
>  4400 Massachusetts Avenue, NW
>  Washington, DC 20016-8050
>
>  jpnolan at american.edu
>  202.885.3140 voice
>  202.885.3155 fax
>  http://academic2.american.edu/~jpnolan
>  ...........................................................................
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



More information about the R-help mailing list