Re: [R] Titles changing when a plot is redrawn
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 -xieyi...@gmail.com wrote: - To: John Nolan From: Yihui Xie Sent by: xieyi...@gmail.com Date: 10/05/2011 11:49PM Cc: r-help@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 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 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 > > jpno...@american.edu > 202.885.3140 voice > 202.885.3155 fax > http://academic2.american.edu/~jpnolan > ... > __ > R-help@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. > __ R-help@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.
[R] Titles changing when a plot is redrawn
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 jpno...@american.edu 202.885.3140 voice 202.885.3155 fax http://academic2.american.edu/~jpnolan ... __ R-help@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.
Re: [R] [Rd] Probability of data values form empirical distribution
The function ecdf(x) computes the empirical cdf from data in the vector x. You can plot it with plot(ecdf(x)), or compute the emp. cdf at new values, e.g. my.cdf <- ecdf(x) my.cdf( 0:3 ) # computes the emp. cdf at 0,1,2,3 John ... John P. Nolan Math/Stat Department 227 Gray Hall American University 4400 Massachusetts Avenue, NW Washington, DC 20016-8050 jpno...@american.edu 202.885.3140 voice 202.885.3155 fax http://academic2.american.edu/~jpnolan ... -r-devel-boun...@r-project.org wrote: - To: R help From: Lina Rusyte Sent by: r-devel-boun...@r-project.org Date: 09/29/2009 04:51AM Cc: R devel Subject: [Rd] Probability of data values form empirical distribution Hello, à Could someone help me please and to tell how to get the probability from empirical distribution (not parametric) for each data value (R function). For example, for normal distribution there is such a function like: à ââ¬Åpnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)ââ¬Â à I need the same function only for the empirical distribution (which does not correspond to any typical distribution). R plots the density function of any data: à ââ¬Åplot(density(x))ââ¬Â à I need to find out the probability for each data value from this plot-line. à Best regards, Lina [[alternative HTML version deleted]] __ r-de...@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel [[alternative HTML version deleted]] __ R-help@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.
[R] calling a C function with a struct
I am trying to call a precompiled C function that uses a struct as one of it's arguments. I could write a wrapper function in C, but I was hoping there is some way to pack fields into an array of type raw that could be passed directly to the function. Here is some more detail. The C struct is simple, but has mixed types: struct STRUCT1 { long type; long nx; double *x; double a; double b; }; typedef struct STRUCT1 STRUCT1_TYPE; The C function header is void func1( long method, STRUCT1 my_struct, double *output); I would like to have an R list mimicking the C struct, and then use .C to call func1 with this information, e.g. my.struct <- list(type=3,nx=5,x=1:5,a=2.5,b=8.3) my.func1( 3, convert2raw( my.struct ), ) where R function convert2raw would return a vector of type raw with the fields of my.struct packed into memory just like STRUCT1, and then I could call func1 with that vector of raws. Can I write a convert2raw( ) function and then use my.func1 <- function( method, buf ) { a <- .C("func1", as.integer(method), as.raw(buf) , output=double(1) ) return(a$output) } John ... John P. Nolan Math/Stat Department 227 Gray Hall American University 4400 Massachusetts Avenue, NW Washington, DC 20016-8050 [EMAIL PROTECTED] 202.885.3140 voice 202.885.3155 fax http://academic2.american.edu/~jpnolan ... [[alternative HTML version deleted]] __ R-help@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.