Re: [R] Plot multiple columns
Hi, You could use melt from the reshape package to create a long format data.frame. This is more easy to plot with lattice or ggplot2, and you can then use facetting to arrange several plots on the same page. The dummy example below produces 10 pages of output with 10 graphs per page. library(ggplot2) dl - replicate(10, as.data.frame(matrix(rnorm(1e3), ncol=10)), simplify=FALSE) names(dl) - paste(column, seq_along(dl), sep=) # dummy list of data.frames str(dl) # a function to plot one data.frame plotone - function(d, ...) qplot(seq_along(value), value, data=melt(d)) + facet_wrap(~variable, scales=free) # call the function for each data.frame pl - llply(dl, plotone) # print to a file pdf(test.pdf) l_ply(pl, print) dev.off() HTH, baptiste On 1 June 2010 10:02, Noah Silverman n...@smartmediacorp.com wrote: I'm running a long MCMC chain that is generating samples for 22 variables. I have each run of the chain as a row in a matrix. So: Chain[,1] is the column with all the samples for variable one. Chain[,2] is the column with all the samples for variable 2, etc. I'd like to fit all 22 on a single page to print a nice summary. It is OK if the graphs are small, I just need to show the overall shape and convergence. Using par(mfrow=(11,2)) gives me the error: figure margins too large when I try to draw a plot I looked at the Lattice package, which seems very promising, but I can't figure out how to have it plot each column in a separate box. I need to do this same one page summary about 50 times, so it would be a nightmare to make over 1,000 plots by hand. Ideally, I'd create a loop for each of the 50 runs that would generate a single page containing the 22 plots. In pseudocode: for( i in 1:50){ par(mfrow(11,2)) for(j in 1:22){ plot(Chain[,j], type=l) } } BUT, this doesn't work. Does anyone have any ideas about an easy way to do this? Thanks! __ 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.
Re: [R] textbox in lattice
Hi, It's not clear what you mean by summary text without a minimal reproducible example. If your text is ordered as a matrix or a data.frame, you might want to try this grid function, gridExtra::grid.table(as.matrix(summary(iris)), theme=theme.white()) If your text has the form of a paragraph, the RGraphics::splitTextGrob function might help. HTH, baptiste On 1 June 2010 19:37, Noah Silverman n...@smartmediacorp.com wrote: Hi, I want to add a box at the bottom of a lattice window (device/page?). Lattice has drawn a nice group of panels with all the plots I need. How do I add my own summary text at the bottom (several lines worth?) __ 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.
Re: [R] Faster union of polygons?
Hi, I think you could use a concave hull from the alphahull package, http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/ It may be difficult to find the right parameters if the polygons differ widely in edge lengths, though. HTH, baptiste On 2 June 2010 03:53, Remko Duursma remkoduur...@gmail.com wrote: Dear R-helpers, thanks for yesterday's speeding-up tip. Here is my next query: I have lots of polygons (not necessarily convex ones, and they never have holes) given by x,y coordinates. I want to get the polygon that is the union of these polygons. This is my current method, but I am hoping there is a faster method (up to thousands of polygons, each with ca. 40 xy points). Example: library(gpclib) # A polygon leaf - structure(c(0, 1, 12.9, 16.5, 18.8, 17, 16.8, 15.5, 12.1, 8.2, 6.3, 5, 2, 0, -1.5, -4.3, -6.6, -10.3, -14.8, -19.4, -22.2, -23.5, -22.2, -17.6, -7.8, 0, 0, -2.4, 2.8, 8.9, 19.9, 33.9, 34.8, 40.4, 49.7, 69.2, 77.4, 83.4, 91.4, 99, 92.8, 87.3, 81.2, 71.1, 57.6, 45.4, 39.2, 26, 15.6, 5.3, 0.6, 0), .Dim = c(26L, 2L), .Dimnames = list( NULL, c(X, Y))) # Lots of polygons: releaf - function(leaf)cbind(leaf[,1]+rnorm(1,0,50),leaf[,2]+rnorm(1,0,50)) leaves - replicate(500, releaf(leaf), simplify=FALSE) # Make into gpc.poly class: leaves - lapply(leaves, as, gpc.poly) # Make union . system.time({ leavesoutline - union(leaves[[1]], leaves[[2]]) for(i in 3:length(leaves))leavesoutline - union(leavesoutline, leaves[[i]]) }) # about 1sec here. # Check it: plot(leavesoutline) thanks! Remko - Remko Duursma Research Lecturer Centre for Plants and the Environment University of Western Sydney Hawkesbury Campus Richmond NSW 2753 Dept of Biological Science Macquarie University North Ryde NSW 2109 Australia Mobile: +61 (0)422 096908 www.remkoduursma.com __ 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.
Re: [R] Fancy Page layout
Hi, ggplot2 or lattice could help you in creating the plots. Adding a summary will however require some play with Grid graphics; either using gridBase to mix lattice / ggplot2 output with base R graphics (e.g. textplot() from some package I forget), or you'll need to produce the textual summary in some form that Grid understands (of course, LaTeX / Sweave is a good option for this step too). A pure Grid graphics example is illustrated below, library(ggplot2) library(gridExtra) # R-forge str(diamonds) onelevel - function(d){ plots - qplot(depth, table, data=d, colour=clarity) + facet_wrap(~cut) tab - tableGrob(head(d)) plotsandtable - c(list(plots), list(tab), list(plot=FALSE, main=paste(unique(d$color do.call(arrange, plotsandtable) } l - dlply(diamonds, .(color), onelevel) pdf(test.pdf) l_ply(l, function(page) {grid.newpage(); grid.draw(page)} ) dev.off() HTH, baptiste On 31 May 2010 20:16, Noah Silverman n...@smartmediacorp.com wrote: Hi, Working on a report that is going to have a large number of graphs and summaries. We have 80 groups with 20 variables each. Ideally, I'd like to produce ONE page for each group. It would have two columns of 10 graphs and then the 5 number summary of the variables at the bottom. So, perhaps the top 2/3 of the page has the graphs and the bottom third has 20 rows of data summary(maybe a table of sorts.) This COULD be done in Latex, but would have to be hand coded for each of the 80 groups which would be painfully slow. I can easily do the graphs with par(mfrow=c(5,2)) band then draw the graphs in a loop. But I am stuck from here: 1) How do I control the size of the plot window. (Ideally, it should print to fill an 8.5 x 11 piece of paper) 2) Is there a way to easily insert a 5 number summary (summary command) into the lower half of the page. Does anybody have any ideas?? Thanks! __ 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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] substitute, expression and factors
Thank you for the explanation, and the fortune-ish quote, “As the documentation for substitute() says, there is no guarantee that the result makes sense.” Best, baptiste On 19 May 2010 02:59, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 18/05/2010 4:36 PM, baptiste auguie wrote: Dear list, I am puzzled by this, substitute(expression(x), list(x = factor(letters[1:2]))) # expression(1:2) Why do I get back the factor levels inside the expression and not the labels? As the documentation for substitute() says, there is no guarantee that the result makes sense. Yours doesn't, and it confuses the deparser, which is not displaying what you really have: y - substitute(expression(x), list(x = factor(letters[1:2]))) y expression(1:2) str(y) language expression(structure(1:2, .Label = c(a, b), class = factor)) The problem is that expressions don't normally have attributes, and factors have both .Label and class attributes. Put another way: expressions don't normally include factors, they include names and calls and atomic vectors. The deparser doesn't distinguish between the language 1:2 and the atomic vector that is the value of that expression, but it doesn't expect attributes, and doesn't go looking for them. Duncan Murdoch The following work as I expected, substitute(expression(x), list(x = letters[1:2])) # expression(c(a, b)) substitute(x, list(x = factor(letters[1:2]))) # [1] a b # Levels: a b bquote(.(factor(letters[1:2]))) # [1] a b # Levels: a b All the best, baptiste __ 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.
Re: [R] x y plot with z coordinate scaling to a color value
Hi, See also ?lattice::xyplot and ?ggplot2::geom_point , either one can do it automatically. HTH, baptiste On 19 May 2010 12:24, Jannis bt_jan...@yahoo.de wrote: Dears, before I start programming my own function I would like to ask you whether there is any function already available that lets me plot a x/y plot with the colors of the points determined by scaling a third variable z into a defined colorramp? Until now I am using a wild combination of functions to create a colorvector by hand, then calling plot(x,y,col=colors) and then using color.legend() from plotrix to add a colorbar. Is there anything where I can just call (for example) plot(x,y,z) and get a plot and a colorbar next to it? If not I have to program that myself because my current way involves a lot of steps and readjusting plotting regions. Thanks for your help! Jannis __ 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] lattice::panel.levelplot.raster too picky with unequal spacing
Dear all, I got a couple of warnings using panel.levelplot.raster, In panel.levelplot.raster(..., interpolate = TRUE) : 'y' values are not equispaced; output will be wrong although I was quite sure my data were equally spaced (indeed, I created them with seq()). A closer look at the source code reveals that the function tests for exact uniformity in grid spacing, if (length(unique(diff(uy))) != 1) warning('x' values are not equispaced; output will be wrong) The following dummy example would suggest that a strict equality is not always suitable, x - seq(0, 50, length=100) ux - sort(unique(x[!is.na(x)])) length(unique(diff(ux))) # 8 sd(unique(diff(ux))) # 2.462951e-15 Suggestions / comments are welcome. Best regards, baptiste sessionInfo() R version 2.11.0 RC (2010-04-16 r51754) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lattice_0.18-5 loaded via a namespace (and not attached): [1] grid_2.11.0 tools_2.11.0 -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] lattice::panel.levelplot.raster too picky with unequal spacing
On 18 May 2010 15:30, Deepayan Sarkar deepayan.sar...@r-project.org wrote: Maybe a better test would be isTRUE(all.equal(diff(range(diff(ux))), 0)) I'll try that out for the next release. Sounds good (and works for me), thanks. baptiste __ 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] substitute, expression and factors
Dear list, I am puzzled by this, substitute(expression(x), list(x = factor(letters[1:2]))) # expression(1:2) Why do I get back the factor levels inside the expression and not the labels? The following work as I expected, substitute(expression(x), list(x = letters[1:2])) # expression(c(a, b)) substitute(x, list(x = factor(letters[1:2]))) # [1] a b # Levels: a b bquote(.(factor(letters[1:2]))) # [1] a b # Levels: a b All the best, baptiste __ 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] retrieving last R output
Hi, Try this, saveMyWork - .Last.value HTH, baptiste On 17 May 2010 15:07, math_daddy math_da...@hotmail.com wrote: Hello. I ran a simulation that took a few days to complete, and want to analyze the results, but have just realized that I (idiotically) did not assign the output to a variable when I intitiated the simulation. Is there any way to retrieve the last output produced by R so that these last few days were not a waste? Thank you very much. -- View this message in context: http://r.789695.n4.nabble.com/retrieving-last-R-output-tp2219574p2219574.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Display Large Matrix as an Image
Hi, try this, m = matrix(runif(2000*2400), nrow=2000) library(grid) grid.raster(m) HTH, baptiste On 17 May 2010 20:35, tetonedge de...@tetonedge.net wrote: I have a matrix that is 2400x2000 and I would like to display it as an image, I have tried image(), but due to the size of the matrix the drawing of the plot is extremely slow. Does anybody know of any fast ways to visualize such a large matrix. Thanks -- View this message in context: http://r.789695.n4.nabble.com/Display-Large-Matrix-as-an-Image-tp2220139p2220139.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] how to save multiple plots in one PDF file?
No, that's only true for lattice and ggplot2 graphics. The problem here is with this line, windows(width=5, height=5) which shouldn't be there. HTH, baptiste On 17 May 2010 22:23, Jun Shen jun.shen...@gmail.com wrote: If you do plotting in a loop, then you need to print it to the device. print(plot(xj,y)) On Mon, May 17, 2010 at 3:02 PM, Shirley Bao baoxi...@gmail.com wrote: Thanks! I got an error message when opening the pdf file: There was an error opening this document. This file cannot be opened because it has no pages. Here is what I did in plotting and saving the file: pdf(file=C:/figure.pdf) for (j in 1:numColumns) { windows(width=5, height=5) plot(xj,y) } dev.off() Any ideas what might cause the problem? Thanks! On Mon, May 17, 2010 at 12:52 PM, Jun Shen jun.shen...@gmail.com wrote: 1.Open pdf device pdf() 2.Do your plotting as many as you want, you won't see the plots on the screen because they go directly to the pdf() device. 3.Turn off the pdf() dev.off() Then you can review your plots in the pdf file. For more details see ?pdf Jun On Mon, May 17, 2010 at 2:41 PM, Shirley Bao baoxi...@gmail.com wrote: I have created separate plots in multiple graphics windows using the windows() function in R. How do I save all the plots in one PDF file? I tried savePlot(C:/rplot.pdf, type = pdf). However, it only saved the plot in the current graphics window. Thank you! [[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-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.
Re: [R] problem with making multiple plots (geom_pointrange) in a loop (ggplot2)
Hi, On 16 May 2010 03:31, michael westphal mi_westp...@yahoo.com wrote: [ snipped ] Any suggestions? i'd suggest you - read the posting guide - upgrade your R to the latest version - don't post to two mailing lists - make your example minimal, self-contained, reproducible - show the result of sessionInfo() HTH, baptiste [[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-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] graphics::plot Organizing line types, line colors and generating matching legends...
Hi, Lattice and ggplot2 are both ideally suited for this task. Consider this example, library(ggplot2) d = data.frame(x=1:10, a1=rnorm(10), b1=rnorm(10)) m = melt(d, id =x) # reshape into long format qplot(x, value, data=m, geom=path, colour=variable) library(lattice) xyplot(value~x, data=m, type=l, group=variable, auto.key=TRUE) HTH, baptiste On 10 May 2010 21:29, Ralf B ralf.bie...@gmail.com wrote: Lets say I have a generated data frame with variables that follow a naming convention: title,a1,a2,b1,b2,b3,c1,c2,c3,c4... I am plotting every column (starting from a1) as a line in a plot. That works. However my diagram becomes very unorganized. Creating legends is nice, but trying out different combinations requires me to adjust my legend since it is generally disconnected from the data. Is there an elegant way where R generates legends for its variables so that the legend will fit the line and uses the column name as in the legend? I guess I am asking for the basic Excel thing. I understand that in the standard graphics package, this is not really intended. Perhaps somebody can point me into a direction where this more easily possible? Is it for example easier in gplot or lattice? Ralf __ 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.
Re: [R] R2.11.0 - rasterImage() and barplot fill-patterns
Hi, This idea was also discussed when Paul Murrell first announced the grid.raster function to R-devel, http://tolstoy.newcastle.edu.au/R/e8/devel/09/12/0912.html My personal conclusion was that vector fill patterns are generally better in terms of resolution and speed. Of course the situation might be different if one wanted to use a fancy image pattern, or if there was a fast implementation of tiling patterns at the C level. I wrote a proof-of-concept here --- my main issue is that the resulting grob is not vectorized, http://gridextra.googlecode.com/svn/trunk/inst/comparisonPattern.r Best, baptiste On 22 April 2010 14:10, Tal Galili tal.gal...@gmail.com wrote: Hello Peter, Thank you, and the R core team, for the new release. I see that in R 2.11.0 there is now support for rendering of raster (bitmap) images through rasterImage(). I am wondering - can this be used to create a texture/fill-pattern for hist()/barplot() ? (A request made several times throughout the years on the mailing list. For example: http://osdir.com/ml/lang.r.general/2005-07/msg00799.html ) (I am also sending this e-mail to the maintainers of lattice, ggplot2 and gplots in the hope for more perspectives) With much respect, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- On Thu, Apr 22, 2010 at 12:01 PM, Peter Dalgaard pd@cbs.dk wrote: I've rolled up R-2.11.0.tar.gz a short while ago. This is a development release which contains a number of new features. Also, a number of mostly minor bugs have been fixed. See the full list of changes below. NOTE: The build platform has been changed for this release. Please watch out extra carefully for anomalies. You can get it from http://cran.r-project.org/src/base/R-2/R-2.11.0.tar.gz or wait for it to be mirrored at a CRAN site nearer to you. Binaries for various platforms will appear in due course. For the R Core Team Peter Dalgaard These are the md5sums for the freshly created files, in case you wish to check that they are uncorrupted: MD5 (AUTHORS) = ac9746b4845ae81f51cfc99262f5 MD5 (COPYING) = eb723b61539feef013de476e68b5c50a MD5 (COPYING.LIB) = a6f89e2100d9b6cdffcea4f398e37343 MD5 (FAQ) = 5b653442bedab476a4eff7468192fb5f MD5 (INSTALL) = 70447ae7f2c35233d3065b004aa4f331 MD5 (NEWS) = 59017734fb8474f98f994c7a5a27f9fb MD5 (ONEWS) = a8c985af5ad5e9c7e0a9f502d07baeb4 MD5 (OONEWS) = 4f004de59e24a52d0f500063b4603bcb MD5 (R-latest.tar.gz) = c6c1e866299f533617750889c729bfb3 MD5 (README) = 433182754c05c2cf7a04ad0da474a1d0 MD5 (RESOURCES) = 020479f381d5f9038dcb18708997f5da MD5 (THANKS) = f2ccf22f3e20ebaa86f8ee5cc6b0f655 MD5 (R-2/R-2.11.0.tar.gz) = c6c1e866299f533617750889c729bfb3 This is the relevant part of the NEWS file: CHANGES IN R VERSION 2.11.0 SIGNIFICANT USER-VISIBLE CHANGES o Packages must have been installed under R = 2.10.0, as the current help system is the only one now supported. o A port to 64-bit Windows is now available as well as binary package repositiories: see the 'R Administration and Installation Manual'. o Argument matching for primitive functions is now done in the same way as for interpreted functions except for the deliberate exceptions call switch .C .Fortran .Call .External all of which use positional matching for their first argument, and also some internal-use-only primitives. o The default device for command-line R at the console on Mac OS X is now quartz() and not X11(). NEW FEATURES o The 'open' modes for connections are now interpreted more consistently. open = r is now equivalent to open = rt for all connections. The default open = now means rt for all connections except the compressed file connections gzfile(), bzfile() and xzfile() for which it means rb. o R CMD INSTALL now uses the internal untar() in package utils: this ensures that all platforms can install bzip2- and xz-compressed tarballs. In case this causes problems (as it has on some Windows file systems when run from Cygwin tools) it can be overridden by the environment variable R_INSTALL_TAR: setting this to a modern external tar program will speed up unpacking of large (tens of Mb or more) tarballs. o help(try.all.packages = TRUE) is much faster (although the time taken by the OS to find all the packages the first time it is used can dominate the time). o R CMD check has a new option '--timings' to record per-example timings in file
Re: [R] Words appear to be bolded in the PDF output
Hi, Taking a wild guess, it looks to me that you might have overlaid several times the same text, plot.new() text(0.5,0.5,rep(test,10)) HTH, baptiste On 20 April 2010 08:54, chrisli1223 chri...@austwaterenv.com.au wrote: Hi all, I have written a note near each of my graphs using mtext. mtext(text,side=1,line=4,cex=0.5,adj=0) Then I have exported the graphs as a PDF file. pdf(file=name,paper='a4',width=7.27,height=10.69) The mtext appears OK in R. But it looks like it is bolded in the PDF file. http://n4.nabble.com/file/n2016971/graph.png I am not sure if this is actually my monitor/computer's problem. But I want to see if it can be fixed in R. Many thanks, Chris -- View this message in context: http://n4.nabble.com/Words-appear-to-be-bolded-in-the-PDF-output-tp2016971p2016971.html Sent from the R help mailing list archive at Nabble.com. [[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-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] How to embed italic Greek letters in a eps file?
Hi, Another option might be the tikzDevice package, which uses LaTeX to process the fonts, library(tikzDevice) tikz(standAlone=T) plot(1,1, type = 'n') mtext(side = 3, line = 2, $\\mu$) dev.off() ## system(/usr/texbin/pdflatex Rplots.tex) HTH, baptiste On 20 April 2010 07:30, Prof Brian Ripley rip...@stats.ox.ac.uk wrote: This is discussed in detail on the help page for postcript! Default PostScript with the 14 standard fonts does not cover Greek at all (but it does cover mathematical symbols such as mu, in a different typeface). See the section 'Encodings': to display Greek you need to have an encoding which supports Greek. You did not give us the 'at a minimum' information requested in the posting guide so we don't know your locale, but at a guess you got ISOLatin1, which does not support Greek. So try encoding=Greek and family=URWHelvetica, and make sure that your viewer is able to support the font. On Mon, 19 Apr 2010, Julia Uitz wrote: Hi, I need to add on a plot text containing italic Greek characters using the function mtext (i.e. I cannot use Hershey vectors). The characters are nicely displayed when the file is saved as png but not when saved as eps. See code below as example: #postscript('test.eps') png('test.png') plot(1,1, type = 'n') mtext(side = 3, line = 2, expression(italic('\u03bc'))) graphics.off() Does anyone have an idea on how to solve this issue? FYI I use R 2.10.1 for Max OS X (v. 10.6.3) but this is not an OS related issue (I have also tried with Windows). Many many thanks in advance, - Julia -- Julia Uitz Scripps Institution of Oceanography University of California San Diego __ 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 __ 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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] Exporting an rgl graph
I have seen pdf files with 3D objects embedded in it, using the U3D format, http://en.wikipedia.org/wiki/Universal_3D but I don't think there's a device for this in R; in fact there may not even exist a third-party post-processing route available at this time to bridge the gap between rgl and this format. It sure would be nice, though. Best, baptiste On 15 April 2010 14:12, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Thu, Apr 15, 2010 at 1:01 PM, cgeno...@u-paris10.fr wrote: Thanks for you answer. Let me precise my question. In fact, I do not want to capture a screen, I want to save an object that can be seen in 3D. With rgl, using my mouse, I can make the object move. This is what I want to export: an real 3D object that my collaborator will have the possibility to see in 3D. You mean without them having to install R and rgl and run the code that produces your graphic? I guess you could somehow export a VRML or some other 3d file: http://en.wikipedia.org/wiki/VRML but I suspect of all the billions of people on the planet only Duncan Murdoch knows enough about rgl to figure that one out... The person at the other end would still need a VRML viewer. Just get them to install R. Barry __ 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.
Re: [R] Exporting an rgl graph
On 15 April 2010 18:34, l...@stat.uiowa.edu wrote: The current issue of JCGS (Vol 18 No 1, http://pubs.amstat.org/toc/jcgs/19/1) has an editorial on including animations, 3D visualizations, and movies in on-line PDF files supporting JCGS articles. The online supplements to the editorial include examples. The 3D examples related to the misc3d packages are also available in http://www.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/. At some point the code there will be added to misc3d. It should be possible to adapt these ideas to other objects rendered with rgl. luke On Thu, 15 Apr 2010, baptiste auguie wrote: I have seen pdf files with 3D objects embedded in it, using the U3D format, http://en.wikipedia.org/wiki/Universal_3D but I don't think there's a device for this in R; in fact there may not even exist a third-party post-processing route available at this time to bridge the gap between rgl and this format. It sure would be nice, though. Very glad to be proven wrong! Thanks, baptiste __ 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] Figures within tables [slightly off-topic]
Hi, On 12 April 2010 22:07, Peter Jepsen p...@dce.au.dk wrote: 3. Are there R packages that can draw tables? the gplots package has a textplot() function, and the gridExtra package a tableGrob(), http://rwiki.sciviews.org/doku.php?id=tips:graphics-grid:table In theory it should be possible to adapt the latter to allow the placement of a (preferably lattice / ggplot2 / Grid) graphic in the desired cells. HTH, baptiste __ 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] It This data viz possible in R?
An improved version below (now the connections are drawn in the correct order), library(grid) arcTextGrob - function(x=unit(0.5, npc), y=unit(0.5, npc), labels=library()$results[,1], links=sample(seq_along(labels), 20, rep=T), default.units=npc, gp=gpar(), ...) { ## circle full.height - sum(stringHeight(labels)) radius - 1.2 /(2*pi) * full.height g1 - circleGrob(0.5, 0.5, r=radius, default.units=npc, gp=gpar(col=NA)) ## text labels n - length(labels) ang - seq(0, n-1) * 2 * pi/n radius.npc - convertUnit(radius, npc, val=T) coords - data.frame(x=0.5+radius.npc*cos(ang), y=0.5+radius.npc*sin(ang)) g2 - textGrob(labels, x=coords$x , y=coords$y , rot=ang*180/pi, default.units=npc, hjust=0) ## connecting pairs xm - matrix(coords$x[links], ncol=2, byrow=T) ym - matrix(coords$y[links], ncol=2, byrow=T) ## find out which pairs are not in trigo order ## and swap them swap - as.logical(sign((xm[, 1]-0.5)*(ym[, 2]-0.5) - (xm[, 2]-0.5)*(ym[, 1]-0.5)) + 1) xm[swap, ] - rev(xm[swap]) ym[swap, ] - rev(ym[swap]) g3 - do.call(gList, mapply(curveGrob, x1=xm[, 1], y1=ym[, 1], x2=xm[, 2], y2=ym[, 2], ncp=8, curvature=0.4, square=F, SIMPLIFY=FALSE)) gTree(children=gList(g1, g2, g3), outer.radius=convertUnit(radius, npc) + convertUnit(max(stringWidth(labels)), npc)) } grid.arcText - function(...) grid.draw(arcTextGrob(...)) set.seed(1234) grid.newpage() grid.arcText() On 7 April 2010 23:13, baptiste auguie baptiste.aug...@googlemail.com wrote: The following grob might be a starting point. I couldn't find a clean way of orienting the linking arcs though... Best, baptiste library(grid) paragraph - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent adipiscing lobortis placerat. Nunc vel arcu mauris. Aliquam erat volutpat. Integer et pharetra orci. Sed rutrum facilisis dolor et condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nunc leo nibh, pellentesque et convallis quis, mattis ut mi. Nunc dignissim auctor elit pulvinar malesuada. Cras dapibus hendrerit ligula quis suscipit. Proin porta tempor feugiat. Ut quis nisi lacus, et egestas tortor. Fusce porttitor tincidunt fringilla. Vivamus rhoncus ultrices elit, at fermentum nisl scelerisque et. Duis placerat est at justo vestibulum sodales. Curabitur quis eros tellus. words - strsplit(paragraph, )[[1]] labels - apply(matrix(words, ncol=3, byrow=T), 1, paste, collapse= ) arcTextGrob - function(x=unit(0.5, npc), y=unit(0.5, npc), labels=letters[1:10], links=sample(seq_along(labels), 10), min.radius=unit(2, cm), default.units=npc, gp=gpar(), ...) { ## circle of perimeter = 1.5 * the text height full.height - sum(stringHeight(labels)) radius - 1.5 /(2*pi) * full.height g1 - circleGrob(0.5, 0.5, r=radius, default.units=npc) ## text labels n - length(labels) ang - seq(0, n-1) * 2 * pi/n radius.mm - convertUnit(radius, npc, val=T) coords - data.frame(x=0.5+radius.mm*cos(ang), y=0.5+radius.mm*sin(ang)) g2 - textGrob(labels, x=coords$x , y=coords$y , rot=ang*180/pi, default.units=npc, hjust=0) ## links, ## NOTE: they are not well ordered... xm - matrix(coords$x[links], ncol=2, byrow=T) ym - matrix(coords$y[links], ncol=2, byrow=T) g3 - do.call(gList, mapply(curveGrob, x1=xm[, 1], y1=ym[, 1], x2=xm[, 2], y2=ym[, 2], ncp=8, curvature=0.3, square=F, SIMPLIFY=FALSE)) gTree(children=gList(g1, g2, g3)) } grid.arcText - function(...) grid.draw(arcTextGrob(...)) dev.new() grid.arcText(labels=labels) On 7 April 2010 16:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: There is draw.arc in the plotrix package. On Wed, Apr 7, 2010 at 10:20 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, Barry suggested a way to place the text labels; I would like to point out the grid.curve() function that might help in connecting the labels with nice-looking curves. I don't know of a base graphics equivalent (xspline() might come close) so it might be best to opt for Grid. HTH, baptiste On 7 April 2010 15:46, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Wed, Apr 7, 2010 at 2:28 PM, Brock Tibert btibe...@yahoo.com wrote: Hi All, I am new to R, but it has been a lot of fun learning as I go and have been blow away by what it can do. Came across this example and wanted to see if ggplot2 or some other visualization package could make this sort of graphic. http://www.visualcomplexity.com/vc/project.cfm?id=717utm_source=feedburnerutm_medium=feedutm_campaign=Feed
Re: [R] square root of inverse
try this, install.packages(expm, repos=http://R-Forge.R-project.org;) On 8 April 2010 17:28, arindam fadikar arindam.fadi...@gmail.com wrote: On Thu, Apr 8, 2010 at 5:38 PM, David Winsemius dwinsem...@comcast.netwrote: On Apr 8, 2010, at 1:45 AM, arindam fadikar wrote: Dear users, How to get a symmetric square root of a positive definite matrix? I have tried using spectral decomposition, but some eigen values come out to be complex. Is there any function in R that can give the symmetric square root of a pd matrix? require(expm) ?sqrtm expm is not a package, but a function which gives exp of a matrix, and i dont get any result from ?sqrtm. Please help. -- Arindam Fadikar M.Stat Indian Statistical Institute. New Delhi, India [[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. David Winsemius, MD West Hartford, CT -- Arindam Fadikar M.Stat Indian Statistical Institute. New Delhi, India [[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-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] It This data viz possible in R?
Hi, Barry suggested a way to place the text labels; I would like to point out the grid.curve() function that might help in connecting the labels with nice-looking curves. I don't know of a base graphics equivalent (xspline() might come close) so it might be best to opt for Grid. HTH, baptiste On 7 April 2010 15:46, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Wed, Apr 7, 2010 at 2:28 PM, Brock Tibert btibe...@yahoo.com wrote: Hi All, I am new to R, but it has been a lot of fun learning as I go and have been blow away by what it can do. Came across this example and wanted to see if ggplot2 or some other visualization package could make this sort of graphic. http://www.visualcomplexity.com/vc/project.cfm?id=717utm_source=feedburnerutm_medium=feedutm_campaign=Feed:+visualcomplexity+(visualcomplexity.com)utm_content=Google+Reader Thanks in advance! Not quite out-of-the box, but you can draw text with the text() function setting the angle with the 'srt' parameter, and you can draw lines using 'lines'. You can compute angles using 'pi'. You'll need a bit of trig to work out the angle that the lines start and end at. That's about all you need to know. Some of the subtleties of the typesetting of that specific piece may be tricky, but it's easy to write a function that takes a vector of strings and an adjacency matrix and plots something like it. Give R-help another hour and I reckon something will turn up. Not from me, I'm watching the IPL cricket. Barry __ 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.
Re: [R] It This data viz possible in R?
The following grob might be a starting point. I couldn't find a clean way of orienting the linking arcs though... Best, baptiste library(grid) paragraph - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent adipiscing lobortis placerat. Nunc vel arcu mauris. Aliquam erat volutpat. Integer et pharetra orci. Sed rutrum facilisis dolor et condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nunc leo nibh, pellentesque et convallis quis, mattis ut mi. Nunc dignissim auctor elit pulvinar malesuada. Cras dapibus hendrerit ligula quis suscipit. Proin porta tempor feugiat. Ut quis nisi lacus, et egestas tortor. Fusce porttitor tincidunt fringilla. Vivamus rhoncus ultrices elit, at fermentum nisl scelerisque et. Duis placerat est at justo vestibulum sodales. Curabitur quis eros tellus. words - strsplit(paragraph, )[[1]] labels - apply(matrix(words, ncol=3, byrow=T), 1, paste, collapse= ) arcTextGrob - function(x=unit(0.5, npc), y=unit(0.5, npc), labels=letters[1:10], links=sample(seq_along(labels), 10), min.radius=unit(2, cm), default.units=npc, gp=gpar(), ...) { ## circle of perimeter = 1.5 * the text height full.height - sum(stringHeight(labels)) radius - 1.5 /(2*pi) * full.height g1 - circleGrob(0.5, 0.5, r=radius, default.units=npc) ## text labels n - length(labels) ang - seq(0, n-1) * 2 * pi/n radius.mm - convertUnit(radius, npc, val=T) coords - data.frame(x=0.5+radius.mm*cos(ang), y=0.5+radius.mm*sin(ang)) g2 - textGrob(labels, x=coords$x , y=coords$y , rot=ang*180/pi, default.units=npc, hjust=0) ## links, ## NOTE: they are not well ordered... xm - matrix(coords$x[links], ncol=2, byrow=T) ym - matrix(coords$y[links], ncol=2, byrow=T) g3 - do.call(gList, mapply(curveGrob, x1=xm[, 1], y1=ym[, 1], x2=xm[, 2], y2=ym[, 2], ncp=8, curvature=0.3, square=F, SIMPLIFY=FALSE)) gTree(children=gList(g1, g2, g3)) } grid.arcText - function(...) grid.draw(arcTextGrob(...)) dev.new() grid.arcText(labels=labels) On 7 April 2010 16:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: There is draw.arc in the plotrix package. On Wed, Apr 7, 2010 at 10:20 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, Barry suggested a way to place the text labels; I would like to point out the grid.curve() function that might help in connecting the labels with nice-looking curves. I don't know of a base graphics equivalent (xspline() might come close) so it might be best to opt for Grid. HTH, baptiste On 7 April 2010 15:46, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Wed, Apr 7, 2010 at 2:28 PM, Brock Tibert btibe...@yahoo.com wrote: Hi All, I am new to R, but it has been a lot of fun learning as I go and have been blow away by what it can do. Came across this example and wanted to see if ggplot2 or some other visualization package could make this sort of graphic. http://www.visualcomplexity.com/vc/project.cfm?id=717utm_source=feedburnerutm_medium=feedutm_campaign=Feed:+visualcomplexity+(visualcomplexity.com)utm_content=Google+Reader Thanks in advance! Not quite out-of-the box, but you can draw text with the text() function setting the angle with the 'srt' parameter, and you can draw lines using 'lines'. You can compute angles using 'pi'. You'll need a bit of trig to work out the angle that the lines start and end at. That's about all you need to know. Some of the subtleties of the typesetting of that specific piece may be tricky, but it's easy to write a function that takes a vector of strings and an adjacency matrix and plots something like it. Give R-help another hour and I reckon something will turn up. Not from me, I'm watching the IPL cricket. Barry __ 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-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] Aligning text in the call to the text function
Hi, One option with Grid graphics, m - matrix(c( 1667,3,459, 2001, 45, 34, 1996, 2,5235), dimnames=list(c(Eric Alan, Alan,John David)), ncol=3, byrow=T) ## install.packages(gridExtra, repos=http://R-Forge.R-project.org;) library(gridExtra) grid.table(m, theme=theme.white(row.just=left,core.just=left)) HTH, baptiste On 1 April 2010 21:39, Tighiouart, Hocine htighiou...@tuftsmedicalcenter.org wrote: Hi, I have text (see below) that is aligned nicely when printed in the command window in R but when plotted using text function, it does not show alignment unless I use the family=mono in the call to the text function. Is there a way to specify a different font while maintaining the alignment? Eric Alan 1667 3 459 Alan 2001 45 34 John David 1996 2 5235 Thanks for any hints Hocine __ 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.
Re: [R] lattice grob
Hi, On 24 March 2010 23:22, Paul Murrell p.murr...@auckland.ac.nz wrote: Hi baptiste auguie wrote: Thanks Felix and Paul. I had overlooked grid.grabExpr, assuming that one had to draw on a device before grabbing the output. Now I'm not sure if there's any difference between either solution, I'll go for the shortest. As Felix pointed out, one possible problem with the grid.gradExpr() approach is that you get a copy of what was drawn on the current device, which may be dependent on things like how big the current device is, what fonts it uses, etc. I understand the point you're making, except for one detail: grid.grabExpr() can capture the output without any real device being open. I am guessing that there is some kind of virtual NullDevice playing its role, but then the captured output wouldn't know to decide anything about sizes, fonts, etc., would it? This may well be explained in your 'R graphics' book, unfortunately I left my copy abroad. I don't remember seeing mention of the drawDetails() approach for this purpose though, which made me a little wary. My current code seems to work fine with grid.grabExpr(), http://code.google.com/p/gridextra/source/browse/trunk/R/arrange.r I wonder what's the reason behind most high-level packages based on Grid not defining their output as a grob/gTree but pushing/popping viewports directly instead. Thanks again, baptiste The drawDetails() approach means that 'lattice' will calculate what to draw every time you want to draw, so it should adapt to different device sizes and different device properties more gracefully. On the other hand, the drawDetails() approach only records a VERY high-level description of what you are drawing (a 'lattice' object), so you cannot fiddle about with the low-level details of what you draw. For example, following ... latticeGrob - function(p, ...){ grob(p=p, ..., cl=lattice) } drawDetails.lattice - function(x, recording=FALSE){ lattice:::plot.trellis(x$p, newpage=FALSE) } p1 - xyplot(1:10 ~ 1:10) g1 - latticeGrob(p1) grid.draw(g1) ... grid.ls() gives you ... grid.ls() GRID.lattice.53 ... whereas the grid.gradExpr() approach records all of the bits and pieces of the drawing, e.g., following ... p1 - xyplot(1:10 ~ 1:10) g1 - grid.grabExpr(print(p1)) grid.draw(g1) ... grid.ls() gives you ... grid.ls() GRID.gTree.94 GRID.rect.85 plot1.xlab plot1.ylab GRID.segments.86 GRID.segments.87 GRID.text.88 GRID.segments.89 GRID.text.90 GRID.segments.91 GRID.points.92 GRID.rect.93 So it's a bit of a trade-off. Paul Best, baptiste On 22 March 2010 00:18, Felix Andrews fe...@nfrac.org wrote: What's wrong with using grid.grabExpr? p1 - xyplot(1:10 ~ 1:10) g1 - grid.grabExpr(print(p1)) I can imagine there would be potential problems to do with the plot-time aspect and layout calculations... On 19 March 2010 21:51, baptiste auguie baptiste.aug...@googlemail.com wrote: Dear list, I'm trying to arrange various grid objects on a page using a frameGrob. It works fine with basic grobs (textGrob, gTree, etc.), and also with ggplot2 objects using the ggplotGrob() function. I am however stuck with lattice. As far as I understand, lattice produces a list of class trellis, which is eventually displayed using the plot.trellis method. I am not sure if/how one can convert this list into a high-level grob. I tried the following, latticeGrob - function(p, ...){ grob(p=p, ..., cl=lattice) } drawDetails.lattice - function(x, recording=FALSE){ lattice:::plot.trellis(x$p) } p1 - xyplot(1:10 ~ 1:10) g1 - latticeGrob(p1) grid.draw(g1) # works fine but, fg - frameGrob(layout = grid.layout(1,1)) fg - placeGrob(fg, g1, row = 1, col = 1) grid.draw(fg) Error in UseMethod(depth) : no applicable method for 'depth' applied to an object of class NULL Ideas are most welcome, Best regards, baptiste sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] grid tools stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_0.8.7 digest_0.4.1 reshape_0.8.3 plyr_0.1.9 proto_0.3-8 gridExtra_0.5 lattice_0.17-26 gtools_2.6.1 __ 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. -- Felix Andrews / 安福立 Postdoctoral Fellow Integrated Catchment Assessment and Management (iCAM) Centre Fenner School of Environment and Society [Bldg 48a] The Australian National University Canberra ACT 0200 Australia M: +61 410 400 963 T: + 61 2 6125 4670 E: felix.andr...@anu.edu.au CRICOS Provider No. 00120C -- http://www.neurofractal.org/felix
Re: [R] multiple logical comparisons
try this one, `%ni%` - Negate(`%in%`) Best, baptiste __ 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] lattice grob
Thanks Felix and Paul. I had overlooked grid.grabExpr, assuming that one had to draw on a device before grabbing the output. Now I'm not sure if there's any difference between either solution, I'll go for the shortest. Best, baptiste On 22 March 2010 00:18, Felix Andrews fe...@nfrac.org wrote: What's wrong with using grid.grabExpr? p1 - xyplot(1:10 ~ 1:10) g1 - grid.grabExpr(print(p1)) I can imagine there would be potential problems to do with the plot-time aspect and layout calculations... On 19 March 2010 21:51, baptiste auguie baptiste.aug...@googlemail.com wrote: Dear list, I'm trying to arrange various grid objects on a page using a frameGrob. It works fine with basic grobs (textGrob, gTree, etc.), and also with ggplot2 objects using the ggplotGrob() function. I am however stuck with lattice. As far as I understand, lattice produces a list of class trellis, which is eventually displayed using the plot.trellis method. I am not sure if/how one can convert this list into a high-level grob. I tried the following, latticeGrob - function(p, ...){ grob(p=p, ..., cl=lattice) } drawDetails.lattice - function(x, recording=FALSE){ lattice:::plot.trellis(x$p) } p1 - xyplot(1:10 ~ 1:10) g1 - latticeGrob(p1) grid.draw(g1) # works fine but, fg - frameGrob(layout = grid.layout(1,1)) fg - placeGrob(fg, g1, row = 1, col = 1) grid.draw(fg) Error in UseMethod(depth) : no applicable method for 'depth' applied to an object of class NULL Ideas are most welcome, Best regards, baptiste sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] grid tools stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_0.8.7 digest_0.4.1 reshape_0.8.3 plyr_0.1.9 proto_0.3-8 gridExtra_0.5 lattice_0.17-26 gtools_2.6.1 __ 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. -- Felix Andrews / 安福立 Postdoctoral Fellow Integrated Catchment Assessment and Management (iCAM) Centre Fenner School of Environment and Society [Bldg 48a] The Australian National University Canberra ACT 0200 Australia M: +61 410 400 963 T: + 61 2 6125 4670 E: felix.andr...@anu.edu.au CRICOS Provider No. 00120C -- http://www.neurofractal.org/felix/ __ 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] lattice grob
Dear list, I'm trying to arrange various grid objects on a page using a frameGrob. It works fine with basic grobs (textGrob, gTree, etc.), and also with ggplot2 objects using the ggplotGrob() function. I am however stuck with lattice. As far as I understand, lattice produces a list of class trellis, which is eventually displayed using the plot.trellis method. I am not sure if/how one can convert this list into a high-level grob. I tried the following, latticeGrob - function(p, ...){ grob(p=p, ..., cl=lattice) } drawDetails.lattice - function(x, recording=FALSE){ lattice:::plot.trellis(x$p) } p1 - xyplot(1:10 ~ 1:10) g1 - latticeGrob(p1) grid.draw(g1) # works fine but, fg - frameGrob(layout = grid.layout(1,1)) fg - placeGrob(fg, g1, row = 1, col = 1) grid.draw(fg) Error in UseMethod(depth) : no applicable method for 'depth' applied to an object of class NULL Ideas are most welcome, Best regards, baptiste sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] grid tools stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_0.8.7 digest_0.4.1reshape_0.8.3 plyr_0.1.9 proto_0.3-8 gridExtra_0.5 lattice_0.17-26 gtools_2.6.1 __ 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] How to take out the content of character string
Hi, it's generally considered a bad practice but try this, eval(parse(text=AA)) library(fortunes) fortune(106) HTH, baptiste On 10 March 2010 07:46, jq81 jingqia...@gmail.com wrote: My question is represented by the following example. For example, I have a character string a, which is defined as AA=list(x=1, y=2) I want to take out the content of AA by using some function, so that I can obtain the following expression automatically. list(x=1, y=2) Does anyone know how to do it? Thanks a lot, -- View this message in context: http://n4.nabble.com/How-to-take-out-the-content-of-character-string-tp1587004p1587004.html Sent from the R help mailing list archive at Nabble.com. __ 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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] write.fortran
Dear all, I'm trying to write tabular data to a text file, these data will be the input of a Fortran program. The format needs to be (i7,2x,7(e15.7,2x)). I have not been able to find a clean way of producing this output with write.table. I searched for a write.fortran function similar to read.fortran() in package utils but I couldn't find any. Below is a small example of what I'm trying to achieve, but it's clearly suboptimal in many ways, m - cbind(seq(1, 5), matrix(rnorm(7*5), ncol=7)) do.call(cat, c(lapply(seq(1, nrow(m)), function(ii){ x - m[ii, ] sprintf(%i %15.7e %15.7e %15.7e %15.7e %15.7e %15.7e %15.7e \n, ii , x[1],x[2],x[3],x[4],x[5],x[6],x[7]) }), list(sep=))) Best regards, baptiste sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] tools stats graphics grDevices utils datasets methods base other attached packages: [1] foreign_0.8-38 __ 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] ggplot2: Changing colour scheme for bar plot filling?
Hi, last_plot() + scale_fill_grey() should do it HTH, baptiste On 10 March 2010 09:46, Johannes Graumann johannes_graum...@web.de wrote: Hello, I'd like to sitch to a monochrome/bw color-palette for the filling of geom_bar-bars (produced via qplot as in the example below). Hours of googling didn't yield anything useful, so I thought, I'd just ask ... Thanks, Joh library(ggplot2) qplot(factor(cyl), data=mtcars, geom=bar, fill=factor(cyl)) __ 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.
Re: [R] ggplot2: Changing colour scheme for bar plot filling?
not with the theme, as far as I know, but you can do: set_default_scale(fill, discrete,grey) baptiste On 10 March 2010 10:31, Johannes Graumann johannes_graum...@web.de wrote: Indeed. Thank you. Is there a global switch analogous to theme_set(theme_bw())? thanks for your help, Joh On Wednesday 10 March 2010 10:29:05 baptiste auguie wrote: Hi, last_plot() + scale_fill_grey() should do it HTH, baptiste On 10 March 2010 09:46, Johannes Graumann johannes_graum...@web.de wrote: Hello, I'd like to sitch to a monochrome/bw color-palette for the filling of geom_bar-bars (produced via qplot as in the example below). Hours of googling didn't yield anything useful, so I thought, I'd just ask ... Thanks, Joh library(ggplot2) qplot(factor(cyl), data=mtcars, geom=bar, fill=factor(cyl)) __ 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.
Re: [R] write.fortran
Thanks, it is indeed a bit cleaner. I was hoping for a more generic solution but I guess people use cat() and sprintf() in such cases. Thanks, baptiste On 10 March 2010 12:46, Berend Hasselman b...@xs4all.nl wrote: baptiste auguie-5 wrote: This is a lot easier for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n)) Berend Oops. Missing sep= and closing }. The correct line is for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n),sep=)} Berend -- View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587272.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] write.fortran
That's perfect, thanks! baptiste On 10 March 2010 14:47, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this. It takes a matrix or data.frame, codes= which is a vector of % codes and other text and file= and other cat arguments. If no ... args are given it outputs the character string that it would have handed to cat. write.mat - function(mat, codes, sep = , ...) { s - do.call(sprintf, unname(c(paste(codes, collapse = ), as.data.frame(m if (length(list(...)) 0) cat(s, sep = sep, ...) else s } # test - m is from original post write.mat(m, rep(c(%i, %15.7e, \n), c(1, 6, 1)), file = ) On Wed, Mar 10, 2010 at 7:52 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: Thanks, it is indeed a bit cleaner. I was hoping for a more generic solution but I guess people use cat() and sprintf() in such cases. Thanks, baptiste On 10 March 2010 12:46, Berend Hasselman b...@xs4all.nl wrote: baptiste auguie-5 wrote: This is a lot easier for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n)) Berend Oops. Missing sep= and closing }. The correct line is for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n),sep=)} Berend -- View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587272.html Sent from the R help mailing list archive at Nabble.com. __ 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-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] data.frame question
Hi, try this, data.frame(x,as.numeric(x %in% y)) HTH, baptiste On 7 March 2010 21:06, joseph jdsan...@yahoo.com wrote: hello can you show me how to create a data.frame from two factors x and y. column 1 should be equal to x and column 2 is 1 if it is common to y and 0 if it is not. x=factor(c(A,B,C,D,E,F,G)) y=factor(c(B,C,G)) the output should look like this: A 0 B 1 C 1 D 0 E 0 F 0 G 1 Thanks Joseph Dhahbi [[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-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] how to make this sequence: 1,2,3,4,5,4,3,2,1
c(x - 1:5, rev(x[-length(x)])) On 5 March 2010 07:04, kensuguro magronb...@gmail.com wrote: I'm just beginning R, with book Using R for Introductory Statistics, and one of the early questions has me baffled. The question is, create the sequence: 1,2,3,4,5,4,3,2,1 using seq() and rep(). Now, as a programmer, I am punching myself to not be able to figure it out.. I mean, as simple as a for loop, but using seq, I am stumped. I would think c(1:5, 4:1) would be the brute force method with very non intelligent coding.. there has to be a way to make the turning point (in this case 5) parametric right? So you could change it later and the sequence will reflect it. -- View this message in context: http://n4.nabble.com/how-to-make-this-sequence-1-2-3-4-5-4-3-2-1-tp1579245p1579245.html Sent from the R help mailing list archive at Nabble.com. __ 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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] Three most useful R package
Hi, The 3 packages I load most often are my own; typically I make a new package for every new job. It automatically loads other packages as dependencies (top-ranked are ggplot2, reshape, plyr) as well as my data and functions I'm currently working with. If some functions evolve further towards a more general use I may consider moving them to a clean package on r-forge (ultimately CRAN, but I haven't gone that far). Best, baptiste On 2 March 2010 21:13, Ralf B ralf.bie...@gmail.com wrote: Hi R-fans, I would like put out a question to all R users on this list and hope it will create some feedback and discussion. 1) What are your 3 most useful R package? and The 3 packages I load most often are my own, typically I make a new package for a new job. It automatically loads 2) What R package do you still miss and why do you think it would make a useful addition? Pulling answers together for these questions will serve as a guide for new users and help people who just want to get a hint where to look first. Happy replying! Best, Ralf __ 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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] Preserving lists in a function
Hi, I think I would follow this approach too, using updatelist() from the reshape package, updatelist - function (x, y) { common - intersect(names(x), names(y)) x[common] - y[common] x } myfunction=function(list1=NULL, list2=NULL, list3=NULL){ list1=updatelist(list(variable1=1, variable2=2, variable3=3), list1) list2=updatelist(list(variable1=variable1, variable2=variable2, variable3=variable3), list2) list3=updatelist(list(variable1=character, variable2=24, variable3=c(0.1,0.1,0.1,0.1), variable4=TRUE), list3) return(list(list1=list1,list2=list2,list3=list3)) } Best regards, baptiste On 27 February 2010 01:51, Don MacQueen m...@llnl.gov wrote: Barry explained your first puzzle, but let me add some explanation and examples. tmpfun - function( a =3 ) {a} tmpfun() [1] 3 tmpfun(a='x') [1] x Inside the function, the value of the argument is whatever the user supplied. The default is replaced by what the user supplies. There is no mechanism for retaining the default structure and filling in any missing parts. R never preserves the defaults when the user supplies something other than the default. For example, and using your function, myfunction(list1='x') $list1 [1] x $list2 $list2$variable1 [1] variable1 $list2$variable2 [1] variable2 $list2$variable3 [1] variable3 $list3 $list3$variable1 [1] character $list3$variable2 [1] 24 $list3$variable3 [1] 0.1 0.1 0.1 0.1 $list3$variable4 [1] TRUE myfunction(list1=data.frame(a=1:2, b=c('x','y'))) $list1 a b 1 1 x 2 2 y $list2 $list2$variable1 [1] variable1 $list2$variable2 [1] variable2 $list2$variable3 [1] variable3 $list3 $list3$variable1 [1] character $list3$variable2 [1] 24 $list3$variable3 [1] 0.1 0.1 0.1 0.1 $list3$variable4 [1] TRUE What you put in is what you get out. I don't know that I would deal with this the way Barry did. I would probably write code to examine the structure of what the user supplies, compare it to the required structure, and then fill in. myf - function(l1, l2, l3) { if (missing(l1)) { ## user did not supply l1, so set it = to the default l1 - list(v1=1, v2=2, v3=3) } else if (!is.list(l1)) { ## user must supply a list, if not, it's an error stop('l1 must be a list') } else { ## user has at least supplied a list ## now write code to check the names of the list that the user supplied ## make sure the names that the user supplied are valid, if not, stop() ## if the user supplied too few elements, fill in the missing ones ## if the user supplied too many elements stop() ## if the user supplied all the correct elements, with all the correct names, use what the user supplied } Looks complicated; maybe Barry's way is better... -Don At 5:56 PM -0500 2/26/10, Shang Gao wrote: Dear R users, A co-worker and I are writing a function to facilitate graph plotting in R. The function makes use of a lot of lists in its defaults. However, we discovered that R does not necessarily preserve the defaults if we were to input them in the form of list() when initializing the function. For example, if you feed the function codes below into R: myfunction=function( list1=list (variable1=1, variable2=2, variable3=3), list2=list (variable1=variable1, variable2=variable2, variable3=variable3), list3=list (variable1=character, variable2=24, variable3=c(0.1,0.1,0.1,0.1), variable4=TRUE)) {return(list(list1=list1,list2=list2,list3=list3))} By definition, the values associated with each variable in the lists would be the default unless the user impute a different value while executing the function. But a problem arises when a variable in the list is left out completely (not imputed at all). An example is shown below: myfunction( list1=list (variable1=1, variable2=2), #variable 3 deliberately left out list2=list (variable1=variable1, variable3=position changed, variable2=variable2), list3=list (variable1=character, variable2=24, variable4=FALSE)) #variable 3 deliberately left out #The outcome of the above execution is shown below: $list1 $list1$variable1 [1] 1 $list1$variable2 [1] 2 #list1$variable3 is missing. Defaults in function not assigned in this execution $list2 $list2$variable1 [1] variable1 $list2$variable3 [1] position changed $list2$variable2 [1] variable2 $list3 $list3$variable1 [1] character $list3$variable2 [1] 24 $list3$variable4 [1] FALSE #list3$variable3 is missing. Defaults in function not assigned in this execution We later realized that the problem lies in list() commands. Hence, we tried to enforce the defaults on
Re: [R] Preserving lists in a function
Point well taken --- grid::gpar() is also a good example; I'll make use of your suggestion in my future coding. Best, baptiste On 27 February 2010 15:02, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Sat, Feb 27, 2010 at 11:29 AM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Or use modifyList which is in the core of R. All these solutions appear to be adding on more and more code with less and less semantics. Result: messy code which is harder to read and debug. It seems that the arguments should have proper constructors with meaningful names. A bit like an optimisation function with a control argument. What's better: o = optimmy(f, control=list(niter=10,ftol=0.01)) or o = optimmy(f,control=control(niter=10,ftol=0.01)) here you are explicitly constructing a 'control' object that has the options for controlling the optimiser, and it will have its own sensible defaults which the user can selectively override. It seems to be the correct paradigm for collecting related parameters, and indeed is used in lots of R code. Done this way you get selectively overridable arguments, a meaningful name, readable code, leveraging the built-in defaults system, and the possibility of sticking consistency checks in your control() function. Tell me where that is not made of pure win? Barry __ 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] Plotting a Trivial Matrix
Hi, A minimalist example using Grid graphics, library(RGraphics) bwImage - function(m, cols=c(white, black), draw=TRUE, gp=gpar()){ g - imageGrob(nrow(m), ncol(m), cols=cols[m+1], gp=gp) if(draw) grid.draw(g) return(g) } m - matrix(rnorm(200) 0, ncol=20) bwImage(m) HTH, baptiste On 26 February 2010 09:29, Lorenzo Isella lorenzo.ise...@gmail.com wrote: Dear All, Consider a matrix (N x N) where each entry is either zero or one (can hardly get any simpler). Now, I would like to plot it as a 'chessboard' where every matrix entry is a black (1) or white (0) square. Whatever tool I use to plot it, it should not try to interpolate the data at all. I found some online references http://www.phaget4.org/R/image_matrix.html but probably I can resort to something much simpler. Can anyone provide me with a simple example I can modify later on? Many thanks Lorenzo __ 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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] Plotting a Trivial Matrix
On 26 February 2010 11:12, Lorenzo Isella lorenzo.ise...@gmail.com wrote: Thanks Augustine and Jim for the prompt reply. You both answered my question. To avoid another post, I would simply like to know if something along these lines is doable also with ggplot2. Many thanks Lorenzo Augustine??? Anyhow, with ggplot2, m - matrix(rnorm(200) 0, ncol=20) require(ggplot2) d - melt(m) qplot(X1, X2, data=d, fill=value, geom=tile) + scale_fill_manual(values=c(white, black)) HTH, baptiste __ 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] How to fill in a region with different patterns?
Hi, If you are curious you might like to try a highly experimental Grid function I wrote some time ago, library(grid) source(http://gridextra.googlecode.com/svn/trunk/R/patternGrob.r;) grid.newpage() grid.pattern(x=seq(1/6, 5/6, length=6), width=unit(1/8,npc), height=unit(0.5,npc), motif.width=unit(10, mm), pattern=c(1:6), orientation=45, motif.alpha=0.5, motif.cex=c(1, 0.5), motif.col=1:2, motif.fill=NA, gp=gpar(fill=blue, lwd=2, alpha=0.5), clip=T) If you really insist on using shading patterns despite Greg's sound advice, it might give you some inspiration. HTH, baptiste On 25 February 2010 04:34, St.Jeff Shang mathsh...@yahoo.com wrote: Hi to all, Here is a question which I cannot solve. Appreciate so much for any suggestions! I have a squared region which is irregularly divided into many rectangular patches. Each patch is associated with a value, and two patches possibly share a common value. I hope to fill in each patch a pattern according to its value. For instance, if a patch has value 1, then I fill in that patch with pattern A; if it has value 2, then fill in it with pattern B,... The pattern is something like a rectangular region with certain colored lines in it. For instance, pattern A may be a white rectangular region filled in by two thick black lines; pattern B may be a white rectagular region filled in by 10 thick black lines,... Is this available in matlab? I have serached polygnon in matlab user guide and found a fill function may work, however, there is still a long way to finally complete it. Many thanks again! Jeff [[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. -- Baptiste Auguié Departamento de Química Física, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides __ 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] Exporting Graphs
Hi, just to make sure, you didn't forget to close the device with dev.off() ? baptiste On 21 February 2010 20:48, Karthik kwr...@gmail.com wrote: Hello Tal, This is the code. hist(rnorm(100)) jpeg(histogram.jpeg) --- Even when I decrease the quality, I still have the same problem. hist(rnorm(100)) jpeg(histogram.jpeg,quality=30) Thank you for taking a look. Karthik On Sun, Feb 21, 2010 at 11:32 AM, Tal Galili tal.gal...@gmail.com wrote: Hi Karthik, Please give a sample code of what it is that you are doing that is causing this. Also, have a look at: ?pdf Or ?png Cheers, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- On Sun, Feb 21, 2010 at 9:27 PM, Karthik kwr...@gmail.com wrote: I am a beginner to R, and am working on exporting graphs. Even when I reduce the quality, it takes 30 or 40 minutes to export the graph. Does anyone have suggestions on how to make it faster? Thank you! Karthik __ 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-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] Bind field of a list
Hi, Try this, unlist(test) or do.call(c, test) HTH, baptiste On 19 February 2010 15:19, statquant colin.uman...@barcap.com wrote: Hello all I am new in R and so easy stuff are difficult... let say that I have a list test - list(a=c(x,v),b=c(n,m)) how can I without a loop get test$a bind with test$b (obviously in real life their would be many fields) Cheers and thanks -- View this message in context: http://n4.nabble.com/Bind-field-of-a-list-tp1561676p1561676.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] evaluate variable within expression - how?
Hi, Try with bquote, plot.new() myText - some text text(.5, .5, bquote(bold(.(myText basically, bquote( .(myText) ) performs the substitution before applying bold() (see ?bquote). HTH, baptiste On 14 February 2010 17:36, Mark Heckmann mark.heckm...@gmx.de wrote: # I want to plot bold text. The text should depend on a variable containing a character string. plot.new() text(.5, .5, expression(bold(Some text))) # now I would like to do the same replacing some text by a variable. plot.new() myText - some text text(.5, .5, expression(bold(myText))) # This obviouyls does not work. # How can I combine substitute, parse, paste etc. do get what I want? # And could someone add a brief explanation what happens, as I find parse, expression, deparse, substitute etc. not easy to understand. Thanks, Mark ––– Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com [[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-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] Problems with boxplot in ggplot2:qplot
Hi, it's hard to tell what's wrong without a reproducible example, but I noted two things: - AFAIK there is no plot method for ggplot2. You probably meant print(p) instead - if you map x to factor(month), I think it will be incompatible with your xlim values range(month). HTH, baptiste On 14 February 2010 19:55, Dimitri Shvorob dimitri.shvo...@gmail.com wrote: Dataframe closed contains balances of closed accounts: each row has month of closure (Date-type column month) and latest balance. I would like to plot by-month distributions of balances. A qplot call below produces several warnings and no output. Can anyone help? Thank you. PS. A really basic task, very similar to the examples on p. 71 of the ggplot2 book, apart from a Date grouping column; I am quite surprised to have problems with it. lattice package to the rescue? qplot(factor(month), balance, data = closed, geom = boxplot, xlim = range(closed$month)) There were 13 warnings (use warnings() to see them) warnings() Warning messages: 1: Removed 1 rows containing missing values (stat_boxplot). 2: Removed 7 rows containing missing values (geom_point). 3: Removed 5 rows containing missing values (geom_point). 4: Removed 8 rows containing missing values (geom_point). 5: Removed 3 rows containing missing values (geom_point). 6: Removed 5 rows containing missing values (geom_point). 7: Removed 2 rows containing missing values (geom_point). 8: Removed 12 rows containing missing values (geom_point). 9: Removed 2 rows containing missing values (geom_point). 10: Removed 1 rows containing missing values (geom_point). 11: Removed 2 rows containing missing values (geom_point). 12: Removed 3 rows containing missing values (geom_point). 13: Removed 4 rows containing missing values (geom_point). p = qplot(factor(month), balance, data = closed, geom = boxplot, xlim = range(closed$month)) plot(p) Error in plot.window(...) : need finite 'xlim' values -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1555338.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] unexpected results with higher-order functions and lapply
Hi, I believe it's lazy evaluation. See ?force HTH, baptiste On 14 February 2010 20:32, Jyotirmoy Bhattacharya jyotir...@jyotirmoy.net wrote: I want to use lapply and a function returning a function in order to build a list of functions. genr1 - function(k) {function() {k}} l1 - lapply(1:2,genr1) l1[[1]]() [1] 2 This was unexpected. I had expected the answer to be 1, since that is the value k should be bound to when genr1 is applied to the first element of 1:2. By itself genr1 seems to work fine. genr1(5)() [1] 5 I defined a slightly different higher-order function: genr2 - function(k) {k;function() {k}} l2 - lapply(1:2,genr2) l2[[1]]() [1] 1 This gives the answer I expected. Now I am confused. The function returned by genr2 is exactly the same function that was being returned by genr1. Why should evaluating k make a difference? I am using R 2.9.2 on Ubuntu Linux. Jyotirmoy Bhattacharya [[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-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] Flattening Graphics
Hi, On 11 February 2010 22:14, Paul Murrell p.murr...@auckland.ac.nz wrote: Hi baptiste auguie wrote: Hi, You could try the grid.grab() function in R devel if your graphics use the Grid package. It will read the graphical output as a bitmap which grid.grab() does NOT grab a bitmap version of the current picture. It grabs all of the (grid-rendered) grobs in the current picture. You might be thinking of grid.cap(), which is currently only in the development version of R. Oops, that's an unfortunate typo! Yes, I meant grid.cap() of course, but failed to check the actual name, sorry. But if you want a raster version of the current plot, it would make more sense to use a raster device, like png(). It was my understanding that the output may contain several pages of plots, and I don't know of a bitmap device in R which can do that. All the best, baptiste Paul you can then export in a multipage pdf. It may not be really flattening per se but that would definitely help with the viewing speed. HTH, baptiste On 11 February 2010 05:42, Dario Strbenac d.strbe...@garvan.org.au wrote: Hello, This question is a nightmare to search for, as I get so many irrelevant results. What I'm interested in doing if I have many pages of plots and I want to keep them together in the same document, say a PDF, is there a way to flatten all the dot plots and graphics, so that they don't take a long time to load on a slow computer in Adobe Reader, without using external programs outside of R ? Thanks, Dario. - Dario Strbenac Research Assistant Cancer Epigenetics Garvan Institute of Medical Research Darlinghurst NSW 2010 Australia __ 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. -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 p...@stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/ __ 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] Flattening Graphics
Hi, You could try the grid.grab() function in R devel if your graphics use the Grid package. It will read the graphical output as a bitmap which you can then export in a multipage pdf. It may not be really flattening per se but that would definitely help with the viewing speed. HTH, baptiste On 11 February 2010 05:42, Dario Strbenac d.strbe...@garvan.org.au wrote: Hello, This question is a nightmare to search for, as I get so many irrelevant results. What I'm interested in doing if I have many pages of plots and I want to keep them together in the same document, say a PDF, is there a way to flatten all the dot plots and graphics, so that they don't take a long time to load on a slow computer in Adobe Reader, without using external programs outside of R ? Thanks, Dario. - Dario Strbenac Research Assistant Cancer Epigenetics Garvan Institute of Medical Research Darlinghurst NSW 2010 Australia __ 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.
Re: [R] color blending and transparency
Hi, Adding two semi-transparent colours results in non-intuitive colour mixing (a mystery for me anyway). Is it additive (light), substractive (paint), or something else? Consider the following example, depending on the order of the two layers the overlap region is either purple or dark red. I have no idea why. png(testingOrder.png) plot.new() # Red below rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) # Blue below rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) dev.off() Best, baptiste On 3 February 2010 11:15, Jim Lemon j...@bitwrit.com.au wrote: On 02/03/2010 08:43 PM, bluecuttlefish wrote: I am using ggplot and posted this question at that helplist. It was suggested that I try a more general R-help list for a possible solution to this problem. Within ggplot, I am using geom_area with red and blue and expect where they overlap should be purple. But instead, it's dark red. Playing with alpha and with different colors doesn't seem to solve the problem. Here's a very simple reproducible example R --arch x86_64 library(ggplot2) x-c(24,55,69,73) y-c(44,56,12,90) z-c(1,2,3,4) a-data.frame(pos=z, y=y, x=x) ex- ggplot(data=a, aes(pos)) + geom_area(aes(y = y),fill=navyblue, alpha = 0.7, position=identity) + geom_area(aes(y = x), fill= darkred, alpha = 0.7,position=identity ) + opts(panel.background = theme_rect(fill = white)) ex Likewise, with blue and yellow, I would expect overlap to be green. Are there any solutions to this that would allow color overlaps to lead to the expected color? Hi bluecuttlefish, Think ink - should be easy. Only certain devices support transparency. Have you tried this on the pdf device? Jim __ 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] group factor levels
Dear list, I cannot find an elegant solution to this problem. I have a factor f containing several levels (5) and I wish to create a new factor of the same length with fewer levels (2). This new factor should therefore group together some levels of the original data. Ideally this grouping would be at random, i.e I would not group together the first 2 levels of f, then the following 3, etc. Below is a minimal example (my real problem has more levels, otherwise I would do the operation manually...) f - factor(rep(sample(letters[1:5], 20, repl=TRUE), each=10)) # permute the levels in random order disorder - sample(levels(f), length(levels(f))) # new levels matching the old ones new.lev - rep(LETTERS[1:2], length=length(disorder)) # associate old levels to new ones groups - split(disorder, new.lev) # test each element of f for its new category test - lapply(groups, function(g) f %in% g) # f2 is the new factor, initialized with f f2 - as.character(f) # recursively modify f2 sapply(seq_along(test), function(ii) f2[test[[ii]]] - names(test[ii])) # make it a factor f2 - factor(f2) Any suggestions are very welcome, I must have missed something more obvious! Best regards, baptiste __ 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] group factor levels
As always the question seems silly after you've learned the answer! Thanks a lot, baptiste On 3 February 2010 15:06, Petr PIKAL petr.pi...@precheza.cz wrote: # order levels f.t-factor(f, levels=disorder) # change levels levels(f.t) - new.lev all.equal(f.t,f2) [1] TRUE Regards Petr Best regards, baptiste __ 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.
Re: [R] color blending and transparency
That makes perfect sense, thank you, except that I'm not sure where the white comes from when I set the background to transparent? png(testingOrder.png, bg = transparent) plot.new() par(bg=transparent) rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) dev.off() Still produces two different overlap colours, although I *think* only two colours are involved. What I have I missed here? Thanks, baptiste On 3 February 2010 15:17, Duncan Murdoch murd...@stats.uwo.ca wrote: On 03/02/2010 8:50 AM, Ken Knoblauch wrote: baptiste auguie baptiste.auguie at googlemail.com writes: Adding two semi-transparent colours results in non-intuitive colour mixing (a mystery for me anyway). Is it additive (light), substractive (paint), or something else? Consider the following example, depending on the order of the two layers the overlap region is either purple or dark red. I have no idea why. png(testingOrder.png) plot.new() # Red below rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) # Blue below rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) I think it's a fairly simple calculation. In the first example: We are writing red (1,0,0) at alpha=0.5 onto white (1,1,1), so we get a mixture of half existing and half new, i.e. (1,0.5,0.5). Then we write blue (0,0,1) at alpha 0.5 onto that, giving (0.5, 0.25, 0.75). In the second pair, the first write yields (0.5,0.5,1), and the second yields (0.75, 0.25, 0.5). So this is like mixing paints: you don't get the same colour if you mix equal parts red and white, then take equal parts of that mixture with blue, as you get if you put the blue in first. You've got less red in the first mixture than in the second. You would get the same color in both mixtures if you didn't mix the white in: # Red below rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=1)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) # Blue below rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=1)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) Duncan Murdoch dev.off() I would expect overlaid transparencies to act like filters and multiply, producing so-called subtractive color mixing, so blue and yellow gives green. Interestingly, however, overlaying filters is not necessarily a commutative operation, since a transparent filter can yield an additive component (through scatter, for example) though I suspect that the non-commutativity comes about in R because these rules apply to physical lights, filters and surfaces and in R, it is some uncalibrated combination of frame buffer values that is being used. Best, baptiste Ken __ 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.
Re: [R] color blending and transparency
On MacOSX I can tell Preview or Photoshop not to use a white background yet the mixing still shows a difference (with either pdf or png for that matter). So I guess it's something to do with mixing colours with the transparent channel as you say. I'll try to find the reason in the source code later (I haven't found any documentation on this so far). Thanks, baptiste On 3 February 2010 16:38, Duncan Murdoch murd...@stats.uwo.ca wrote: On 03/02/2010 9:38 AM, baptiste auguie wrote: That makes perfect sense, thank you, except that I'm not sure where the white comes from when I set the background to transparent? You'd have to check the png device documentation or source code to find out what it does when you mix half red with half transparent. When I view a .png file in the default viewer in Windows 7, setting the background to transparent displays it as white, I don't see the things behind the window showing through. I don't know if it's the viewer or the file determining that. Duncan Murdoch png(testingOrder.png, bg = transparent) plot.new() par(bg=transparent) rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) dev.off() Still produces two different overlap colours, although I *think* only two colours are involved. What I have I missed here? Thanks, baptiste On 3 February 2010 15:17, Duncan Murdoch murd...@stats.uwo.ca wrote: On 03/02/2010 8:50 AM, Ken Knoblauch wrote: baptiste auguie baptiste.auguie at googlemail.com writes: Adding two semi-transparent colours results in non-intuitive colour mixing (a mystery for me anyway). Is it additive (light), substractive (paint), or something else? Consider the following example, depending on the order of the two layers the overlap region is either purple or dark red. I have no idea why. png(testingOrder.png) plot.new() # Red below rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) # Blue below rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) I think it's a fairly simple calculation. In the first example: We are writing red (1,0,0) at alpha=0.5 onto white (1,1,1), so we get a mixture of half existing and half new, i.e. (1,0.5,0.5). Then we write blue (0,0,1) at alpha 0.5 onto that, giving (0.5, 0.25, 0.75). In the second pair, the first write yields (0.5,0.5,1), and the second yields (0.75, 0.25, 0.5). So this is like mixing paints: you don't get the same colour if you mix equal parts red and white, then take equal parts of that mixture with blue, as you get if you put the blue in first. You've got less red in the first mixture than in the second. You would get the same color in both mixtures if you didn't mix the white in: # Red below rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=1)) rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5)) # Blue below rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=1)) rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5)) Duncan Murdoch dev.off() I would expect overlaid transparencies to act like filters and multiply, producing so-called subtractive color mixing, so blue and yellow gives green. Interestingly, however, overlaying filters is not necessarily a commutative operation, since a transparent filter can yield an additive component (through scatter, for example) though I suspect that the non-commutativity comes about in R because these rules apply to physical lights, filters and surfaces and in R, it is some uncalibrated combination of frame buffer values that is being used. Best, baptiste Ken __ 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-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] color blending and transparency
Thanks for this complementary information. My head itches slightly when reading about these virtual layers with unidirectional absorption and reflection properties but I guess that's imputable to my personal background as a physicist. I still have a few questions, - is this behavior documented? (I'm happy to look at the source, but I'm not even sure if it's in grDevices, device specific, or somewhere else altogether) - have there been any attempts at implementing other options for colour blending? It would be nice to be able to switch between additive and substrative colour mixing rules on occasion, but as far as I understand the scheme we discussed is hard coded deep into R's internals (correct?). An option not to use the background at all in the mixing / reflection process would be great already. Thanks again, baptiste On 3 February 2010 17:04, Thomas Lumley tlum...@u.washington.edu wrote: My mental model for this, which I haven't bothered to check against the actual algorithms, is that colors are composed of reflective/absorbing pigment particles and that alpha says how densely they are packed. Alpha=0 means all the light gets through to bounce of what ever is below, eventually to the white paper, and alpha=1 means that all the light is reflected from the top layer of paint. With 50% blue over 50% red, you reflect 50% of the blue light and absorb 50% of the red and green light in the top layer of paint. Of the remaining light, 50% of the red is reflected and 50% of the green and blue absorbed by the particles in the bottom layer of paint. Anything that makes it through will reflect off the white paper. There is the additional complication that a transparent background still behaves as if it had white paper behind it (it's drawn on an acetate sheet which you lay on paper to see it more clearly). -thomas Thomas Lumley Assoc. Professor, Biostatistics tlum...@u.washington.edu University of Washington, Seattle __ 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] parsing files for plot
Hi, Hadley recently proposed a strategy using plyr for a very similar problem, listOfFiles - list.files() names(listOfFiles) - basename(listOfFiles) library(plyr) d - ldply(listOfFiles, scan) Even if you don't want to use plyr, it's always better to group things in a list rather than clutter your workspace with lots of assign()ed variables. HTH, baptiste On 30 January 2010 13:19, Maxim deeeperso...@googlemail.com wrote: Hi, I have many files containing one column of data. I like to use the scan function to parse the data. Next I like to bind to a large vector. I try this like: count-1 files - list.files() # all files in the working directory for(i in files) { tmp - scan(i) assign(files[count], tmp) count-count+1 } This part works! Now I like to plot the data in a boxplot. Usually I do this from individual vectors like: comb - data.frame(dat = c(vector1, vector2 ..), ind = c(rep('vector1', length(vector1))...)) boxplot(dat ~ ind, data = comb) But how do I do this i a loop? I know the vector names (according to the filenames in the working directory), but I do not how to access them in my R code after having assigned the names. I guess the lapply or dply from the plyr library can do this, but I seem not to be able to do it. Is there a way to do this? gma [[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-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] parsing files for plot
Hi again, Below are two versions, depending on whether you want to use scan or read.table, ## with scan library(reshape) listOfFiles - list.files() d - llply(listOfFiles, scan) names(d) - basename(listOfFiles) melt(d) ## with read.table listOfFiles - list.files() names(listOfFiles) - basename(listOfFiles) library(plyr) ldply(listOfFiles, read.table) Note, I tested this code with the following files, system(mkdir dummy) setwd(paste(getwd(), /dummy, sep=)) files - replicate(5, rnorm(sample(3:20, 1)), simplify=FALSE) names - paste(datafile, letters[1:5],.txt, sep=) l_ply(seq_along(files), function(ii, ...) write.table(x=files[[ii]], file=names[ii], ... ), row.names = F, col.names = F) HTH, baptiste On 30 January 2010 14:23, Maxim deeeperso...@googlemail.com wrote: Hi, my data is really not spectacular, each of the 6 files (later several hundred) contains correlation coefficients in plain text format like: 0.923960073 0.923960073 0.612571344 0.064183275 0.007733399 -0.315444372 -0.064591277 -0.268336142 ... with between 1000-13000 rows. Scanning from the directory works, as this script: comb-data.frame() count-0 files - list.files() # all files in the working directory for(i in files) { count-count+1 tmp - scan(i) assign(files[count], tmp) if (i ==1) comb-data.frame(dats=c(tmp), index=c(rep(files[1], length(tmp else combadd-data.frame(dats=c(tmp), index=c(rep(files[count], length(tmp comb-rbind(comb,combadd) } boxplot(dats ~ index, data = comb) works just great. There is no additional files in the folder. But look, how much code for such a simple task. I'd definitely prefer the plyr solution. Maxim 2010/1/30 baptiste auguie baptiste.aug...@googlemail.com Why don't you post an example of what your input files look like? (to the list, not just to me!) A reproducible example is always required if you want a good answer. Note that if you are scanning *all* files in the working directory, you may also be scanning the R file containing your instructions which won't have the correct format, obviously. Best, baptiste On 30 January 2010 13:52, Maxim deeeperso...@googlemail.com wrote: Hi, thanks, that looks much more elegant than what I managed to accomplish in meantime: count-1 files - list.files() # all files in the working directory for(i in files) { tmp - scan(i) assign(files[count], tmp) if (i ==1) comb-data.frame(dats=c(tmp), index=c(rep(files[1], length(tmp else combadd-data.frame(dats=c(tmp), index=c(rep(files[count], length(tmp comb-rbind(comb,combadd) count-count+1 } boxplot(dats ~ index, data = comb) This code works, unfortunately the plots get plotted in a different order than expected (appears to be more or less random to me). Why is this? Concerning your code: I get an error like: Read 2652 items Read 3310 items Read 1096 items Read 2177 items Read 11387 items Read 12503 items Error in list_to_dataframe(res, attr(.data, split_labels)) : Results are not equal lengths hmmh? Maxim 2010/1/30 baptiste auguie baptiste.aug...@googlemail.com Hi, Hadley recently proposed a strategy using plyr for a very similar problem, listOfFiles - list.files() names(listOfFiles) - basename(listOfFiles) library(plyr) d - ldply(listOfFiles, scan) Even if you don't want to use plyr, it's always better to group things in a list rather than clutter your workspace with lots of assign()ed variables. HTH, baptiste On 30 January 2010 13:19, Maxim deeeperso...@googlemail.com wrote: Hi, I have many files containing one column of data. I like to use the scan function to parse the data. Next I like to bind to a large vector. I try this like: count-1 files - list.files() # all files in the working directory for(i in files) { tmp - scan(i) assign(files[count], tmp) count-count+1 } This part works! Now I like to plot the data in a boxplot. Usually I do this from individual vectors like: comb - data.frame(dat = c(vector1, vector2 ..), ind = c(rep('vector1', length(vector1))...)) boxplot(dat ~ ind, data = comb) But how do I do this i a loop? I know the vector names (according to the filenames in the working directory), but I do not how to access them in my R code after having assigned the names. I guess the lapply or dply from the plyr library can do this, but I seem not to be able to do it. Is there a way to do this? gma [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read
Re: [R] evaluating expressions with sub expressions
Hi, Would this do as an alternative syntax? g1 - quote(1/Tm) mat - list(0, bquote(f1*s1*.(g1))) vals - data.frame(f1=1, s1=.5, Tm=2) sapply(mat, eval, vals) HTH, baptiste On 29 January 2010 17:51, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: Hallo I'm having trouble figuring out how to evaluate an expression when one of the variables in the expression is defined separately as a sub expression. Here's a simplified example mat - expression(0, f1*s1*g1) # vector of formulae g1 - expression(1/Tm) # expansion of the definition of g1 vals - data.frame(f1=1, s1=.5, Tm=2) # one set of possible values for variables before adding this sub expression I was using the following to evaluate mat sapply(mat, eval, vals) Obviously I could manually substitute in 1/Tm for each g1 in the definition of mat, but the actual expression vector is much longer, and the sub expression more complicated. Also, the subexpression is often adjusted for different scenarios. Is there a simple way of changing this or redefining mat so that I can define g1 like a macro to be used in the expression vector. Thanks! Jennifer __ 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.
Re: [R] ggplot2 theme for custom point colors?
Hi, I remember asking a similar question some time ago, I don't know if the matter has evolved since then, http://groups.google.com/group/ggplot2/browse_frm/thread/a3df8a0d1ee335fb/e3bedd50fb9bd567?lnk=gstq=theme#e3bedd50fb9bd567 There's also set_default_scale, somewhat related to your question. baptiste 2010/1/27 Kevin Wright kw.s...@gmail.com: 1. Is it possible to set the point colors in a ggplot2 theme? For example, to default to these colors: scale_colour_manual(value = c(red, orange, yellow, green, blue) 2. Is it possible to set this theme in .Rprofile, similar to: my.lattice.colors = function () { ...etc.} options(lattice.theme=.mw.lattice.colors) -- Kevin Wright [[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-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] color matrix
Hi, It's easy with ggplot2, cols = matrix(c(#F7FBFF, #DEEBF7, #C6DBEF, #9ECAE1, #6BAED6, #4292C6, #2171B5, #08519C ,#08306B), ncol=3) library(ggplot2) m = melt(cols) qplot(factor(X1),factor(X2),data=m, fill=value, geom=tile) + scale_fill_identity() HTH, baptiste 2010/1/27 evgeny55 evgen...@yahoo.com: Hi, Is it possible to create a heatmap for say a 3x3 matrix of data where every color is pre-assigned? I can easily create a matrix of hex colors but when I try to use that matrix as the parameter for the 'col' option it doesn't work. If it's possible to just display a matrix of hex colors that would work as well thanks -- View this message in context: http://n4.nabble.com/color-matrix-tp1312078p1312078.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Fwd: Re: Graph color
Hi, Try this, x - seq(0, 10, len = 100) y - jitter(sin(x), 1000) old.par - par() par(bg=grey(0.5)) plot(x, y, new = TRUE, t = n) lims - par(usr) plot(x, y, col = 1, panel.first = { rect(lims[1], lims[3], lims[2], lims[4], col = lightblue) }) HTH, baptiste 2010/1/26 narillosdesan...@gmail.com: No mate, Sorry first of all about my indefinition (I´m Spanish, I´m improving everyday but a long road to the perfection). Sorry pleae. Second, also it is diffcoult sometimes to express what we try (sorry and many thanks just for reading of course for helping). Imagine you plot X=[2 4 6 8] front a Y=[6 5 8 7] When you plot it by default all is white what I want is to use par(br=gray) to make the graph gray but I want that the area (the imaginary square defined by the axis) not to be gray I want to deffine its colour by ie lightblue. So the image will be a square image outside gray and on the axis area (not the dots, points or bar plots) the area in lightblue. I don´t now if I have expressed well if not latter I will send an example, ok? Many thanks -- Mensaje reenviado -- De: Kyle. ambe...@gmail.com Fecha: Asunto: Re: [R] Graph color Para: Jose Narillos de Santos narillosdesan...@gmail.com CC: r-help@r-project.org If I understand what you want correctly, you'll probably want to use the col argument in whatever base graphics function you're using, rather than changing something in the graphical parameters. For example, if I wanted to add red points to an existing plot, I would use something like points(c(1:10), col=red) Or, if I wanted to generate a barplot using a shading color other than gray, barplot(c(1:10), col=steelblue) Does that answer your question? Kyle H. Ambert Fellow, National Library of Medicine Department of Medical Informatics Clinical Epidemiology Oregon Health Science University On Tue, Jan 26, 2010 at 6:44 AM, Jose Narillos de Santos narillosdesan...@gmail.com wrote: Hi all I want to apply different colors on a simple plot: If I type par(br=gray) before a plot it puts all the image in gray but (imagine I run a simple plot) want to let the centrall box (where the dots are plotted) in white or image in lightblue. Can anyone guide me to apply this second step (make the box where the series are plotted in different colours). Thanks in advance. [[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. [[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-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] column selection in list
Hi, Try this, a = replicate(3, data.frame(x=1:10, y=rnorm(10)), simplify=FALSE) lapply(a, [, y) HTH, baptiste 2010/1/22 Ivan Calandra ivan.calan...@uni-hamburg.de: Hi everybody! I have a (stupid) question but I cannot find a way to do it! I have a list like: SPECSHOR_tx_Asfc $cotau SPECSHOR Asfc.median 38 cotau 381.0247 39 cotau 154.6280 40 cotau 303.3219 41 cotau 351.2933 42 cotau 156.5327 $eqgre SPECSHOR Asfc.median 145 eqgre 219.5389 146 eqgre 162.5926 147 eqgre 146.3726 148 eqgre 127.6413 149 eqgre 274.2888 $gicam SPECSHOR Asfc.median 263 gicam 174.7445 264 gicam 83.4821 265 gicam 157.6005 266 gicam 153.7519 267 gicam 344.9775 I would just like to remove the column SPECSHOR (or extract the other one) so that it looks like $cotau Asfc.median 38 381.0247 39 154.6280 40 303.3219 41 351.2933 42 156.5327 etc. How should I do it? I know how to select each element like SPECSHOR_tx_Asfc[[1]], but I don't know how to select a single column within an element. Could you please help me on that? Thanks Ivan __ 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.
Re: [R] about loops
Hi, One approach could be a while loop as follows. Note that your circle should have radius 0.5 if I understood the problem correctly. N - 5 npoints - 0 ntests - 0 points.in.circle - matrix(NA, ncol=2, nrow=N) while (npoints N) { test.point - runif(2, -0.5, 0.5) # generate new point in 2D if ( sqrt(test.point %*% test.point) = 0.5){ # test its distance to the origin npoints - npoints + 1 points.in.circle[npoints, ] - test.point } ntests - ntests + 1 } print(paste(npoints, out of, ntests, 'where in the circle') ) HTH, baptiste 2010/1/21 Wolfgang Amadeus fdfcz...@163.com: Hello ! I have a quick question about loops. For example, I have a 1 * 1 square and its inscribed circle tangent i, whose radius, of course, is also 1. The loop here is as the following: generate n points, say 5, in the square randomly repeatedly until we have five in total in the circle, then we stop, otherwise we continue. I do not know how ! Help me Please ~ Thank you very much for your time. Yours Wolfgang Amadeus [[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-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] How do I juxtapose two lattice graphs with common X axes such that the X axes line up?
Hi, try c.trellis() from the latticeExtra package. HTH, baptiste 2010/1/20 George Chen glc...@stanford.edu: Hello, I would like to juxtapose two lattice graphs with common X axes such that the X axes line up. I am using plot right now but the edges are not neat and it would be nice if I could just draw 1 X axis and not both of them. Here is my code: upper-bwplot(SignalUsed~as.factor(AllNormalHitsNamesCount),data=NmlOverviewArray2, xlab=, ylab=Intensity of Individual Antibody Responses, main=Intensity, Frequency, Distribution, Quantity of Normal Antibody Responses, box.ratio=1, panel = function (AllNormalHitsNamesCount,...) { panel.bwplot(...) } ) lower-barchart(as.vector(table(NmlOverviewArray2$AllNormalHitsNamesCount)) ~as.factor(as.numeric(names(table(NmlOverviewArray2$AllNormalHitsNamesCount, data=NmlOverviewArray2, ylab=Number of Individual Antibody Responses, xlab=Occurrence of Individual Antibody Responses (Out of 45 Normals), box.ratio=1) plot (upper, newpage=TRUE, more=TRUE, position = c(0,.15,1,1)) plot (lower, newpage=FALSE, more=TRUE, position = c(0,0,1,.3)) George __ 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.
Re: [R] exporting text output to pdf
Hi, You could play with the splitTextGrob() function from the RGraphics package, string - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque leo ipsum, ultricies scelerisque volutpat non, volutpat et nulla. Curabitur consequat ullamcorper tellus id imperdiet. Duis semper malesuada nulla, blandit lobortis diam fringilla at. Vestibulum nec tellus orci, eu sollicitudin quam. Phasellus sit amet enim diam. Phasellus mattis hendrerit varius. Curabitur ut tristique enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed convallis, tortor id vehicula facilisis, nunc justo facilisis tellus, sed eleifend nisi lacus id purus. Maecenas tempus sollicitudin libero, molestie laoreet metus dapibus eu. Mauris justo ante, mattis et pulvinar a, varius pretium eros. Curabitur fringilla dui ac dui rutrum pretium. Donec sed magna adipiscing nisi accumsan congue sed ac est. Vivamus lorem urna, tristique quis accumsan quis, ullamcorper aliquet velit. library(RGraphics) g - splitTextGrob(string) grid.draw(g) See also this ggplot2 thread for mixing this kind of basic text with tables and graphics using only Grid functions, http://groups.google.com/group/ggplot2/browse_frm/thread/808af3b15d54ef38/822d8c2296ef3447 I haven't seen mention here of asciidoc or brew, these two packages might give you other options. HTH, baptiste 2010/1/18 Dimitri Shvorob dimitri.shvo...@gmail.com: ... You can modify this (dysfunctional) snippet. pdf() plot.new() mtext(Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque leo ipsum, ultricies scelerisque volutpat non, volutpat et nulla. Curabitur consequat ullamcorper tellus id imperdiet. Duis semper malesuada nulla, blandit lobortis diam fringilla at. Vestibulum nec tellus orci, eu sollicitudin quam. Phasellus sit amet enim diam. Phasellus mattis hendrerit varius. Curabitur ut tristique enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed convallis, tortor id vehicula facilisis, nunc justo facilisis tellus, sed eleifend nisi lacus id purus. Maecenas tempus sollicitudin libero, molestie laoreet metus dapibus eu. Mauris justo ante, mattis et pulvinar a, varius pretium eros. Curabitur fringilla dui ac dui rutrum pretium. Donec sed magna adipiscing nisi accumsan congue sed ac est. Vivamus lorem urna, tristique quis accumsan quis, ullamcorper aliquet velit.) mtext(A nice-looking paragraph! Now this is what I call good advice!) dev.off() -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1017087.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] call R with un expression (String)?
Hi, ?eval seems like a good candidate HTH, baptiste 2010/1/15 Jiiindo jiin...@yahoo.com: Hello all, I want to call R from java. And I have a expression in Java as a String, example : (variable 1 + variable 2)* variable 3 and i want R calculate this expression. How can I do? ex: Java -int x1,x2; -float x3; -String s=( x1.toString()+x2.toString() ) * x3.toString(); R: calculate expression s and return in to Java? Thanks Jin -- View this message in context: http://n4.nabble.com/call-R-with-un-expression-String-tp1014832p1014832.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] apply command
Hi, I think you could use iapply (search the archives) or the plyr package to save you from transposing the result. HTH, baptiste 2010/1/19 marco salvini marco.salv...@gmail.com: Can you please help on the issue? I using the apply command on a matrix below the example: Create a vector x =c(5, 3, 2:4, NA, 7, 3, 9, 2, 1, 5) create a matrix of 2 rows by 6 columns b=matrix(x, 2,6) print(b) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 5 2 4 7 9 1 [2,] 3 3 NA 3 2 5 using the command apply print(apply(b, 1, function(y) sort(y, na.last=F))) the output is a matrix of 6 rows by 2 columns. [,1] [,2] [1,] 1 NA [2,] 2 2 [3,] 4 3 [4,] 5 3 [5,] 7 3 [6,] 9 5 As you can see in the example I start with a matrix of (2 by 6) and the output of apply is a mtraxi of (6 by 2). This is very strange because I was expecting as output a matrix of the same dim (2 by 6) of the input matrix. I can solve this issues using an if statment on the dim of the matrix but if I am using a square matrix I am not able to control if the result of the apply is correct. Do anyone find a solution to this issue? thanks Marco [[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-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] expand.grid game
It did take me a good night's sleep to understand it. I was stuck with the exact same question but I see now how the remaining balls are shared among all 8 urns (therefore cases with 11, 12, 13, ... 17 balls are also dealt with). Thanks again, baptiste 2010/1/12 Rolf Turner r.tur...@auckland.ac.nz: On 13/01/2010, at 9:19 AM, Greg Snow wrote: How trivial is probably subjective, I don't think it is much above trivial. I would not have been surprised to see this question on an exam in my undergraduate (300 or junior level) probability course (the hard part was remembering the details from that class from over 20 years ago). My favorite test question of all time came from that course: You have a deck of poker cards with the 3's removed (and jokers), you deal yourself 5 cards at random, what is the probability of getting a straight (not including straight flushes)? This problem is simpler. Just think of the 8 places in the number as urns, and the 17 1's as balls to be put into the urns. One ball has to go in the first urn, so you have 16 left, there are choose(16+8-1,8-1) ways to distribute 16 undistinguishable balls among 8 distinguishable urns. But that includes some solutions with more than 9 balls in an urn which violates the digits restriction, so subtract off the illegal counts. If we place 10 balls in the first urn, then we have 7 remaining balls to distribute between the 8 urns or choose( 7+8-1, 7), If we place 1 ball in the first urn and 10 balls in one of the 7 other urns (7*), then there are choose( 6+8-1, 7 ) ways to distribute the remaining 6 balls in the 8 urns. Not too complicated once you remember (or look up) the formula for urns and balls. Sorry to be a thicko --- but doesn't the foregoing solution *leave in* the possibility of putting all 17 balls in the first urn? Or 3 balls in the first urn, 12 in the second, and the remaining 2 in any of the other six urns? Etc. I.e. don't more terms have to be subtracted? cheers, Rolf Turner ## Attention:This e-mail message is privileged and confidential. If you are not theintended recipient please delete the message and notify the sender.Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshalwww.marshalsoftware.com ## __ 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] expand.grid game
Nice --- am I missing something or was this closed form solution not entirely trivial to find? I ought to compile the various clever solutions given in this thread someday, it's fascinating! Thanks, baptiste 2010/1/12 Greg Snow greg.s...@imail.org: This also has a closed form solution: choose(16+8-1,7) - choose(7+8-1, 7) - 7*choose(6+8-1,7) [1] 229713 -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Brian Diggs Sent: Thursday, December 31, 2009 3:08 PM To: baptiste auguie; David Winsemius Cc: r-help Subject: Re: [R] expand.grid game baptiste auguie wrote: 2009/12/19 David Winsemius dwinsem...@comcast.net: On Dec 19, 2009, at 9:06 AM, baptiste auguie wrote: Dear list, In a little numbers game, I've hit a performance snag and I'm not sure how to code this in C. The game is the following: how many 8-digit numbers have the sum of their digits equal to 17? And are you considering the number 0089 to be in the acceptable set? Or is the range of possible numbers in 1079:9800 ? The latter, the first digit should not be 0. But if you have an interesting solution for the other case, let me know anyway. I should also stress that this is only for entertainment and curiosity's sake. baptiste I realize I'm late coming to this, but I was reading it in my post- vacation catch-up and it sounded interesting so I thought I'd give it a shot. After coding a couple of solutions that were exponential in time (for the number of digits), I rearranged things and came up with something that is linear in time (for the number of digits) and gives the count of numbers for all sums at once: library(plyr) nsum3 - function(digits) { digits - as.integer(digits)[[1L]] if (digits==1) { rep(1,9) } else { dm1 - nsum3(digits-1) Reduce(+, llply(0:9, function(x) {c(rep(0,x),dm1,rep(0,9-x))})) } } nsums - llply(1:8, nsum3) nsums[[5]][17] # [1] 3675 nsums[[8]][17] # [1] 229713 The whole thing runs in well under a second on my machine (a several years old dual core Windows machine). In the results of nsum3, the i- th element is the number of numbers whose digits sum to i. The basic idea is recursion on the number of digits; if n_{t,d} is the number of d-digit numbers that sum to t, then n_{t,d} = \sum_{i\in(0,9)} n_{t- i,d-1}. (Adding the digit i to each of those numbers makes their sum t and increases the digits to d). When digits==1, then 0 isn't a valid choice and that also implies the sum of digits can't be 0, which fits well with the 1 indexing of arrays. -- Brian Diggs, Ph.D. Senior Research Associate, Department of Surgery, Oregon Health Science University __ 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.
Re: [R] postscript, greek lellters
Hi, Something like this maybe, plot.new() lab = expression(bar(T)*(*-x* ; *alpha*)-G*(*x* ; *alpha* , *J*)) text(0.5,0.5,lab) ?plotmath HTH, baptiste 2010/1/8 bernardo lagos alvarez blacert...@gmail.com: Dear useRs, How can I, writting the correct greek letter using postscrip or pdf function. In my figures appears only the first letter (a of alpha, n of nu) when include the graphs in my .tex doxument. I am using title( expression(bar(T)(paste(-x,;,alpha))-G(paste(x,;,alpha,,,J title( expression(bar(T)(paste(-x,;~alpha))-G(paste(x,;~alpha,,,J title( expression(bar(T)(paste(-x,;*alpha))-G(paste(x,;*alpha,,,J, not work. sessionInfo() R version 2.9.0 (2009-04-17) i386-pc-mingw32 locale: LC_COLLATE=Spanish_Spain.1252;LC_CTYPE=Spanish_Spain.1252;LC_MONETARY=Spanish_Spain.1252;LC_NUMERIC=C;LC_TIME=Spanish_Spain.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base Thanks for your help, Bernardo. __ 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.
Re: [R] [R-pkgs] fortunes: 250th fortune
Hi, Thank you for this fun package. I recently posted a related question on R-help that seemed to pass unnoticed. Basically, I suggested that the functionality of fortunes could be extended to R FAQ entries, also allowing contributed packages to provide their own (fortune or) faq data file. My initial suggestion is here: http://markmail.org/message/oisbnuugifx5e36k I played with the fortune() source code and it doesn't take much effort to amend it for FAQ-like entries (I did not go as far as converting all the official FAQ entries, that's another problem). However such an approach is suboptimal in that most of the code would be duplicated. Would it make sense to write a slightly more general code working for a wider range of database entries? Best regards, baptiste 2010/1/6 Achim Zeileis achim.zeil...@wu-wien.ac.at: Dear useRs, it's a new year and time for a new CRAN-version of the fortunes package. Version 1.3-7 is now online at http://CRAN.R-project.org/package=fortunes which contains the 250th fortune: R fortune(250) As Obi-Wan Kenobi may have said in Star Wars: Use the source, Luke! -- Barry Rowlingson (answering a question on the documentation of some implementation details) R-devel (January 2010) Actually there is also the 251st... Thanks to all that have contributed to the fortunes package in the form of creating these nice quote or submitting them to the package. Best wishes, Z ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ 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.
Re: [R] xyplot: problems with column names legend
Hi, Using backticks might work to some extent, library(lattice) `my variable` = 1:10 y=rnorm(10) xyplot(`my variable` ~ y) but if your data is in a data.frame the names should have been converted, make.names('my variable') [1] my.variable HTH, baptiste 2010/1/3 Jay josip.2...@gmail.com: Hello! one more question about xyplot. If I have data which have space in the column names, say xyz 123. How do I create a working graph where this text is displayed in the legend key? Now when I try something like xyplot(xyz 123 ~ variable1, data = mydata, ...) I get nothing. Also, is it possible to genrate the graph with xyplot(mydata[,1] ~ variable1, data = mydata, ...) and then later in the code specify the names that should be displayed in the legend? Thank you! __ 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.
Re: [R] Axis Labels for Compound Plots in ggplot2
Hi, You can set up a Grid layout with one viewport at the bottom and another on the left and use grid.text to add your labels. An example is given below using grid.pack. The gridExtra package provides a convenient wrapper for these regular arrangements of plots, ##library(gridExtra) #http://gridextra.googlecode.com/ source(http://gridextra.googlecode.com/svn/trunk/R/arrange.r;) arrange(a, b, c, d) I will add a viewport on the left side for a global y axis, but in the meantime you can use packGrob, ## return a grob with all plots p - arrange(a, b, c, d, plot=FALSE) g - frameGrob() g - packGrob(g, p) g - packGrob(g, textGrob(my y label, rot=90), side = left, border = unit(rep(4, 4), mm)) g - packGrob(g, textGrob(my x label), side = bottom, border = unit(rep(4, 4), mm)) grid.draw(g) HTH, baptiste 2009/12/30 Lorenzo Isella lorenzo.ise...@gmail.com: Dear All, I am trying to stitch together multiple plots using ggplot2 Consider for instance the following snippet based on an old thread (http://tinyurl.com/ylehm2t) library(ggplot2) vplayout - function(x, y) viewport(layout.pos.row=x, layout.pos.col=y) draw4 - function(pdfname, a,b,c,d,w,h) { pdf(pdfname,width=w, height=h) grid.newpage() pushViewport(viewport(layout=grid.layout(2,2) ) ) print(a, vp=vplayout(1,1)) print(b, vp=vplayout(1,2)) print(c, vp=vplayout(2,1)) print(d, vp=vplayout(2,2)) dev.off() } data(diamonds) set.seed(1234) randind - sample(nrow(diamonds),1000,replace=FALSE) dsmall - diamonds[randind,] a - qplot(carat, data=dsmall, geom=histogram,binwidth=1) b - qplot(carat, data=dsmall, geom=histogram,binwidth=.1) c - qplot(carat, data=dsmall, geom=histogram,binwidth=.01) d - qplot(carat, data=dsmall, geom=histogram,binwidth=2) width - 7 height - 7 draw4( test-4.pdf, a,b,c,d, width, height) Is there any way to give an overall label along the y and x axis? E.g.something like a xlab to write some text which applies to the x axis of all the plots and which should go at the middle bottom of the compound plot (and something perfectly along these lines for the y axis). Many thanks Lorenzo __ 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.
Re: [R] ggplot2, building a simple formula interface
Hi, Try print(p) instead of plot(p) HTH, baptiste 2009/12/29 Bryan Hanson han...@depauw.edu: I¹m trying to build a simple formula interface to work with a function using ggplot2. The following scheme ³works² up until the plot(p) request, at which point there are complaints about xlim¹s and a blank graphics window. Looking at str(p) I do see the limits are NULL, plus layer 1 claims to have an empty data frame (but df is reproduced correctly). I'm sure I'm missing something really obvious; alas my ggplot2 book is at work and it's cold outside! simple - function(formula, data, ...){ mf - model.frame(formula = formula, data = data) } res - rnorm(100) f1 - rep(c(C, D, D, C), 25) f2 - rep(c(A, B), 50) mydata - data.frame(res, f1, f2) df - simple(~ res + f1, mydata) p - ggplot(df, aes(f1, res)) + geom_boxplot() plot(p) Thanks, Bryan * Bryan Hanson Acting Chair Professor of Chemistry Biochemistry DePauw University, Greencastle IN USA sessionInfo() R version 2.10.1 (2009-12-14) x86_64-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] datasets tools grid graphics grDevices utils stats methods base other attached packages: [1] ggplot2_0.8.5 digest_0.4.2 reshape_0.8.3 proto_0.3-8 mvbutils_2.5.0 ChemoSpec_1.43 [7] R.utils_1.2.4 R.oo_1.6.5 R.methodsS3_1.0.3 rgl_0.87 lattice_0.17-26 mvoutlier_1.4 [13] plyr_0.1.9 RColorBrewer_1.0-2 chemometrics_0.5 som_0.3-4 robustbase_0.5-0-1 rpart_3.1-45 [19] pls_2.1-0 pcaPP_1.7 mvtnorm_0.9-8 nnet_7.3-1 mclust_3.4 MASS_7.3-4 [25] lars_0.9-7 e1071_1.5-22 class_7.3-1 __ 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.
Re: [R] subsetting by groups, with conditions
Hi, I think you can also use plyr for this, dft - read.table(textConnection(P1idVeg1Veg2AreaPoly2 P2ID 1 p p 1 1 1 p p 1.5 2 2 p p 2 3 2 p h 3.5 4), header=T) library(plyr) ddply(dft, .(P1id), function(.df) { .ddf - subset(.df, as.character(Veg1)==as.character(Veg2)) .ddf[which.max(.ddf$AreaPoly2), ] }) HTH, baptiste 2009/12/29 Seth W Bigelow sbige...@fs.fed.us: I have a data set similar to this: P1id Veg1 Veg2 AreaPoly2 P2ID 1 p p 1 1 1 p p 1.5 2 2 p p 2 3 2 p h 3.5 4 For each group of Poly1id records, I wish to output (subset) the record which has largest AreaPoly2 value, but only if Veg1=Veg2. For this example, the desired dataset would be P1id Veg1 Veg2 AreaPoly2 P2ID 1 p p 1.5 2 2 p p 2 3 Can anyone point me in the right direction on this? Dr. Seth W. Bigelow Biologist, USDA-FS Pacific Southwest Research Station 1731 Research Park Drive, Davis California [[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-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] Histogram/BoxPlot Panel
Hi, Here is some artificial data followed by minimal ggplot2 and lattice examples, makeUpData - function(){ data.frame(x=sample(letters[1:4], 100, repl=TRUE), y=rnorm(100)) } datasets - replicate(15, makeUpData(), simplify=FALSE) names(datasets) - paste(dataset, seq_along(datasets), sep=) str(datasets) require(reshape) ## combine the datasets in one long format data.frame m - melt(datasets, meas=c(y)) str(m) require(ggplot2) ggplot(m)+ geom_boxplot(mapping=aes(x, value))+ facet_wrap(~L1) # or more concisely qplot(x, value, data=m, geom=boxplot, facets=~L1) require(lattice) bwplot(value~x | L1, data=m) HTH, baptiste 2009/12/29 Lorenzo Isella lorenzo.ise...@gmail.com: Dear All, I am given 15 different data sets and I would like to generate a panel showing all of them. Each dataset will be presented either as a boxplot or as a histogram. There are several possible ways to achieve this (as far as I know) (1) using plot and mfrow() (2) using lattice (3) using ggplot/ggplot2 I am not very experienced (to be euphemistic) about (2) and (3). My question then is: how would you try to organize these 15 histograms/boxplots into a single figure? Can anyone provide me with a simple example (with artificial data) for (2) and (3) (or point me to some targeted online resource)? Any suggestion is welcome. Many thanks Lorenzo __ 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.
Re: [R] Histogram/BoxPlot Panel
I forgot the base graphics way, ## divide the window in 4x4 cells par(mfrow=n2mfrow(length(datasets))) ## loop over the list of datasets and plot each one be.quiet - lapply(datasets, function(ii) boxplot(y~x, data=ii)) ggplot2 has a website with many examples, http://had.co.nz/ggplot2/ as well as a book. Lattice also has a dedicated book, and a companion website with the figures, http://r-forge.r-project.org/projects/lmdvr/ HTH, baptiste 2009/12/29 baptiste auguie baptiste.aug...@googlemail.com: Hi, Here is some artificial data followed by minimal ggplot2 and lattice examples, makeUpData - function(){ data.frame(x=sample(letters[1:4], 100, repl=TRUE), y=rnorm(100)) } datasets - replicate(15, makeUpData(), simplify=FALSE) names(datasets) - paste(dataset, seq_along(datasets), sep=) str(datasets) require(reshape) ## combine the datasets in one long format data.frame m - melt(datasets, meas=c(y)) str(m) require(ggplot2) ggplot(m)+ geom_boxplot(mapping=aes(x, value))+ facet_wrap(~L1) # or more concisely qplot(x, value, data=m, geom=boxplot, facets=~L1) require(lattice) bwplot(value~x | L1, data=m) HTH, baptiste 2009/12/29 Lorenzo Isella lorenzo.ise...@gmail.com: Dear All, I am given 15 different data sets and I would like to generate a panel showing all of them. Each dataset will be presented either as a boxplot or as a histogram. There are several possible ways to achieve this (as far as I know) (1) using plot and mfrow() (2) using lattice (3) using ggplot/ggplot2 I am not very experienced (to be euphemistic) about (2) and (3). My question then is: how would you try to organize these 15 histograms/boxplots into a single figure? Can anyone provide me with a simple example (with artificial data) for (2) and (3) (or point me to some targeted online resource)? Any suggestion is welcome. Many thanks Lorenzo __ 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.
Re: [R] Scaling error
Hi, Try this, x - matrix(1:9, ncol=3, byrow=T) sca - c(2.5, 1.7, 3.6) x %*% diag(1/sca) HTH, baptiste 2009/12/27 Muhammad Rahiz muhammad.ra...@ouce.ox.ac.uk: Hi useRs, I ran into an inconsistent output problem again. Here is the simplify illustration I've got a matrix as follows x V1 V2 V3 [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Associated with the matrix is a scaling factor, sca, derived from, say the mean of the respective columns, V1-V3; sca V1 V2 V3 2.5 1.7 3.6 The idea is that the scaling factor gets applied to each element in the column matrix. So out - x / sca would give me V1 V2 V3 1 *2.5 2 *1.7 3 *3.6 4 *2.5 5 *1.7 6 *3.6 7 *2.5 8 *1.7 9 *3.6 But what actually happen is this, V1 V2 V3 1 *2.5 2 *2.5 3 *2.5 4 *1.7 5 *1.7 6 *1.7 7 *3.6 8 *3.6 9 *3.6 I can do the following; x[,1] / sca[1] x[,2] / sca[2] which is OK for a set of test data but not for my actual dataset. At the moment, I'm thinking of something in the lines of a for loop function i.e. for (i in ...){ statement... } Is there a syntax in the for loop that allows me to select the column/row in the file? Thanks. -- Muhammad Rahiz | Doctoral Student in Regional Climate Modeling Climate Research Laboratory, School of Geography the Environment Oxford University Centre for the Environment South Parks Road, Oxford, OX1 3QY, United Kingdom Tel: +44 (0)1865-285194 Mobile: +44 (0)7854-625974 Email: muhammad.ra...@ouce.ox.ac.uk __ 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.
Re: [R] String question
Will this do? temp - paste(m, 1:3, sep=,collapse=,) HTH, baptiste 2009/12/23 Knut Krueger r...@krueger-family.de: Hi to all I need a string like temp - paste(m1,m2,m3,sep=,) But i must know how many items are in the string,afterwards the other option would be to use a vector temp - c(m1,m2,m3) No problem to get the count of items but I must get afterwards the string m1,m2,m3 No problem to build the string with a loop, but it should be more easy but it seems that I am looking to the wrong functions. Kind regards Knut __ 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.
Re: [R] String question
Isn't paste doing exactly this? temp - c(November, December,Monday,Tuesday) paste(temp, collapse=,) # November,December,Monday,Tuesday HTH, baptiste 2009/12/23 Ted Harding ted.hard...@manchester.ac.uk: On 23-Dec-09 11:08:02, Knut Krueger wrote: Jim Lemon schrieb: Not as easy as I thought it would be, but: mlist-as.list(paste(m,1:sample(5:10,1),sep=)) do.call(paste,c(mlist,sep=,)) Hi Jim, yes it works :-) temp - c(November, December,Monday,Tuesday) length(temp) #getting the length of the vector string1=do.call(paste,c(as.list(temp),sep=,)) #converting to a string but I have no idea where I could find that documentation ;-) Thanks a lot Knut Interestingly, cat() does the pasting job in the simplest possible way: temp - c(November, December,Monday,Tuesday) cat(temp,sep=,) November,December,Monday,Tuesday [copied from the R console; note the trialing which is the command prompt for the next input -- not part of the output of cat() ] with output to screen (or to nominated file). But there seems to be no way to persuade cat to *return* this result as a value, which could be assigned to a variable. If there were such a way, that would be a very smooth solution! Ted. E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 23-Dec-09 Time: 11:28:47 -- XFMail -- __ 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.
Re: [R] expand.grid game
Wow! system.time({ all = blockparts(rep(9,8),17) print( dim(all[,all[1,]!=0])[2] ) # remove leading 0s }) ## 229713 user system elapsed 0.160 0.068 0.228 In some ways I think this is close to Hadley's suggestion, though I didn't know how to implement it. Thanks a lot to everybody who participated, I have learned interesting things from a seemingly innocuous question. Best regards, baptiste 2009/12/21 Robin Hankin rk...@cam.ac.uk: Hi library(partitions) jj - blockparts(rep(9,8),17) dim(jj) gives 318648 HTH rksh baptiste auguie wrote: Dear list, In a little numbers game, I've hit a performance snag and I'm not sure how to code this in C. The game is the following: how many 8-digit numbers have the sum of their digits equal to 17? The brute-force answer could be: maxi - 9 # digits from 0 to 9 N - 5 # 8 is too large test - 17 # for example's sake sum(rowSums(do.call(expand.grid, c(list(1:maxi), rep(list(0:maxi), N-1 == test) ## 3675 Now, if I make N = 8, R stalls for some time and finally gives up with: Error: cannot allocate vector of size 343.3 Mb I thought I could get around this using Reduce() to recursively apply rowSum to intermediate results, but it doesn't seem to help, a=list(1:maxi) b=rep(list(0:maxi), N-1) foo - function(a, b, fun=sum, ...){ switch(fun, 'sum' = rowSums(do.call(expand.grid, c(a, b))), 'mean' = rowMeans(do.call(expand.grid, c(a, b))), apply(do.call(expand.grid, c(a, b)), 1, fun, ...)) # generic case } sum(Reduce(foo, list(b), init=a) == test) ## 3675 # OK Same problem with N=8. Now, if N was fixed I could write a little C code to do this calculation term-by-term, something along those lines, test = 0; for (i1=1, i1=9, i1++) { for (i2=0, i2=9, i2++) { [... other nested loops ] test = test + (i1 + i2 + [...] == 17); } [...] } but here the number of for loops, N, should remain a variable. In despair I coded this in R as a wicked eval(parse()) construct, and it does produce the expected result after an awfully long time. makeNestedLoops - function(N=3){ startLoop - function(ii, start=1, end=9){ paste(for (i, ii, in seq(,start,, ,end,)) {\n, sep=) } first - startLoop(1) inner.start - lapply(seq(2, N), startLoop, start=0) calculation - paste(test - test + (, paste(i, seq(1, N), sep=, collapse=+), ==17 )\n) end - replicate(N, }\n) code.to.run - do.call(paste, c(list(first), inner.start, calculation, end)) cat(code.to.run) invisible(code.to.run) } test - 0 eval(parse(text = makeNestedLoops(8)) ) ## 229713 I hope I have missed a better way to do this in R. Otherwise, I believe what I'm after is some kind of C or C++ macro expansion, because the number of loops should not be hard coded. Thanks for any tips you may have! Best regards, baptiste __ 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. -- Robin K. S. Hankin Uncertainty Analyst University of Cambridge 19 Silver Street Cambridge CB3 9EP 01223-764877 __ 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] expression()
Hi, try this, ylab = expression(Temperature~(degree*F)) ?plotmath baptiste 2009/12/20 Kim Jung Hwa kimhwamaill...@gmail.com: Hi All, I'm wondering if its possible to write degree in symbol. I would like y-label as Temperature (degreeF). where degree should be in symbols. Thanks in advance, #R Code library(lattice) data(barley) barchart(yield ~ variety | site, data = barley, groups = year, layout = c(1,6), ylab = Temperature (degreeF), scales = list(x = list(abbreviate = TRUE, minlength = 5))) ~Kim Jung Hwa [[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-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] basic proto question
Dear list, I made the following example of a proto object that contains some data and a spline interpolation. I don't understand why test$predict() fails with this error message: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Best regards, baptiste test - proto(source = data.frame(x=1:10, y=rnorm(10)), raw = function(.){ data.frame(xx=.$source$x, yy=.$source$y) }, spline = function(.){ with(.$raw(), smooth.spline(xx, yy)) }, predict = function(., range=NULL, n=100){ if(is.null(range)) range - range(.$raw()$xx) x.fine - seq(from=range[1], to=range[2], length=n) predict(.$spline(), x.fine) } ) test$source test$raw() test$spline() # OK so far test$predict() # fails sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] grid tools stats graphics grDevices utils datasets [8] methods base other attached packages: [1] lattice_0.17-26 ggplot2_0.8.5 digest_0.4.1reshape_0.8.3 [5] plyr_0.1.9 proto_0.3-8 constants_1.0 gtools_2.6.1 __ 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] basic proto question
Thanks, it seems so obvious now! baptiste 2009/12/20 Gabor Grothendieck ggrothendi...@gmail.com: The free variables in a proto method are looked up in the object that the method was defined in so by referencing predict within test$predict you are referring back to test$predict whereas you mean to refer to stats::predict. Change the line that calls predict to: stats::predict(.$spline(), x.fine) On Sun, Dec 20, 2009 at 11:49 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: Dear list, I made the following example of a proto object that contains some data and a spline interpolation. I don't understand why test$predict() fails with this error message: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Best regards, baptiste test - proto(source = data.frame(x=1:10, y=rnorm(10)), raw = function(.){ data.frame(xx=.$source$x, yy=.$source$y) }, spline = function(.){ with(.$raw(), smooth.spline(xx, yy)) }, predict = function(., range=NULL, n=100){ if(is.null(range)) range - range(.$raw()$xx) x.fine - seq(from=range[1], to=range[2], length=n) predict(.$spline(), x.fine) } ) test$source test$raw() test$spline() # OK so far test$predict() # fails sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] grid tools stats graphics grDevices utils datasets [8] methods base other attached packages: [1] lattice_0.17-26 ggplot2_0.8.5 digest_0.4.1 reshape_0.8.3 [5] plyr_0.1.9 proto_0.3-8 constants_1.0 gtools_2.6.1 __ 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.
Re: [R] How to rotate an axis?
Hi, Do you want a ternary plot? http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=34 It's easy to rotate an axis with Grid graphics, library(grid) pushViewport(viewport(0.5,0.5, width=0.5, height=unit(3, lines))) grid.xaxis(at=seq(-0.5,0.5,by=0.1), vp=viewport(x=1, angle=-60)) HTH, baptiste 2009/12/20 Etienne Stockhausen einohr2...@web.de: Hi everybody, I'm trying to build a trilineare coordinate system with R. For that, I need to rotate an axis. Does anybody know, if it is possible to rotate an axis, created with the command axis(), about for instance 60 degrees? I'm looking foward to any ideas and hints and want to wish everybody a merry merry christmas. Thanks in advance. Etienne Stockhausen __ 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] expand.grid game
Dear list, In a little numbers game, I've hit a performance snag and I'm not sure how to code this in C. The game is the following: how many 8-digit numbers have the sum of their digits equal to 17? The brute-force answer could be: maxi - 9 # digits from 0 to 9 N - 5 # 8 is too large test - 17 # for example's sake sum(rowSums(do.call(expand.grid, c(list(1:maxi), rep(list(0:maxi), N-1 == test) ## 3675 Now, if I make N = 8, R stalls for some time and finally gives up with: Error: cannot allocate vector of size 343.3 Mb I thought I could get around this using Reduce() to recursively apply rowSum to intermediate results, but it doesn't seem to help, a=list(1:maxi) b=rep(list(0:maxi), N-1) foo - function(a, b, fun=sum, ...){ switch(fun, 'sum' = rowSums(do.call(expand.grid, c(a, b))), 'mean' = rowMeans(do.call(expand.grid, c(a, b))), apply(do.call(expand.grid, c(a, b)), 1, fun, ...)) # generic case } sum(Reduce(foo, list(b), init=a) == test) ## 3675 # OK Same problem with N=8. Now, if N was fixed I could write a little C code to do this calculation term-by-term, something along those lines, test = 0; for (i1=1, i1=9, i1++) { for (i2=0, i2=9, i2++) { [... other nested loops ] test = test + (i1 + i2 + [...] == 17); } [...] } but here the number of for loops, N, should remain a variable. In despair I coded this in R as a wicked eval(parse()) construct, and it does produce the expected result after an awfully long time. makeNestedLoops - function(N=3){ startLoop - function(ii, start=1, end=9){ paste(for (i, ii, in seq(,start,, ,end,)) {\n, sep=) } first - startLoop(1) inner.start - lapply(seq(2, N), startLoop, start=0) calculation - paste(test - test + (, paste(i, seq(1, N), sep=, collapse=+), ==17 )\n) end - replicate(N, }\n) code.to.run - do.call(paste, c(list(first), inner.start, calculation, end)) cat(code.to.run) invisible(code.to.run) } test - 0 eval(parse(text = makeNestedLoops(8)) ) ## 229713 I hope I have missed a better way to do this in R. Otherwise, I believe what I'm after is some kind of C or C++ macro expansion, because the number of loops should not be hard coded. Thanks for any tips you may have! Best regards, baptiste __ 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] expand.grid game
2009/12/19 David Winsemius dwinsem...@comcast.net: On Dec 19, 2009, at 9:06 AM, baptiste auguie wrote: Dear list, In a little numbers game, I've hit a performance snag and I'm not sure how to code this in C. The game is the following: how many 8-digit numbers have the sum of their digits equal to 17? And are you considering the number 0089 to be in the acceptable set? Or is the range of possible numbers in 1079:9800 ? The latter, the first digit should not be 0. But if you have an interesting solution for the other case, let me know anyway. I should also stress that this is only for entertainment and curiosity's sake. baptiste __ 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] expand.grid game
Hi, Thanks for the link, I guess it's some kind of a classic game. I'm a bit surprised by your timing, my ugly eval(parse()) solution definitely took less than one hour with a machine not so different from yours, system.time( for (i in 1079:1179) if (sumdigits(i)==17) {idx-c(idx,i)}) user system elapsed 34.050 1.109 35.791 I'm surprised by idx-c(idx,i), isn't that considered a sin in the R Inferno? Presumably growing idx will waste time for large N. Thanks, baptiste 2009/12/19 David Winsemius dwinsem...@comcast.net: On Dec 19, 2009, at 1:36 PM, baptiste auguie wrote: 2009/12/19 David Winsemius dwinsem...@comcast.net: On Dec 19, 2009, at 9:06 AM, baptiste auguie wrote: Dear list, In a little numbers game, I've hit a performance snag and I'm not sure how to code this in C. The game is the following: how many 8-digit numbers have the sum of their digits equal to 17? And are you considering the number 0089 to be in the acceptable set? Or is the range of possible numbers in 1079:9800 ? The latter, the first digit should not be 0. But if you have an interesting solution for the other case, let me know anyway. I should also stress that this is only for entertainment and curiosity's sake. The sequence of numbers whose digit sum is 13 is one of the ATT integer sequences: http://www.research.att.com/~njas/sequences/A143164 No R implementations there, only Maple and Mathematica. Rather than doing strsplit()'s, I thought that a mathematical approach would be faster for a sumdigits function: sumdigits - function(x) { s=0; for (i in 1:(1+floor(log(x, base=10))) ){ s-s+x%%10; x-x%/%10} return(s) } # what follows would be expected to take roughly 1/100th and 1/50th of the time of a complete enumeration but is useful for estimating the size of the result and the time of an exhaustive search: system.time( for (i in 1079:1179) if (sumdigits(i)==17) {idx-c(idx,i)}) user system elapsed 30.997 3.516 34.403 system.time( for (i in 1079:1279) if (sumdigits(i)==17) {idx-c(idx,i)}) user system elapsed 55.975 2.433 58.218 head(idx) [1] 1079 1088 1097 1169 1178 1187 length(idx) [1] 31572 So it looks as though an exhaustive strategy would take a bit under an hour on my machine (a 2 year-old device) and be a vector around 1578600 elements in length. (Takes very little memory, and would undoubtedly be faster if I could use more than one core.) baptiste David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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] expand.grid game
This problem does yield some interesting and unexpected distributions. Here is another, the number of positive cases as a function of number of digits (8 in the original question) and of test value (17). maxi - 9 N - 5 test - 17 foo - function(N=2, test=1){ sum(rowSums(do.call(expand.grid, c(list(1:maxi), rep(list(0:maxi), N-1 == test) } library(plyr) N - seq(1, 6) maxi - 9*N params - data.frame(from=N, to=maxi+1) params2 - mlply(params, seq) NN - lapply(seq_along(params2), function(x) rep(x, length(params2[[x]]))) params3 - data.frame(N=do.call(c, NN), test=do.call(c, params2)) results - mdply(params3, foo) library(ggplot2) p - qplot(test, V1, data=results, geom=path)+ facet_wrap(~N, scales=free) + scale_x_continuous(expand=c(0, 0))+ xlab(test) + ylab(number of match) p Best, baptiste [David: sorry for the duplicate, i initially sent an attachment that was too large for the list] 2009/12/19 David Winsemius dwinsem...@comcast.net: On Dec 19, 2009, at 2:10 PM, David Winsemius wrote: On Dec 19, 2009, at 1:36 PM, baptiste auguie wrote: 2009/12/19 David Winsemius dwinsem...@comcast.net: On Dec 19, 2009, at 9:06 AM, baptiste auguie wrote: Dear list, In a little numbers game, I've hit a performance snag and I'm not sure how to code this in C. The game is the following: how many 8-digit numbers have the sum of their digits equal to 17? And are you considering the number 0089 to be in the acceptable set? Or is the range of possible numbers in 1079:9800 ? The latter, the first digit should not be 0. But if you have an interesting solution for the other case, let me know anyway. I should also stress that this is only for entertainment and curiosity's sake. The sequence of numbers whose digit sum is 13 is one of the ATT integer sequences: http://www.research.att.com/~njas/sequences/A143164 No R implementations there, only Maple and Mathematica. Rather than doing strsplit()'s, I thought that a mathematical approach would be faster for a sumdigits function: sumdigits - function(x) { s=0; for (i in 1:(1+floor(log(x, base=10))) ){ s-s+x%%10; x-x%/%10} return(s) } # what follows would be expected to take roughly 1/100th and 1/50th of the time of a complete enumeration but is useful for estimating the size of the result and the time of an exhaustive search: system.time( for (i in 1079:1179) if (sumdigits(i)==17) {idx-c(idx,i)}) user system elapsed 30.997 3.516 34.403 system.time( for (i in 1079:1279) if (sumdigits(i)==17) {idx-c(idx,i)}) user system elapsed 55.975 2.433 58.218 head(idx) [1] 1079 1088 1097 1169 1178 1187 length(idx) [1] 31572 So it looks as though an exhaustive strategy would take a bit under an hour on my machine (a 2 year-old device) and be a vector around 1578600 elements in length. (Takes very little memory, and would undoubtedly be faster if I could use more than one core.) I was assuming that it would be a relatively constant density of numbers in that range which can be seen to be false by examining a density plot of roughly the first 5% of that range. system.time( for (i in 1079:1479) if (sumdigits(i)==17) {idx-c(idx,i)}) user system elapsed 113.074 5.764 118.391 plot(density(idx)) Plot attached: baptiste David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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] mutlidimensional in.convex.hull (wasmultidimensionalpoint.in.polygon??)
Hi, Excellent, thanks for doing this! I had tried the 2D case myself but I was put off by the fact that Octave's convhulln had a different ordering of the points to R's geometry package (an improvement to Octave's convhulln was made after it was ported to R). I'm not sure how you got around this but it's good to see that the result was worth the effort! Below are a few minor syntax suggestions, # p - dim(calpts)[2] # columns in calpts # and other similar lines could be replaced with ncol(calpts) nrow(testpts) nrow(hull) # length(degenflag[degenflag]) # can probably be written sum(degenflag) # center = apply(calpts, 2, mean) # more efficient colMeans(calpts) Would you consider submitting this function to the maintainer of the geometry package, after checking it's OK with inhull's original author? Best regards, baptiste 2009/12/18 Keith Jewell k.jew...@campden.co.uk: Hi All, I couldn't resist doing this the right way! A colleague explained the vector algebra to me (thanks Martin!) and I've followed the structure of the Matlab code referenced below, but all errors are mine! I don't pretend to great R expertise, so this code may not be optimal (in time or the memory issue addressed by the Matlab code), but it is a lot faster than the other algorithm discussed in this thread. These timings are on the real example data described in my previous message in this thread. system.time(inhull(xs,ps)) # the right way user system elapsed 1.34 0.07 1.41 system.time({phull - convhulln(ps) # the other algorithm + phull2 - convhulln(rbind(ps,xs)) + nrp - nrow(ps) + nrx - nrow(xs) + outside - unique(phull2[phull2nrp])-nrp + done - FALSE + while(!done){ + phull3 - convhulln(rbind(ps,xs[-(outside),])) + also.outside - (1:nrx)[-outside][unique(phull3[phull3nrp])-nrp] + outside - c(outside,also.outside) + done - length(also.outside)==0 + }}) user system elapsed 15.09 0.09 15.22 Although I really must move on now, if anyone has comments, criticisms, suggestions for improvement I would be interested. Enjoy! Keith Jewell inhull - function(testpts, calpts, hull=convhulln(calpts), tol=mean(mean(abs(calpts)))*sqrt(.Machine$double.eps)) { # # R implementation of the Matlab code by John D'Errico 04 Mar 2006 (Updated 30 Oct 2006) # downloaded from http://www.mathworks.com/matlabcentral/fileexchange/10226-inhull # with some modifications and simplifications # # Efficient test for points inside a convex hull in n dimensions # #% arguments: (input) #% testpts - nxp array to test, n data points, in p dimensions #% If you have many points to test, it is most efficient to #% call this function once with the entire set. #% #% calpts - mxp array of vertices of the convex hull, as used by #% convhulln. #% #% hull - (OPTIONAL) tessellation (or triangulation) generated by convhulln #% If hull is left empty or not supplied, then it will be #% generated. #% #% tol - (OPTIONAL) tolerance on the tests for inclusion in the #% convex hull. You can think of tol as the distance a point #% may possibly lie outside the hull, and still be perceived #% as on the surface of the hull. Because of numerical slop #% nothing can ever be done exactly here. I might guess a #% semi-intelligent value of tol to be #% #% tol = 1.e-13*mean(abs(calpts(:))) #% #% In higher dimensions, the numerical issues of floating #% point arithmetic will probably suggest a larger value #% of tol. #% # In this R implementation default tol=mean(mean(abs(calpts)))*sqrt(.Machine$double.eps) # DEFAULT: tol = 1e-6 # # VALUE: Matlab returns a vector of TRUE (inside/on) or FALSE (outside) # This R implementation returns an integer vector of length n # 1 = inside hull # -1 = inside hull # 0 = on hull (to precision indicated by tol) # require(geometry, quietly=TRUE) # for convhulln require(MASS, quietly=TRUE) # for Null # ensure arguments are matrices (not data frames) and get sizes calpts - as.matrix(calpts) testpts - as.matrix(testpts) p - dim(calpts)[2] # columns in calpts cx - dim(testpts)[1] # rows in testpts nt - dim(hull)[1] # number of simplexes in hull # find normal vectors to each simplex nrmls - matrix(NA, nt, p) # predefine each nrml as NA, degenerate degenflag - matrix(TRUE, nt, 1) for (i in 1:nt) { nullsp - t(Null(t(calpts[hull[i,-1],] - matrix(calpts[hull[i,1],],p-1,p, byrow=TRUE if (dim(nullsp)[1] == 1) { nrmls[i,] - nullsp degenflag[i] - FALSE}} # Warn of degenerate faces, and remove corresponding normals if(length(degenflag[degenflag]) 0) warning(length(degenflag[degenflag]), degenerate faces in convex hull) nrmls - nrmls[!degenflag,] nt - dim(nrmls)[1]
Re: [R] problem with a densityplot
FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f you need to print() HTH, baptiste 2009/12/16 c...@autistici.org c...@autistici.org: Hi, i have a script how i launch lattice to make a densityplot. in the script: jpeg(file=XXX.jpg) densityplot(~f_diametro+m_diametro+n_diametro, plot.points=rug, auto.key=T) dev.off() does'nt work and in R i dont have any output but if i launch by R row by row, runs correctly. any idea? __ 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] fortune-like FAQ search
Dear list, I'm not so familiar with the internals of the fortunes package, but I really like the interface. I was wondering if someone had implemented a similar functionality to parse the entries of the R FAQ http://cran.r-project.org/doc/FAQ/R-FAQ.html . Say, if I was to answer a question Why does my lattice graphic fail to produce an output in this function?, I might try: library(rfaq) faq(lattice) ## or if I were a number kind of person faq(722) The database could be split into windows only, etc. Should this approach prove useful, one could even imagine using this rfaq database as the original source to produce the current html page. The infrastructure might also be of interest for other packages which could provide their own database. Just a thought anyway, Best regards, baptiste __ 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] Combinations
Hi, Try this, apply(expand.grid(letters[1:3], letters[24:26]), 1, paste,collapse=) [1] ax bx cx ay by cy az bz cz ?expand.grid HTH, baptiste 2009/12/14 Amelia Livington amelia_living...@yahoo.com: Dear R helpers, I am working on the scenario analysis pertaining to various interest rates. In this connection I need to form the various combinations as under : Suppose I have two sets A = (a, b, c) and B = (x,y,z) Then I can easily form the cominations as (ax, ay, az, bx, by, bz, cx, cy, cz) However, if I have say 5 variables, then total no of possible combinations will be 3^5 = 243. Thus, A = (a,b,c), B = (x, y, z), C = (l, m, n), D = (p,q,r), E = (s, t, u). Then may be my possble combination will start as (a, x, l, p, s), then next combination may be (a, x, l, p, u) and so on. The last combination (243rd in this case) may be (c, z, n, r, u) or something like this. In R, is there any way to list all these 3^5 = 243 combinations? Amelia [[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-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] code for [[.data.frame
`[[.data.frame` and more generally see Rnews Volume 6/4, October 2006 Accessing the Sources. HTH, baptiste 2009/12/13 Guillaume Yziquel guillaume.yziq...@citycable.ch: Hello. I'm currently trying to wrap up data frames into OCaml via OCaml-R, and I'm having trouble with data frame subsetting: # x#column 1;; Erreur dans (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : l'élément 1 est vide ; la partie de la liste d'arguments de 'is.matrix' en cours d'évaluation était : (i) So I'd like to know what is the code of [[.data.frame. I know how to show the code of functions in R (just typing the name of the function), but I'm having trouble with [[.data.frame, as it has a special syntacting handling. Could someone kindly show me how to display the code of [[.data.frame in the R toploop? All the best, -- Guillaume Yziquel http://yziquel.homelinux.org/ __ 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.
Re: [R] mutlidimensional in.convex.hull (was multidimensional point.in.polygon??)
2009/12/10 Charles C. Berry cbe...@tajo.ucsd.edu: [snipped] Many? set.seed(1234) ps - matrix(rnorm(4000),ncol=4) phull - convhulln(ps) xs - matrix(rnorm(1200),ncol=4) phull2 - convhulln(rbind(ps,xs)) nrp - nrow(ps) nrx - nrow(xs) outside - unique(phull2[phull2nrp])-nrp done - FALSE while(!done){ + phull3 - convhulln(rbind(ps,xs[-(outside),])) + also.outside - (1:nrx)[-outside][unique(phull3[phull3nrp])-nrp] + print(length(also.outside)) + outside - c(outside,also.outside) + done - length(also.outside)==0 + } [1] 3 [1] 0 phull2 was evaluated once, phull3 twice. Any point that is in the convex hull of rbind(ps,xs) is either in or outside the convex hull of ps. Right? So, just recursively eliminate points that are in the convex hull of the larger set. If I'm not mistaken this method is efficient only because the two point distributions are very similar (drawn from rnorm, so they look like two concentric balls). If one of the convex hulls is very distorted along one axis, say, I believe the method will involve many more iterations and in the limit will require computing a convex hull for each test point as Duncan suggested. Such a pathological of test points example might be, xs - matrix(0,ncol=4,nrow=100) xs[,1] - seq(1,100) Or did I completely miss something? (quite possible) Regarding the inhull Matlab code, I came to the opposite conclusion: it should be easily ported to R. 1) it is a very short piece of code (even more so if one disregards the various checks and handling of special cases), with no Matlab-specific objects (only integers, booleans, matrices and vectors). 2) The core of the program relies on the qhull library, and the same applies to R I think. 3) Matlab and R use very similar indexing for matrices and similar linear algebra in general. That said, I'm a bit short on time to give it a go myself. I think the open-source Octave could run this code too, so it might help in checking the code step-by-step. All the best, baptiste __ 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] get the enclosing function name
Hi, .NotYetImplemented gives an example, function () stop(gettextf('%s' is not implemented yet, as.character(sys.call(sys.parent())[[1L]])), call. = FALSE) environment: namespace:base HTH, baptiste 2009/12/11 Hao Cen h...@andrew.cmu.edu: Hi, Is there a way to get the enclosing function name within a function? For example, I would like to have a function getEnclosingFunctionName(). It works like below f = function(){ print(getEnclosingFunctionName()) } f() # will print f Thanks Jeff __ 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.
Re: [R] multidimensional point.in.polygon??
Hi, Regarding your proposed algorithm (looks like it is indeed the correct way to do it), there seem to be a somewhat similar Matlab implementation, http://www.mathworks.com/matlabcentral/fileexchange/10226-inhull It should be possible to port this to R (you might want to check what to do with the author's license, eventually). Also, you might have better luck on this list if you provide a small example for people to play with. Best, baptiste 2009/12/10 Keith Jewell k.jew...@campden.co.uk: Hi, Doing some more reading, I think the problem is easier because the hull is convex. Then an algorithm for testing points might be: a) Define the convex hull as a set of planes (simplexes). [as returned by convhulln!!] b) Define one point, i, known to be interior [e.g. mean of all the points defining the hull] c) If point x is i) for every plane, on the same side as i; x is interior ii) for every plane, on the same side as i or in the plane; x is in the surface iii) else x is exterior So now I need to find the directions of points from multidimensional planes.Perhaps I can find the vectors of the perpendiculars from the points to the planes (possibly extended) and test for parallel/anti-parallel? I feel that I'm in the right direction because this uses the structure of a convex hull returned by convhulln. But, I still feel I'm re-inventing the wheel. Surely this has been done before? Isn't a (the?) major purpose of a convex hull to test other points for inclusion? Perhaps when I get the geometry sorted this will be so easy I'll understand why noone has pointed me to an existing R function, but currently I feel I and Baptiste are wandering in the dark :-( Any hints? Thanks in advance, Keith Jewell - baptiste auguie baptiste.aug...@googlemail.com wrote in message news:de4e29f50912040550m71fbffafnfa1ed6e0f4451...@mail.gmail.com... Hi, Yet another one of my very naive ideas on the subject: maybe you can first evaluate the circumscribed and inscribed spheres of the base set of points (maximum and minimum of their distances to the center of gravity). Any points within a distance smaller than the infimum is good, any point further than the supremum is not good. This should be faster than the calculation of a convex hull for each point. Of course the usefulness of this first test really depends on how aspherical is your base convex hull. I do hope to read a real answer from someone who knows this stuff! HTH, baptiste 2009/12/4 Keith Jewell k.jew...@campden.co.uk: Hi, I seek to identify those points in/outside a multidimensional convex hull (geometry::convhulln). Any suggestions? Background just in case I'm going down a really wrong road: Given an observed data set with one dependent/observed variable (Y) and multiple (3 to 10) independent/design variables (X1, X2, ...) I want to increase the number of points by interpolating. I'm using expand.grid(X) to generate the X points and locfit::predict.locfit to interpolate the Y values. No problem so far. BUT my observed X data set is very far from rectangular, so the set of points produced by expand.grid includes many extrapolations which I'd want to remove. I'm aware of the problems of defining extrapolation in multiple dimensions and am minded to define it as outside the convex hull, hence my question. In searching r-project.org I came across a thread in which baptiste auguie said one general way to do this would be to compute the convex hull (?chull) of the augmented set of points and test if the point belongs to it; an approach I'd considered generalising to multiple points thus (pseudo R code)... baseHull - convhulln(baseSet) augHull - convhulln(augSet) while (augHull != baseHull) { augSet - augSet [-(augHull !%in% baseHull)] augHull - convhulln(augSet) } ... but this has that horrible loop including a convhulln! In the (real, typical) test data set I'm using for development baseSet is 5 columns by 2637 rows while baseHull has only 42 distinct points. If augHull has a similarly simple hull, then each time round the loop will remove only a few dozen points; I need to remove many thousands. And (in my naivete) I think there must be a better way of testing for a point inside a polygon, I'd have thought convhulln must need to do that often! Thanks in advance for any pointers, Keith Jewell __ 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
Re: [R] Avoid for-loop in creating data.frames
Hi, Is the following close enough? apply(set2, 2, function(x) x[is.na(x)]) HTH, baptiste 2009/12/10 Andreas Wittmann andreas_wittm...@gmx.de: Dear R-users, after several tries with lapply and searching the mailing list, i want to ask, wheter and how it is possibly to avoid the for-loop in the following piece of code? set2-as.data.frame(matrix(rnorm(9),ncol=3)) set2[1,1] - NA set2[3,2] - NA set2[2,1] - NA dimnames(set2)[1] - list(c(A,B,C)) r - !is.na(set2) imp - vector(list, ncol(set2)) for (j in 1:dim(set2)[2]) { imp[[j]] - as.data.frame(matrix(NA, nrow = sum(!r[,j]), ncol = 1)) dimnames(imp[[j]]) - list(row.names(set2)[r[,j] == FALSE], 1) } many thanks and best regards Andreas __ 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.
Re: [R] Greek symbols on ylab= using barchart() {Lattice}
Hi, try this, barchart(1:2, ylab=expression(mu*g/m^3)) ?plotmath baptiste 2009/12/9 Peng Cai pengcaimaill...@gmail.com: Hi All, I'm trying to write ug/m3 as y-label, with greek letter mu replacing u AND 3 going as a power. These commands works in general: plot.new() text(0.5, 0.5, expression(symbol(m))) But, I'm sure about how to do it using barchart() from Lattice. Can anyone help please? Thanks, Peng Cai [[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-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.