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 ...........................................................................
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. >
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( ). 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? 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 ) > } > #------------------------------------------------- > > > ........................................................................... > > 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. >