Re: [R] Draw a circle on a filled.contour() plot
Use my function filled.next: filled.next - function(fun) { mar.orig - mar - par(mar) w - (3 + mar[2]) * par(csi) * 2.54 layout(matrix(c(2, 1), nc = 2), widths = c(1, lcm(w))) mar[4] - 1 par(mar = mar) par(mfg=c(1,2)) par(new=TRUE) fun par(mar = mar.orig) } # then filled.contour(y,x,z,levels=seq(0.02,1.0,len=50), color.palette=colorRampPalette(c(blue,yellow,red)), title=title(main=,xlab=,ylab=)) filled.next(symbols(0.62,0.0,circles=c(2.5),add=TRUE,inches=FALSE)) With plot function in filled.next, don't forget to add xaxs='i', yaxs='i'... Jean Coursol -- Quoting Renaud Lancelot [EMAIL PROTECTED]: Try levelplot in package lattice instead of filled.contour: you will be able to annotate the plot using the grid package. Moreover, you can remove the key using the colorkey argument. library(lattice) library(grid) z - as.matrix(read.table(hist_2d)) dimnames(z) - NULL Data - expand.grid(x = seq(-4.45, 4.45, len = 90), y = seq(-4.45, 4.45, len = 90)) Data$z - c(z) rm(z) Cols - colorRampPalette(c(white, blue,yellow,red)) levelplot(z ~ x * y, data = Data, aspect = 1, colorkey = FALSE, col.regions = Cols(6), panel = function(...){ panel.levelplot(...) grid.circle(x = 0.62, y = 0, r = 2.5, default.units = native) }) Best, Renaud 2006/12/17, Webmaster [EMAIL PROTECTED]: The output produced by 'filled.contour' is actually a combination of two plots; one is the filled contour and one is the legend. Two separate coordinate systems are set up for these two plots, but they are only used internally - once the function has returned these coordinate systems are lost. If you want to annotate the main contour plot, for example to add points, you can specify graphics commands in the 'plot.axes' argument. An example is given below. Thanks! I now have another question (hopefully the last) for which I couldn't find an answer in the mailing-list archives: how can I get rid of the color key on the contour.filled() plot? I that question, unanswered, in a post one year ago on this same list, so I hope it's by any mean possible... (I think it's possible to change the code completely and use image(), but it's much less pretty). FX __ R-help@stat.math.ethz.ch 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. -- Renaud LANCELOT Département Elevage et Médecine Vétérinaire (EMVT) du CIRAD Directeur adjoint chargé des affaires scientifiques CIRAD, Animal Production and Veterinary Medicine Department Deputy director for scientific affairs Campus international de Baillarguet TA 30 / B (Bât. B, Bur. 214) 34398 Montpellier Cedex 5 - France Tél +33 (0)4 67 59 37 17 Secr. +33 (0)4 67 59 39 04 Fax +33 (0)4 67 59 37 95 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] tcl/tk bind destroy event
Perhaps Destroy key is unknown by Tcl; it is not in the Event modifiers table in Welch Book... But try with Control-L or Shift-Control_L, it runs (but not with Control_L-Shift ??). Use xmodmap to see the current mappings from keys to modifiers. Jean Coursol On Wed, 2 Aug 2006, Franco Mendolia wrote: Hello! I want to create a messagebox whenever the user wants to destroy the window (e.g. Alt-F4 or the 'x' in the right top corner) and ask if a modified file should be saved or not. If 'cancel' is chosen then nothing should happen and the windows still should be existing. This doesn't work. When I press cancel the window will be destroyed although. I also implemented a menu item 'Quit' where I show the same messagebox and there it works fine. How can I make it work or is there another method to do this? I'm very new to R and tcl/tk. Here is part of my code: exitProg - function() { returnVal - tkmessageBox(title=Question, message=Save modified file?, icon=question, type=yesnocancel, default=yes) returnVal - as.character(returnVal) if( returnVal == yes ) { # save file value - saveFile() # destroy window when save was successfull if( value == 1 ) tkdestroy(mw) } if( returnVal == no ) { tkdestroy(mw) } if( returnVal == cancel ) { # do nothing cat(Cancel was pressed.\n) } } # bind the destroy event in order to show a message box tkbind(mw,Destroy,exitProg) # menu item which works fine tkadd(fileMenu, command, label=Quit, command=exitProg) Thank you. Franco Mendolia __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] splitting and saving a large dataframe
for( i in levels(mydata$myfactor) ) { d = mydata[mydata$myfactor==i,] write.table(d, paste('d', i, '.data', sep='') ) } Jean Coursol Quoting Dimitri Szerman [EMAIL PROTECTED]: Hi, I searched for this in the mailing list, but found no results. I have a large dataframe ( dim(mydata)= 1297059 16, object.size(mydata= 145280576) ) , and I want to perform some calculations which can be done by a factor's levels, say, mydata$myfactor. So what I want is to split this dataframe into nlevels(mydata$myfactor) = 80 levels. But I must do this efficiently, that is, I can't actually split the dataframe and then save the resulting 80 smaller ones, as I would run out of memory. I don't know how to do it though. So, can anyone give a hint on how to get to get one smaller dataframe, save it into a file, remove it from the workspace, get the next smaller dataframe, save it into another file, remoe it from the workspace, and so on? Thank you, Dimitri [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] locator() via tcltk
To illustrate (?) Professor Ripley comments, I send you an example (stolen in various places...). Note the tkwm.resizable(tt, 0, 0) directive that prevents the window rescaling (if not,the coordinates will not be correct). # # Getting the mouse coords with TclTk # # Two possibilities: tkrplot package or Img library # # A) USING the tkrplot package # # 1a) Opening an X11 device named XImage (current device) X11(XImage,width=600, height=600) # 2a) Putting a contour... x - 10*(1:nrow(volcano)) y - 10*(1:ncol(volcano)) image(x, y, volcano, col = terrain.colors(100), axes = FALSE) contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, col = peru) axis(1, at = seq(100, 800, by = 100)) axis(2, at = seq(100, 600, by = 100)) box() title(main = Maunga Whau Volcano, font.main = 4) # image dimensions (before XImage deletion) parPlotSize - par('plt') usrCoords - par('usr') # 3a) tkrplot loading (that load tcltk package) #The tcltk directive extended by tkrplot # image create Rplot truc #put the XImage image in the image Tcl objet named truc #and close XImage library(tkrplot) .Tcl(image create Rplot truc) # # B) USING the Tcl Img library (for .jpg and .png) # 1b) Opening a jpeg (or png) device jpeg(monimage.jpg,width=800, height=800) # 2b) = 2) + closing jpeg device x - 10*(1:nrow(volcano)) y - 10*(1:ncol(volcano)) image(x, y, volcano, col = terrain.colors(100), axes = FALSE) contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, col = peru) axis(1, at = seq(100, 800, by = 100)) axis(2, at = seq(100, 600, by = 100)) box() title(main = Maunga Whau Volcano, font.main = 4) # image dimensions parPlotSize - par('plt') usrCoords - par('usr') dev.off() # 3b) Reading the monimage file in the Tcl objet named truc # and deleting the file library(tcltk) tclRequire('Img') # to read a .jpg file truc - tclVar() tkcmd(image,create,photo,truc,file=monimage.jpg) unlink(monimage.jpg) # # COMMON STEPS for both possibilities # 4) Opening the Tk window and putting the image inside tt - tktoplevel() tkpack(ll - tklabel(tt, image=truc), fill=both) tkwm.resizable(tt, 0, 0) # non resizable window # 5) Callback # Calculus of dimensions initial image/tk image width - as.numeric(tclvalue(tkwinfo(reqwidth,ll))) height - as.numeric(tclvalue(tkwinfo(reqheight,ll))) xMin - parPlotSize[1] * width xRange - (parPlotSize[2] - parPlotSize[1]) * width rangeX - (usrCoords[2] - usrCoords[1])/xRange yMin - parPlotSize[3] * height yRange - (parPlotSize[4] - parPlotSize[3]) * height rangeY - (usrCoords[4] - usrCoords[3])/yRange OnLeftClick = function(x,y) { xClick - as.numeric(x) yClick - height - as.numeric(y) xPlotCoord - usrCoords[1]+(xClick-xMin)*rangeX yPlotCoord - usrCoords[3]+(yClick-yMin)*rangeY ## AN ACTION ## print(c(xPlotCoord, yPlotCoord)) } tkbind(tt, Button-1, OnLeftClick) # Action tkbind(tt, Button-3, function() tclvalue(done) = 1) # Exit # 6) Loop waiting the events done = tclVar(0) tkwait.variable(done) tkdestroy(tt) ### -- Selon Prof Brian Ripley [EMAIL PROTECTED]: You have two asynchronous processes here: your R function will return, leaving a Tk widget up. Duncan Murdoch's solution is to give you a function that will retrieve at some future stage the result of the last press (if any) of Get coordinates button. Is that what you want? I think it is more likely you want to wait for the Tk interaction and then return the results, that is use a `modal' widget. If so, take a look at the examples in src/library/tcltk/R/utils.R which are modal and return their results. On Fri, 3 Jun 2005, Sebastian Luque wrote: Hello, I'm trying to write a function using tcltk to interactively modify a plot and gather locator() data. I've read Peter's articles in Rnews, the help pages in tcltk, http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/, plus a post in R-help sometime ago, but haven't found a solution. The idea goes something like this: require(tcltk) testplot - function() { getcoords - function(...) { locator(5) } x - 1:1000 y - rnorm(1000) plot(x, y) base - tktoplevel() loc.pts - tkbutton(base, text = Get coordinates, command = getcoords) quit.but - tkbutton(base, text = Quit, command = function() tkdestroy(base)) tkpack(loc.pts, quit.but) } I'd like testplot to return the value from getcoords. The Get coordinates button seems to be correctly calling getcoords,
[R] RecordPlot
I want to do a zoom with recordPlot(). I have problems with lists. (R-2.0.1 patched 2004-11-30 , various linux). I have problems with RecordPlot class structure. plot(1:10) saveP - recordPlot() dev.off() sx - saveP[[1]][[2]][[2]] saveP[[1]][[2]][[2]] - sx Error in [[-(`*tmp*`, 1, value = list(list( .Primitive(plot.new)), list(.Primitive(plot .window), c(1, : incompatible types typeof(saveP[[1]][[2]][[2]]) [1] double typeof(sx) [1] double But: s1 - saveP[[1]] s1[[2]][[2]] - c(4,6) saveP[1] - list(s1) saveP # zoom is OK Also, I don't understand recursive indexing. With not modified saveP, saveP[[c(1,2)]] [[1]] .Primitive(plot.window) [[2]] [1] 1 10 [[3]] [1] 1 10 [[4]] [1] [[5]] [1] NA # OK saveP[[c(1,2,2)]] Error: recursive indexing failed at level 2 # why not [1] 1 10 ?? Jean Coursol __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Bug in parse; memory access test forgotten ?
I was exploring the polynom library with students: library(polynom) x - polynomial() z - (1+x)^100 f - as.function(z) # = Segmentation fault (with R-1.8.1 and R-1.9.0) !!! Debugging at hand: as.function.polynomial - function (x, ...) { p - x horner - function(p) { a - as.character(rev(unclass(p))) h - a[1] while (length(a - a[-1]) 0) { h - paste(x*(, h, ), sep = ) if (a[1] != 0) h - paste(a[1], + , h, sep = ) } h } f - function(x) NULL body(f) - parse(text = horner(p))[[1]] f } horner - function(p) { a - as.character(rev(unclass(p))) h - a[1] while (length(a - a[-1]) 0) { h - paste(x*(, h, ), sep = ) if (a[1] != 0) h - paste(a[1], + , h, sep = ) } h } x - polynomial() z - (1+x)^100 zh - horner(z) nchar(zh) [1] 2404 parse(text = zh) # = Segmentation fault (it ran one time !!!) # The R solution (VR S programming p 95) as.function.polynomial - function(p) { function(x) { v - 0; for (a in rev(p)) v - a + x*v; v } } # is running perfectly... Jean Coursol __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html