[R] Titles changing when a plot is redrawn

William Dunlap wdunlap at tibco.com
Thu Oct 6 21:07:49 CEST 2011


You can work around the problem by making each
call to bquote() in a different environment, each
containing its own value of 'i'.  E.g.,

par(mfrow=c(2,1))
for(i in 1:2) {
    x <- 1:100
    rmse <- sin(x/5)  # fake data
    plot(x,rmse)
    str1 <- local({ i <- i ; bquote( paste("local RMSE(",theta,"), ",i==.(i)  ))})
    title( str1 )
}

or

par(mfrow=c(2,1))
lapply(1:2, function(i) {
    x <- 1:100
    rmse <- sin(x/5)  # fake data
    plot(x,rmse)
    str1 <- bquote( paste("lapply RMSE(",theta,"), ",i==.(i)  ))
    title( str1 )
})

I expect that this sort of workaround would continue to
work after the underlying problem has been fixed.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
> Sent: Thursday, October 06, 2011 11:49 AM
> To: David Winsemius
> Cc: r-help at r-project.org; John Nolan
> Subject: Re: [R] Titles changing when a plot is redrawn
> 
> 
> On Oct 6, 2011, at 2:29 PM, David Winsemius wrote:
> 
> >
> > On Oct 6, 2011, at 8:25 AM, John Nolan wrote:
> >
> >> Thank you for telling me a fix.
> >>
> >> But I still don't know if this behavior is what is intended.  I
> >> used bquote(...) because the plotmath(...) help page refers to
> >> bquote and gives an example like this.  I suspect most users will
> >> be baffled by this kind of behavior, especially since it does not
> >> occur when there is one plot.  By this I mean that I can draw one
> >> plot and title it with the same string using bquote( ).
> >
> > It seems to be an infelicity that is not reproducible on Macs:
> >
> > <RMSE.titles.pdf>
> 
> Apologies, I take it back. I read you original post incorrectly and
> thought you were having problems with the original plot appearing
> incorrectly. When I do the the resizing I do see the same unexpected
> change to both titles having " i = 2 "
> >
> >
> >> If I change the value of i, and redraw the graph, the redrawn graph
> >> has the original value of i in the title, not the updated value. So
> >> in this case, an unevaluated expression is not re-evaluated at draw
> >> time?
> >
> > I certainly would not have expected redrawing to call any R code
> > again. I would have expected the graphics device to do the
> > recalculations.
> 
> As I said. I would not have expected this, either. I seem to remember
> this being brought up before, but I was unable to find it on a search.
> >
> > --
> > David.
> >
> >>
> >> John
> >>
> >>
> >> -----xieyihui at gmail.com wrote: -----
> >> To: John Nolan <jpnolan at american.edu>
> >> From: Yihui Xie
> >> Sent by: xieyihui at gmail.com
> >> Date: 10/05/2011 11:49PM
> >> Cc: r-help at r-project.org
> >> Subject: Re: [R] Titles changing when a plot is redrawn
> >>
> >> 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 )
> >>> }
> >>> #-------------------------------------------------
> >>>
> 
> 
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> 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