Re: [R] Different behavior of mtext
Ok, the problem is clear now. I did not get that 'user-coordinates' was refering to par(usr), when I read the help of mtext. If I may ask you some additional questions: - you mentioned a missing unit() call ; at which point should it be done in my code examples ? - could you give me some advices or helpful links about how to set up a grid viewport ? - and finally, probably a stupid question: is a gridview automatically set up when a plotting function is called ? Sebastien PS: To answer to your final question, my goal is to center a block of legend text on the device but to align the text to the left of this block. Prof Brian Ripley a écrit : On Sun, 2 Sep 2007, Sébastien wrote: Dear R Users, I am quite surprised to see that mtext gives different results when it is used with 'pairs' and with plot'. In the two following codes, it seems that the 'at' argument in mtext doesn't consider the same unit system. It is stated to be in 'user coordinates'. Your code does not work because unit() is missing. If you mean the one from package grid, npc is not user coordinates (and refers to a grid viewport which you have not set up and coincidentally is the same as the initial user coordinate system to which pairs() has reverted). Try par(usr) after your pairs() and plot() calls to see the difference. Plotting a 2x2 array of plots _is_ different from plotting one, so this should be as expected. Since centring is the default for 'adj', it is unclear what you are trying to achieve here. I would appreciate your comments on this issue. Sebastien # Pairs mydata-data.frame(x=1:10,y=1:10) par(cex.main=1, cex.axis=1, cex.lab=1, lwd=1, mar=c(5 + 5,4,4,2)+0.1) pairs(mydata,oma=c(5 + 5,4,4,2)) mylegend-c(mylegend A,mylegend B,mylegend C,mylegend test) mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))], figure) for (i in 1:4) { mtext(text=mylegend[i], side = 1, line = 3+i, at = unit((1-mylegend.width)/2,npc),# centers the legend at the bottom adj=0, padj=0)} # plot mydata-data.frame(x=1:10,y=1:10) par(cex.main=1, cex.axis=1, cex.lab=1, lwd=1, mar=c(5 + 5,4,4,2)+0.1) plot(mydata,oma=c(5 + 5,4,4,2)) mylegend-c(mylegend A,mylegend B,mylegend C,mylegend test) mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))], figure) for (i in 1:4) { mtext(text=mylegend[i], side = 1, line = 3+i, at = unit((1-mylegend.width)/2,npc),# should center the legend at the bottom but doesn't do it ! adj=0, padj=0)} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Different behavior of mtext
Thanks for the information on gridBase, I could solve my problem using the 'baseViewports' function and by replacing mtext by grid.text (with coordinates adjustments). Sebastien Prof Brian Ripley a écrit : On Mon, 3 Sep 2007, Sébastien wrote: Ok, the problem is clear now. I did not get that 'user-coordinates' was refering to par(usr), when I read the help of mtext. If I may ask you some additional questions: - you mentioned a missing unit() call ; at which point should it be done in my code examples ? Before it is used. The problem is that I believe more than one package has a unit() function. - could you give me some advices or helpful links about how to set up a grid viewport ? - and finally, probably a stupid question: is a gridview automatically set up when a plotting function is called ? If you want to mix grid and base graphics, you need package gridBase, but really I would not advise a beginner to be using grid directly (that is, not via lattice to ggplot*). Sebastien PS: To answer to your final question, my goal is to center a block of legend text on the device but to align the text to the left of this block. Prof Brian Ripley a écrit : On Sun, 2 Sep 2007, Sébastien wrote: Dear R Users, I am quite surprised to see that mtext gives different results when it is used with 'pairs' and with plot'. In the two following codes, it seems that the 'at' argument in mtext doesn't consider the same unit system. It is stated to be in 'user coordinates'. Your code does not work because unit() is missing. If you mean the one from package grid, npc is not user coordinates (and refers to a grid viewport which you have not set up and coincidentally is the same as the initial user coordinate system to which pairs() has reverted). Try par(usr) after your pairs() and plot() calls to see the difference. Plotting a 2x2 array of plots _is_ different from plotting one, so this should be as expected. Since centring is the default for 'adj', it is unclear what you are trying to achieve here. I would appreciate your comments on this issue. Sebastien # Pairs mydata-data.frame(x=1:10,y=1:10) par(cex.main=1, cex.axis=1, cex.lab=1, lwd=1, mar=c(5 + 5,4,4,2)+0.1) pairs(mydata,oma=c(5 + 5,4,4,2)) mylegend-c(mylegend A,mylegend B,mylegend C,mylegend test) mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))], figure) for (i in 1:4) { mtext(text=mylegend[i], side = 1, line = 3+i, at = unit((1-mylegend.width)/2,npc),# centers the legend at the bottom adj=0, padj=0)} # plot mydata-data.frame(x=1:10,y=1:10) par(cex.main=1, cex.axis=1, cex.lab=1, lwd=1, mar=c(5 + 5,4,4,2)+0.1) plot(mydata,oma=c(5 + 5,4,4,2)) mylegend-c(mylegend A,mylegend B,mylegend C,mylegend test) mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))], figure) for (i in 1:4) { mtext(text=mylegend[i], side = 1, line = 3+i, at = unit((1-mylegend.width)/2,npc),# should center the legend at the bottom but doesn't do it ! adj=0, padj=0)} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Different behavior of mtext
Dear R Users, I am quite surprised to see that mtext gives different results when it is used with 'pairs' and with plot'. In the two following codes, it seems that the 'at' argument in mtext doesn't consider the same unit system. I would appreciate your comments on this issue. Sebastien # Pairs mydata-data.frame(x=1:10,y=1:10) par(cex.main=1, cex.axis=1, cex.lab=1, lwd=1, mar=c(5 + 5,4,4,2)+0.1) pairs(mydata,oma=c(5 + 5,4,4,2)) mylegend-c(mylegend A,mylegend B,mylegend C,mylegend test) mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))], figure) for (i in 1:4) { mtext(text=mylegend[i], side = 1, line = 3+i, at = unit((1-mylegend.width)/2,npc),# centers the legend at the bottom adj=0, padj=0)} # plot mydata-data.frame(x=1:10,y=1:10) par(cex.main=1, cex.axis=1, cex.lab=1, lwd=1, mar=c(5 + 5,4,4,2)+0.1) plot(mydata,oma=c(5 + 5,4,4,2)) mylegend-c(mylegend A,mylegend B,mylegend C,mylegend test) mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))], figure) for (i in 1:4) { mtext(text=mylegend[i], side = 1, line = 3+i, at = unit((1-mylegend.width)/2,npc),# should center the legend at the bottom but doesn't do it ! adj=0, padj=0)} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Limiting size of pairs plots
Dear R-users, I would like to add a legend at the bottom of pairs plots (it's my first use of this function). With the plot function, I usually add some additional space at the bottom when I define the size of the graphical device (using mar); grid functions then allows me to draw my legend as I want. Unfortunatley, this technique does not seem to work with the pairs function as the generated plots use all the available space on the device (see below). I guess I am missing a key argument... my attempts to modify the oma, mar, usr arguments were unsuccesfull, and I could not find any helpful threads on the archives. As usual, any advice would be greatly appreciated Sebastien pdf(file=C:/test.pdf, width=6, height= 6 + 0.2*6) par(mar=c(5 + 6,4,4,2)+0.1) pairs(iris[1:4], main = Anderson's Iris Data -- 3 species, pch = 21, bg = c(red, green3, blue)[unclass(iris$Species)]) dev.off() __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Factor levels
Dear R-users, I have found this not-so-recent post in the archives - http://tolstoy.newcastle.edu.au/R/devel/00a/0291.html - while I was looking for a particular way to reorder factor levels. The question addressed by the author was to know if the read.table function could be modified to order the levels of newly created factors according to the order that they appear in the data file. Exactly what I am looking for. As there was no reply to this post, I wonder if any move have been made towards the implementation of this suggestion. A quick look at ?read.table tells me that if this option was implemented, it was not in the read.table function... Sebastien PS: I am sorry to post so many messages on the list, but I am learning R (basically by trials errors ;-) ) and no one around me has even a slight notion about it... __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Factor levels
Thanks Gabor, I have two questions: 1- Is there any difference between your code and the following one, with regards to Fld2 ? ### test ### Input - Fld1 Fld2 10 A 20 B 30 C 40 A DF - read.table(textConnection(Input), header = TRUE) DF$Fld2-factor(DF$Fld2,levels= c(C, A, B))) 2- do you see any way to bring flexibility to your method ? Because, it looks to me as, at this stage, I have to i) know the order of my levels before I read the table and ii) create one class per factor. My problem is that I am not really working on a specific dataset. My goal is to develop R scripts capable of handling datasets which have various contents but close structures. So, I really need to minimize the quantity of user-specific code. Sebastien Gabor Grothendieck a écrit : You can create your own class and pass that to read table. In the example below Fld2 is read in with factor levels C, A, B in that order. library(methods) setClass(my.levels) setAs(character, my.levels, function(from) factor(from, levels = c(C, A, B))) ### test ### Input - Fld1 Fld2 10 A 20 B 30 C 40 A DF - read.table(textConnection(Input), header = TRUE, colClasses = c(numeric, my.levels)) str(DF) # or DF - read.table(textConnection(Input), header = TRUE, colClasses = list(Fld2 = my.levels)) str(DF) On 8/28/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, I have found this not-so-recent post in the archives - http://tolstoy.newcastle.edu.au/R/devel/00a/0291.html - while I was looking for a particular way to reorder factor levels. The question addressed by the author was to know if the read.table function could be modified to order the levels of newly created factors according to the order that they appear in the data file. Exactly what I am looking for. As there was no reply to this post, I wonder if any move have been made towards the implementation of this suggestion. A quick look at ?read.table tells me that if this option was implemented, it was not in the read.table function... Sebastien PS: I am sorry to post so many messages on the list, but I am learning R (basically by trials errors ;-) ) and no one around me has even a slight notion about it... __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Factor levels
Ok, I cannot send to you one of my dataset since they are confidential. But I can produce a dummy mini dataset to illustrate my question. Let's say I have a csv file with 3 columns and 20 rows which content is reproduced by the following line. mydata-data.frame(a=1:20, b=sample(100:200,20,replace=T),c=sample(letters[1:26], 20, replace = T)) mydata a b c 1 1 176 w 2 2 141 k 3 3 172 r 4 4 182 s 5 5 123 k 6 6 153 p 7 7 176 l 8 8 170 u 9 9 140 z 10 10 194 s 11 11 164 j 12 12 100 j 13 13 127 x 14 14 137 r 15 15 198 d 16 16 173 j 17 17 113 x 18 18 144 w 19 19 198 q 20 20 122 f If I had to read the csv file, I would use something like: mydata-data.frame(read.table(file=c:/test.csv,header=T)) Now, if you look at mydata$c, the levels are alphabetically ordered. mydata$c [1] w k r s k p l u z s j j x r d j x w q f Levels: d f j k l p q r s u w x z What I am trying to do is to reorder the levels as to have them in the order they appear in the table, ie Levels: w k r s p l u z j x d q f Again, keep in mind that my script should be used on datasets which content are unknown to me. In my example, I have used letters for mydata$c, but my code may have to handle factors of numeric or character values (I need to transform specific columns of my dataset into factors for plotting purposes). My goal is to let the code scan the content of each factor of my data.frame during or after the read.table step and reorder their levels automatically without having to ask the user to hard-code the level order. In a way, my problem is more related to the way the factor levels are ordered than to the read.table function, although I guess there is a link... Gabor Grothendieck a écrit : Its not clear from your description what you want. Could you be a bit more specific including an example. On 8/28/07, Sébastien [EMAIL PROTECTED] wrote: Thanks Gabor, I have two questions: 1- Is there any difference between your code and the following one, with regards to Fld2 ? ### test ### Input - Fld1 Fld2 10 A 20 B 30 C 40 A DF - read.table(textConnection(Input), header = TRUE) DF$Fld2-factor(DF$Fld2,levels= c(C, A, B))) 2- do you see any way to bring flexibility to your method ? Because, it looks to me as, at this stage, I have to i) know the order of my levels before I read the table and ii) create one class per factor. My problem is that I am not really working on a specific dataset. My goal is to develop R scripts capable of handling datasets which have various contents but close structures. So, I really need to minimize the quantity of user-specific code. Sebastien Gabor Grothendieck a écrit : You can create your own class and pass that to read table. In the example below Fld2 is read in with factor levels C, A, B in that order. library(methods) setClass(my.levels) setAs(character, my.levels, function(from) factor(from, levels = c(C, A, B))) ### test ### Input - Fld1 Fld2 10 A 20 B 30 C 40 A DF - read.table(textConnection(Input), header = TRUE, colClasses = c(numeric, my.levels)) str(DF) # or DF - read.table(textConnection(Input), header = TRUE, colClasses = list(Fld2 = my.levels)) str(DF) On 8/28/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, I have found this not-so-recent post in the archives - http://tolstoy.newcastle.edu.au/R/devel/00a/0291.html - while I was looking for a particular way to reorder factor levels. The question addressed by the author was to know if the read.table function could be modified to order the levels of newly created factors according to the order that they appear in the data file. Exactly what I am looking for. As there was no reply to this post, I wonder if any move have been made towards the implementation of this suggestion. A quick look at ?read.table tells me that if this option was implemented, it was not in the read.table function... Sebastien PS: I am sorry to post so many messages on the list, but I am learning R (basically by trials errors ;-) ) and no one around me has even a slight notion about it... __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal
Re: [R] Factor levels
Peter, Gabor: thanks to both of you. This 'unique' function is what I was looking for ! Peter Alspach a écrit : Sebastain Does the following work for you? seb - read.table(file='clipboard', header=T) seb$c [1] w k r s k p l u z s j j x r d j x w q f Levels: d f j k l p q r s u w x z seb$c - factor(seb$c, levels=unique(seb$c)) seb$c [1] w k r s k p l u z s j j x r d j x w q f Levels: w k r s p l u z j x d q f Peter Alspach -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Sébastien Sent: Wednesday, 29 August 2007 9:00 a.m. To: Gabor Grothendieck Cc: R-help Subject: Re: [R] Factor levels Ok, I cannot send to you one of my dataset since they are confidential. But I can produce a dummy mini dataset to illustrate my question. Let's say I have a csv file with 3 columns and 20 rows which content is reproduced by the following line. mydata-data.frame(a=1:20, b=sample(100:200,20,replace=T),c=sample(letters[1:26], 20, replace = T)) mydata a b c 1 1 176 w 2 2 141 k 3 3 172 r 4 4 182 s 5 5 123 k 6 6 153 p 7 7 176 l 8 8 170 u 9 9 140 z 10 10 194 s 11 11 164 j 12 12 100 j 13 13 127 x 14 14 137 r 15 15 198 d 16 16 173 j 17 17 113 x 18 18 144 w 19 19 198 q 20 20 122 f If I had to read the csv file, I would use something like: mydata-data.frame(read.table(file=c:/test.csv,header=T)) Now, if you look at mydata$c, the levels are alphabetically ordered. mydata$c [1] w k r s k p l u z s j j x r d j x w q f Levels: d f j k l p q r s u w x z What I am trying to do is to reorder the levels as to have them in the order they appear in the table, ie Levels: w k r s p l u z j x d q f Again, keep in mind that my script should be used on datasets which content are unknown to me. In my example, I have used letters for mydata$c, but my code may have to handle factors of numeric or character values (I need to transform specific columns of my dataset into factors for plotting purposes). My goal is to let the code scan the content of each factor of my data.frame during or after the read.table step and reorder their levels automatically without having to ask the user to hard-code the level order. In a way, my problem is more related to the way the factor levels are ordered than to the read.table function, although I guess there is a link... Gabor Grothendieck a écrit : Its not clear from your description what you want [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to provide argument when opening RGui from an external application
Thanks everyone. I actually thought about ?Rscript.exe but, having used only Rgui, I thought it was a instruction specific to this interface. I will look into it. Sebastien Gabor Grothendieck a écrit : There are also some batch files that can be used with Rscript on XP and info in the README here: http://batchfiles.googlecode.com On 8/26/07, Sébastien [EMAIL PROTECTED] wrote: Thanks for your reply. When you say look into Rscript.exe, do you have a specific document in mind ? I tried to google it but could not find much... I forgot to mention in my first email that I am working under the Windows XP environment. Prof Brian Ripley a écrit : Look into Rscript.exe (on Windows), which is a flexible way to run scripts. Neither using a GUI nor using source() are recommended. On Fri, 24 Aug 2007, Sébastien wrote: Dear R-users, I have written a small application (in visual basic) that automatically generate some R scripts. I would like to execute these scripts when my application is being closed. My problem is that I don't know how to pass the 'source(c:/.../myscript.r)' instruction when I programmatically start RGui. Tinn-R is capable of doing such things, so I guess there must be a way to pass arguments to RGui. Any advice or link to relevant references would be greatly appreciated. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to provide argument when opening RGui from an external application
Thanks for your reply. When you say look into Rscript.exe, do you have a specific document in mind ? I tried to google it but could not find much... I forgot to mention in my first email that I am working under the Windows XP environment. Prof Brian Ripley a écrit : Look into Rscript.exe (on Windows), which is a flexible way to run scripts. Neither using a GUI nor using source() are recommended. On Fri, 24 Aug 2007, Sébastien wrote: Dear R-users, I have written a small application (in visual basic) that automatically generate some R scripts. I would like to execute these scripts when my application is being closed. My problem is that I don't know how to pass the 'source(c:/.../myscript.r)' instruction when I programmatically start RGui. Tinn-R is capable of doing such things, so I guess there must be a way to pass arguments to RGui. Any advice or link to relevant references would be greatly appreciated. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to provide argument when opening RGui from an external application
Dear R-users, I have written a small application (in visual basic) that automatically generate some R scripts. I would like to execute these scripts when my application is being closed. My problem is that I don't know how to pass the 'source(c:/.../myscript.r)' instruction when I programmatically start RGui. Tinn-R is capable of doing such things, so I guess there must be a way to pass arguments to RGui. Any advice or link to relevant references would be greatly appreciated. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] 'From' and 'to' arguments in panel.abline
Dear R-users, The help does not give much details on the use of the arguments 'from' and 'to' in the panel.abline function. I have looked in the archives but did not find how to implement them. My different tries failed miserably. E.g, the following code doesn't seem to work, in a sense that the line is not limited to the (0,10) range. Do these arguments really apply to panel.abline? If so, how should they be specified? xy-data.frame(x-0.1:10,y-0.1:10) xyplot(y~x,data=xy, panel = function(x, y, ...){ panel.abline(a=0,b=1,from=0,to=10) panel.xyplot(x,y) }) Thanks you in advance for your help. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] 'From' and 'to' arguments in panel.abline
Thanks Deepayan, I actually looked at the upper section but couldn't not spot these arguments. I thought they were optional arguments in '...'. I will try to open my eyes next time ;) Deepayan Sarkar a écrit : On 8/13/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, The help does not give much details on the use of the arguments 'from' and 'to' in the panel.abline function. Not surprising, since panel.abline doesn't actually have arguments called 'from' and 'to'. I have looked in the archives but did not find how to implement them. My different tries failed miserably. E.g, the following code doesn't seem to work, in a sense that the line is not limited to the (0,10) range. Do these arguments really apply to panel.abline? No, they do not. It is common to have many functions documented in one help page, and you need to look at the usage section (near the top) to figure out which arguments are releant for which functions. The help page has: Usage: panel.abline(a = NULL, b = 0, h = NULL, v = NULL, reg = NULL, coef = NULL, col, col.line, lty, lwd, type, ...) panel.curve(expr, from, to, n = 101, curve.type = l, col, lty, lwd, type, ...) etc. If you want to limit the range, use panel.curve, e.g. panel.curve(0 + 1 * x,from=0,to=10) -Deepayan If so, how should they be specified? xy-data.frame(x-0.1:10,y-0.1:10) xyplot(y~x,data=xy, panel = function(x, y, ...){ panel.abline(a=0,b=1,from=0,to=10) panel.xyplot(x,y) }) Thanks you in advance for your help. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to control the number format on plot axes ?
Dear R-users, Basically, everything is in the title of my e-mail. I know that some threads from the archives have already addressed this question but they did not really give a clear solution. Here is a series of short codes that will illustrate the problem: # First a-1:10 plot(x=a,y=a,log=y,type=p) # Second a-1:10 myTicks-c(1,2,5,10) plot(x=a,y=a,log=y,type=p,yaxt=n) axis(side=2,at=myTicks) # Third a-1:10 myTicks-c(0.1,1,2,5,10) plot(x=a,y=a,log=y,type=p,yaxt=n) axis(side=2,at=myTicks) # Forth a-0.1:10 plot(x=a,y=a,log=y,type=p) In the first and second examples, the plots are identical and the tick labels are 1, 2, 5 and 10. In the third, the labels are number in the x.0 format (1.0, 2.0, 5.0 and 10.0), even if there is no point below 1. The only reason I see is because the first element of myTicks is 0.1. And, the forth example is self-explanatory. Interestingly, the 'scales' argument of xyplot in the lattice package do not add these (unnecessary) decimals on labels greater than 1. Do you know how I could transpose the behavior of the lattice 'scales' argument to the 'axis' function ? Thank you PS: No offense, but please don't suggest I use lattice. I have to go for base R graphics in my full-scale project (it is a speed issue). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to control the number format on plot axes ?
Thanks Jim, This sprintf function was exactly what I was looking for. jim holtman a écrit : Here is a way that you can put the formatting that you want; you were not clear on exactly what you were after. You can setup the 'labels' argument for whatever you want. a-1:10 myTicks-c(0.1,1,2,5,10) # set ylim to range of myTicks that you want plot(x=a,y=a,log=y,type=p,yaxt=n, ylim=range(myTicks)) # change the sprintf to whatever formatting you want axis(side=2,at=myTicks, labels=ifelse(myTicks = 1, sprintf(%.0f, myTicks), sprintf(%0.1f, myTicks))) On 8/12/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, Basically, everything is in the title of my e-mail. I know that some threads from the archives have already addressed this question but they did not really give a clear solution. Here is a series of short codes that will illustrate the problem: # First a-1:10 plot(x=a,y=a,log=y,type=p) # Second a-1:10 myTicks-c(1,2,5,10) plot(x=a,y=a,log=y,type=p,yaxt=n) axis(side=2,at=myTicks) # Third a-1:10 myTicks-c(0.1,1,2,5,10) plot(x=a,y=a,log=y,type=p,yaxt=n) axis(side=2,at=myTicks) # Forth a-0.1:10 plot(x=a,y=a,log=y,type=p) In the first and second examples, the plots are identical and the tick labels are 1, 2, 5 and 10. In the third, the labels are number in the x.0 format (1.0, 2.0, 5.0 and 10.0), even if there is no point below 1. The only reason I see is because the first element of myTicks is 0.1. And, the forth example is self-explanatory. Interestingly, the 'scales' argument of xyplot in the lattice package do not add these (unnecessary) decimals on labels greater than 1. Do you know how I could transpose the behavior of the lattice 'scales' argument to the 'axis' function ? Thank you PS: No offense, but please don't suggest I use lattice. I have to go for base R graphics in my full-scale project (it is a speed issue). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Replace NAs in dataframe: what am I doing wrong
Dear R-users, My script imports a dataset from a csv file, in which missing values are represented by .. This importation is done into a dataframe using the read.table function with na.strings = . Then I want to replace the NAs in the first column of the dataframe by Missing data. I am using the following code to do so : mydata-data.frame(read.table(myFile,sep=,,header=TRUE,na.strings=.)) # myFile is the full path of the source file mydata[,1][is.na(mydata[,1])]-Missing value This code works perfectly fine if this first column contains only missing values, i.e. .. As soon as it contains multiple levels and missing values, things start to get wrong. I get the following error message and the replacement is not done. Warning message: invalid factor level, NAs generated in: `[-.factor`(`*tmp*`, is.na(mydata[, 1]), value = Missing value) Is there an error in my code or is that a bug (I doubt about it) ? Thanks in advance. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replace NAs in dataframe: what am I doing wrong
Thanks you very much Jim, As usual, your reply was very helpful (although I did not directly apply it). My dataset contains many columns, including numerous columns that I wanted to keep as factor. Instead of adding as.is = true at the readtable step, I am using the same philosophy but on a limited range. I simply change the type of my column of interest using as.character, then do the replacement and finally change the column back to factor. It does the trick and, this way, I do not have to change all my columns to factors. Thank again for your help. Sebastien jim holtman a écrit : The problem is that the first column is probably a factor and you are trying to assign a value that is not already a 'level' in the factor. One way is to read the data with as.is=TRUE to keep it as character, replace the NAs and then convert back to factors if you want to: x - read.csv(textConnection(A,B + a,3 + b,4 + .,. + c,5), na.strings='.', as.is=TRUE) # keep as character # replace NAs x[is.na(x[,1]), 1] - Missing Value # convert back to factors if you want to x[[1]] - factor(x[[1]]) str(x) 'data.frame': 4 obs. of 2 variables: $ A: Factor w/ 4 levels a,b,c,Missing Value: 1 2 4 3 $ B: int 3 4 NA 5 On 8/11/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, My script imports a dataset from a csv file, in which missing values are represented by .. This importation is done into a dataframe using the read.table function with na.strings = . Then I want to replace the NAs in the first column of the dataframe by Missing data. I am using the following code to do so : mydata-data.frame(read.table(myFile,sep=,,header=TRUE,na.strings=.)) # myFile is the full path of the source file mydata[,1][is.na(mydata[,1])]-Missing value This code works perfectly fine if this first column contains only missing values, i.e. .. As soon as it contains multiple levels and missing values, things start to get wrong. I get the following error message and the replacement is not done. Warning message: invalid factor level, NAs generated in: `[-.factor`(`*tmp*`, is.na(mydata[, 1]), value = Missing value) Is there an error in my code or is that a bug (I doubt about it) ? Thanks in advance. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error in using nlevels in apply function
Dear R users, I am currently trying to create my first personnal function and use it with the apply function. The purpose of this function is to create a vector summarizing the number of levels in a given selection of data.frame columns. I tried to transpose the indexation method used by the nlevels function but it doesn't seem to work. I did not find anything uesful in the archives so could someone point to me where my mistake(s) is (are) ? Thanks in advance Sebastien #- mydata-data.frame(1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6) mycol.index-c(1,5,3) nelem-function(x,col.id) nlevels(factor(x[,col.id])) my.nlevels.col-apply(mydata,2,nelem,mycol.index) my.nlevels.col # #The error message is the following #Error in x[, col.id] : incorrect number of dimensions __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in using nlevels in apply function
Mike, Henrique, Thanks for your help. Your code is doing the job perfectly. Sébastien a écrit : Dear R users, I am currently trying to create my first personnal function and use it with the apply function. The purpose of this function is to create a vector summarizing the number of levels in a given selection of data.frame columns. I tried to transpose the indexation method used by the nlevels function but it doesn't seem to work. I did not find anything uesful in the archives so could someone point to me where my mistake(s) is (are) ? Thanks in advance Sebastien #- mydata-data.frame(1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6) mycol.index-c(1,5,3) nelem-function(x,col.id) nlevels(factor(x[,col.id])) my.nlevels.col-apply(mydata,2,nelem,mycol.index) my.nlevels.col # #The error message is the following #Error in x[, col.id] : incorrect number of dimensions __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Background color of symbols in xyplot
Dear R-users, I am using plot symbols given by pch=21:25 in a xyplot. The background color of these symbols can be defined by 'fill' in the panel argument, however I have a hard time to find how to define the same background color in the key. I tried different options like using the auto.key argument and modifying the par.settings, or using normal key argument plus 'fill', 'bg' or 'background'... nothing seems to do I want. What should I change in the following code to make it work ? key=list(space=bottom, points = list(pch = 21:25, col = 32, bg=15), text=list(mylegend)), Thank in advance Sebastien PS: cheng __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Background color of symbols in xyplot
Thank you very much for your help. I have installed the latest versions of R and lattice are installed, and now the 'fill' argument (instead of bg) gives me what I want. [EMAIL PROTECTED] a écrit : On 8/2/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, I am using plot symbols given by pch=21:25 in a xyplot. The background color of these symbols can be defined by 'fill' in the panel argument, however I have a hard time to find how to define the same background color in the key. I tried different options like using the auto.key argument and modifying the par.settings, or using normal key argument plus 'fill', 'bg' or 'background'... nothing seems to do I want. What should I change in the following code to make it work ? key=list(space=bottom, points = list(pch = 21:25, col = 32, bg=15), text=list(mylegend)), For starters, upgrade to the latest version of R and lattice. -Deepayan [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] two plots super-impose
Hi Philip, par(new=TRUE) before your hist(y) lines should do the trick. You may have to modify some settings in case the axes are not overlayed perfectly. Sebastien [EMAIL PROTECTED] a écrit : Dear R users, I used plot(x,y) to draw a scatter plot. I need to add another smaller histogram on the same plot I just had. However when I used hist(y), the histogram was on another page. Any suggestion? Best Regards, Philip [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Problem to remove loops in a routine
Dear R-users, I have written the following code to generate some trellis plots. It works perfectly fine except that it is quite slow when it is apply to my typical datasets (over several thousands of lines). I believe the problem comes from the loops I am using to subset my data.frame. I read in the archives that the tapply function is often more efficient than a loop in R. Unfortunately , it seems that I am not enough familiar with the philosophy of this function to implement it in my code. Would you have some suggestions to speed up the whole thing? Thanks in advance Sebastien PS: the rationale behind these loops is to split the trellis plots on different pages, all plots on a page (or a group of pages) having a given combination of values for the PLOT, DVID, PER and GRP parameters. ### library(lattice) rm(list=ls(all=TRUE)) # Generate a dummy dataset with # - 20 individuals (ID) # - individuals 1 to 10 belong to group (GRP) 1, 11 to 20 belong to group 2 # - measurements (DV) done at 10 time points (TIME) per individuals on 2 occassions (OCC) # - modelisation of the DV versus TIME relationships with 4 different models (MODEL) # - predicted values (Y) # - the PLOT column serves as a flag to plot together the models (A and B) and (C and D) PLOT-rep(1:2,each=40,times=20) ID-rep(1:20,each=80) OCC-rep(1:2,each=10,times=80) GRP-as.numeric(rep(gl(2,80),times=10)) MODEL-as.vector(rep(gl(4,20,label=c(A,B,C,D)),times=20)) TIME-rep.int(1:10,160) DV-OCC*(1:10)*rep(rnorm(20,50,10),each=80)+rep(rnorm(20,10,1),each=80) Y-jitter(DV) mydata-data.frame(PLOT,ID,OCC,GRP,MODEL,TIME,DV,Y) mydata$DVID-rep.int(1,1600) #in a real dataset, DVID could have typically 2 to 3 levels # # Plotting routine # myPath-C:/#TO BE MODIFIED nTrellisCol-2#number of columns per Trellis plot nTrellisRow-3#number of lines per Trellis plot nDVID-nlevels(factor(mydata$DVID))#number of DVID=observations types nidPlot-nlevels(factor(mydata$PLOT))#number of items in the PLOT column nPer-nlevels(factor(mydata$OCC)) #number of occassions (OCC, PER, etc...) nGRP-nlevels(factor(mydata$GRP)) #number of groups pdf(file=paste(myPath,test.pdf,sep=)) trellis.par.set(par.main.text=list(cex=1)) trellis.par.set(par.ylab.text=list(font=2)) trellis.par.set(par.xlab.text=list(font=2)) for (i in 1:nidPlot) { #loop on PLOT id #i=1 idata-subset(mydata,mydata$PLOT==i) for (j in 1:nDVID) { #loop on DVID #j=1 ijdata-subset(idata,idata$DVID==j) for (k in 1:nPer) { #loop on Period #k=1 ijkdata-subset(ijdata,ijdata$OCC==k) for (l in 1:nGRP) { #loop on Group # l=1 subdata-subset(ijkdata,ijkdata$GRP==l) nModel-nlevels(factor(subdata$MODEL)) #number of models to be plotted in this loop mylegend-c(Raw data,levels(factor(subdata$MODEL))) subID-nlevels(factor(subdata$ID)) #number of ID in the new dataset myplot-xyplot(Y ~ TIME | ID, #creates plot data = subdata, type = l, groups = MODEL, observed = subdata$DV, as.table=TRUE, panel = function(x, y, ..., subscripts, observed) { panel.points(x, pch=3,col=1,observed[subscripts]) panel.xyplot(x, y, ..., col=2:nlevels(subdata$MODEL),subscripts = subscripts)}, strip=function (which.panel,...){ col-rep(Black,subID) llines(c(0,1,1,0,0),c(0,0,1,1,0),col.line=1) ltext(rep(0.5,subID),rep(0.5,subID), paste(Subject ,levels(factor(subdata$ID))[which.panel],sep=),cex=trellis.par.get(axis.text)[2])}, key=list(space=bottom, lines = list(pch = as.integer(c(3,rep(,nModel))), type = c(p, gl(1,nModel,label=l)), col = 1:(nModel+1),cex=trellis.par.get(axis.text)[2]), text=list(mylegend, cex=trellis.par.get(axis.text)[2])), xlab=Time (hr), ylab=Concentration (ng/mL), layout=c(nTrellisCol,nTrellisRow), main=paste(paste(paste(Plot ,i,sep=), paste(paste(, DVID ,j,sep=), paste(paste(, Occasion ,k,sep=), paste(, Group ,l,sep=,sep=)) trellis.par.set(par.xlab.text=list(cex=trellis.par.get(axis.text)[2])) trellis.par.set(par.ylab.text=list(cex=trellis.par.get(axis.text)[2]))
Re: [R] Several quick questions
Thanks all of you guys for your help. It will most helpful. [EMAIL PROTECTED] a écrit : On 7/7/07, Sébastien [EMAIL PROTECTED] wrote: Dear R users, Here is a couple a quick questions, for which I was unable to not find any answer in the list archives and in the help: [...] 2- When a log scale is called in a graph, the label takes a format like 10^n. That's true for lattice, but not traditional graphics, as far as I know. Is there a way to come back to a regular number format like 1, 10, 100... without having to create a custom axis ? Depends on what you mean by custom axis. You don't need to manually choose the tick positions etc, but you still need to define the rules that determine how they are calculated. See example(axis.default) for an example where the tick positions remain the same (as the defaults), but the labels change. The slightly different rule used in traditional graphics is available through the axTicks() function, which basically boils down to this: logTicks - function (lim, loc = c(1, 5)) { ii - floor(log10(range(lim))) + c(-1, 2) main - 10^(ii[1]:ii[2]) r - as.numeric(outer(loc, main, *)) r[lim[1] = r r = lim[2]] } where 'lim' is the limits in the original scale. So we have logTicks(c(1, 100)) [1] 1 5 10 50 100 logTicks(c(1, 100), loc = c(2, 5, 10)) [1] 1 2 5 10 20 50 100 3- In lattice graphics, how does the default value of the axs argument influence the values of limits ? This question should be considered in the following context. The help states that a 4% extension is applied by default to the axis range in base graphics. So, I have tried to apply this 4 % extension to create some custom lattice graphics. I worked on a dataset in which the independent variable ranged from 0 to 120, so I basically customized my axis using limits=c(-4.8,124.8). The results of the graphics with and without the limits command were not identical... The extension is user-settable in lattice, and defaults to 7% (I think this value came from Trellis specs, but I don't remember the exact details). lattice.getOption(axis.padding) $numeric [1] 0.07 $factor [1] 0.6 -Deepayan __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Several quick questions
Dear R users, Here is a couple a quick questions, for which I was unable to not find any answer in the list archives and in the help: 1- Is there any R equivalents of the VB functions Cint, CStr, etc... (for non VB users, these functions transform the category of a specified variable and smartly adapt the value of this variable) ? I have tried to use the as.numeric, as.factor and as.vector commands but the result is not exactly what I want ([1] 1, 3, 5, 6) a-as.factor(cbind(1,3,5,6)) # creates a dummy factor a [1] 1 3 5 6 Levels: 1 3 5 6 a-as.vector(as.numeric(a)) a [1] 1 2 3 4 2- When a log scale is called in a graph, the label takes a format like 10^n. Is there a way to come back to a regular number format like 1, 10, 100... without having to create a custom axis ? 3- In lattice graphics, how does the default value of the axs argument influence the values of limits ? This question should be considered in the following context. The help states that a 4% extension is applied by default to the axis range in base graphics. So, I have tried to apply this 4 % extension to create some custom lattice graphics. I worked on a dataset in which the independent variable ranged from 0 to 120, so I basically customized my axis using limits=c(-4.8,124.8). The results of the graphics with and without the limits command were not identical... Thanks in advance for your help. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R-excel
Hi, Directly import from Excel files should be possible using the RODBC package. Yu may want to read the R data import/export manual about the RODBC package for further details. Another solution is to save each of your Excel sheets as *.csv file and import the data in R using the read.table command (see ?read.table). Sebastien Erika Frigo a écrit : Good morning to everybody, I have a problem : how can I import excel files in R??? thank you very much Dr.sa. Erika Frigo Università degli Studi di Milano Facoltà di Medicina Veterinaria Dipartimento di Scienze e Tecnologie Veterinarie per la Sicurezza Alimentare (VSA) Via Grasselli, 7 20137 Milano Tel. 02/50318515 Fax 02/50318501 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Overlaying lattice graphs (continued)
Hi Deepayan, The following code creates a dummy dataset which has the same similar as my usual datasets. I did not try to implement the changes proposed by Hadley, hoping that a solution can be found using the original dataset. # My code # Creating dataset nPts-10# number of time points nInd-6 # number of individuals nModel-3 # number of models TimePts-rep(1:nPts,nInd*nModel)# creates the Time column Coef-rep(rnorm(6,0.1,0.01),each=nPts,nModel) # Creates a vector of coefficients for generating the observations Obs-10*exp(-Coef*TimePts) # creates the observations for (i in 1:60){ Pred[i]-jitter(10*exp(-Coef[i]*TimePts[i])) Pred[i+60]-jitter(5) Pred[i+120]-jitter(10-Coef[i+120]*TimePts[i]) } # creates the predicted values colPlot-rep(1,nPts*nInd*nModel) # creates the Plot column colModel-gl(nModel,nPts*nInd,labels=c(A,B,C)) # creates the Model column colID-gl(nInd,nPts,nPts*nInd*nModel) # creates the ID column mydata-data.frame(colPlot,colModel,colID,TimePts,Obs,Pred) # creates the dataset names(mydata)-c(Plot,Model,Individuals,Time,Observed,Predicted) # Plotting as indicated by Deepayan xyplot(Observed + Predicted ~ Time | Individuals + Model, data = mydata, panel = panel.superpose.2, type = c(p, l), layout = c(0, nlevels(mydata$Individuals))) #, #...) ### End of code This codes is not exactly what I am looking for, although it is pretty close. In the present case, I would like to have a Trellis plot with 6 panels (one for each individual), where the Observations and the Predicted are plotted as symbols and lines, respectively. All three models should be plotted on the same panel. Unfortunately, it looks to me as 3 successives xyplots are created by the code above but only the last one remains displayed. I tried to play with panel.superpose,panel.superpose.2 and type, without much success. I also tried the following code that creates 18 panels and distinguish all (Individuals,Model) couples... so, not what I want. xyplot(Observed + Predicted ~ Time | Individuals+Model, data = mydata, type = c(p, l), distribute.type = TRUE) Sebastien Deepayan Sarkar a écrit : On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Hi Hadley, Hopefully, my dataset won't be too hard to changed. Can I modify the aspect of each group using your code (symbols for observed and lines for predicted)? Sebastien hadley wickham a écrit : Hi Sebastian, I think you need to rearrange your data a bit. Firstly, you need to put observed on the same footing as the different models, so you would have a new column in your data called value (previously observed and predicted) and a new model type (observed). Then you could do: Yes, and ?make.groups (and reshape of course) could help with that. This might not be strictly necessary though. However, I'm finding your pseudo-code confusing. Could you create a small example data set that can be used to try out some real code? Just from your description, I would have suggested something like xyplot(Observed + Predicted ~ Time | Individuals + Model, data = mydata, panel = panel.superpose.2, type = c(p, l), layout = c(0, nlevels(mydata$Individuals)), ...) If all you want is to plot one page at a time, there are easier ways to do that. -Deepayan xyplot(value ~ time | individauls, data=mydata, group=model) Hadley On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Dear R Users, I recently posted an email on this list about the use of data.frame and overlaying multiple plots. Deepayan kindly indicated to me the panel.superposition command which worked perfectly in the context of the example I gave. I'd like to go a little bit further on this topic using a more complex dataset structure (actually the one I want to work on). mydata PlotModelIndividualsTimeObserved Predicted 11A 1 0.05 1010.2 21A 1 0.10 2019.5 etc... 10 1B 1 0.05 10 9.8 11 1B 1 0.10 20 20.2 etc... There are p levels in mydata$Plot, m in mydata$Model, n in mydata$Individuals and t in mydata$Time (Note that I probably use the word levels improperly as all columns are not factors). Basically, this dataset summarizes the t measurements obtained in n individuals as well as the predicted values from m different modeling approaches (applied to all individuals
Re: [R] Overlaying lattice graphs (continued)
Hadley, I have some troubles to run your code with ggplot version 0.4.1. Is the package ggplot2 mandatory ? Sebastien hadley wickham a écrit : Hi Sebastian, I think the following does what you want: library(ggplot2) names(mydata) - tolower(names(mydata)) obs - rename(subset(mydata, model==A, -predicted), c(observed = value)) obs$model - factor(observed) pred - rename(mydata[, -5], c(predicted = value)) all - rbind(obs, pred) ggplot(all, aes(x = time, y = value, colour=model)) + geom_point(data = subset(all, model != Observed)) + geom_line(data= subset(all, model == Observed)) + facet_grid(. ~ individuals) Hadley On 6/22/07, Sébastien [EMAIL PROTECTED] wrote: Hi Deepayan, The following code creates a dummy dataset which has the same similar as my usual datasets. I did not try to implement the changes proposed by Hadley, hoping that a solution can be found using the original dataset. # My code # Creating dataset nPts-10# number of time points nInd-6 # number of individuals nModel-3 # number of models TimePts-rep(1:nPts,nInd*nModel)# creates the Time column Coef-rep(rnorm(6,0.1,0.01),each=nPts,nModel) # Creates a vector of coefficients for generating the observations Obs-10*exp(-Coef*TimePts) # creates the observations for (i in 1:60){ Pred[i]-jitter(10*exp(-Coef[i]*TimePts[i])) Pred[i+60]-jitter(5) Pred[i+120]-jitter(10-Coef[i+120]*TimePts[i]) } # creates the predicted values colPlot-rep(1,nPts*nInd*nModel) # creates the Plot column colModel-gl(nModel,nPts*nInd,labels=c(A,B,C)) # creates the Model column colID-gl(nInd,nPts,nPts*nInd*nModel) # creates the ID column mydata-data.frame(colPlot,colModel,colID,TimePts,Obs,Pred) # creates the dataset names(mydata)-c(Plot,Model,Individuals,Time,Observed,Predicted) # Plotting as indicated by Deepayan xyplot(Observed + Predicted ~ Time | Individuals + Model, data = mydata, panel = panel.superpose.2, type = c(p, l), layout = c(0, nlevels(mydata$Individuals))) #, #...) ### End of code This codes is not exactly what I am looking for, although it is pretty close. In the present case, I would like to have a Trellis plot with 6 panels (one for each individual), where the Observations and the Predicted are plotted as symbols and lines, respectively. All three models should be plotted on the same panel. Unfortunately, it looks to me as 3 successives xyplots are created by the code above but only the last one remains displayed. I tried to play with panel.superpose,panel.superpose.2 and type, without much success. I also tried the following code that creates 18 panels and distinguish all (Individuals,Model) couples... so, not what I want. xyplot(Observed + Predicted ~ Time | Individuals+Model, data = mydata, type = c(p, l), distribute.type = TRUE) Sebastien Deepayan Sarkar a écrit : On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Hi Hadley, Hopefully, my dataset won't be too hard to changed. Can I modify the aspect of each group using your code (symbols for observed and lines for predicted)? Sebastien hadley wickham a écrit : Hi Sebastian, I think you need to rearrange your data a bit. Firstly, you need to put observed on the same footing as the different models, so you would have a new column in your data called value (previously observed and predicted) and a new model type (observed). Then you could do: Yes, and ?make.groups (and reshape of course) could help with that. This might not be strictly necessary though. However, I'm finding your pseudo-code confusing. Could you create a small example data set that can be used to try out some real code? Just from your description, I would have suggested something like xyplot(Observed + Predicted ~ Time | Individuals + Model, data = mydata, panel = panel.superpose.2, type = c(p, l), layout = c(0, nlevels(mydata$Individuals)), ...) If all you want is to plot one page at a time, there are easier ways to do that. -Deepayan xyplot(value ~ time | individauls, data=mydata, group=model) Hadley On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Dear R Users, I recently posted an email on this list about the use of data.frame and overlaying multiple plots. Deepayan kindly indicated to me the panel.superposition command which worked perfectly in the context of the example I gave. I'd like to go a little bit further on this topic using a more complex dataset structure (actually the one I want to work on). mydata PlotModelIndividualsTimeObserved Predicted 11A 1 0.05 1010.2 2
Re: [R] Overlaying lattice graphs (continued)
Sorry, I have forgotten to tell that I work on R version 2.5.0 on Windows XP sp2. Sébastien a écrit : Dear R Users, I recently posted an email on this list about the use of data.frame and overlaying multiple plots. Deepayan kindly indicated to me the panel.superposition command which worked perfectly in the context of the example I gave. I'd like to go a little bit further on this topic using a more complex dataset structure (actually the one I want to work on). mydata PlotModelIndividualsTimeObserved Predicted 11A 1 0.05 1010.2 21A 1 0.10 2019.5 etc... 10 1B 1 0.05 10 9.8 11 1B 1 0.10 20 20.2 etc... There are p levels in mydata$Plot, m in mydata$Model, n in mydata$Individuals and t in mydata$Time (Note that I probably use the word levels improperly as all columns are not factors). Basically, this dataset summarizes the t measurements obtained in n individuals as well as the predicted values from m different modeling approaches (applied to all individuals). Therefore, the observations are repeated m times in the Observed columns, while the predictions appears only once for a given model an a given individual. What I want to write is a R batch file creating a Trellis graph, where each panel corresponds to one individual and contains the observations (as scatterplot) plus the predicted values for all models (as lines of different colors)... $Plot is just a token: it might be used to not overload graphs in case there are too many tested models. The fun part is that the values of p, m, n and t might vary from one dataset to the other, so everything has to be coded dynamically. For the plotting part I was thinking about having a loop in my code containing something like that: for (i in 1:nlevels(mydata$Model)) { subdata-subset(mydata,mydata$Model=level(mydata$Model)[i]) xyplot(subset(Observed + Predicted ~ Time | Individuals, data = subdata) #plus additionnal formatting code } Unfortunately, this code simply creates a new Trellis plot instead of adding the model one by one on the panels. Any idea or link to a useful command will wellcome. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Overlaying lattice graphs (continued)
Hi Hadley, Hopefully, my dataset won't be too hard to changed. Can I modify the aspect of each group using your code (symbols for observed and lines for predicted)? Sebastien hadley wickham a écrit : Hi Sebastian, I think you need to rearrange your data a bit. Firstly, you need to put observed on the same footing as the different models, so you would have a new column in your data called value (previously observed and predicted) and a new model type (observed). Then you could do: xyplot(value ~ time | individauls, data=mydata, group=model) Hadley On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Dear R Users, I recently posted an email on this list about the use of data.frame and overlaying multiple plots. Deepayan kindly indicated to me the panel.superposition command which worked perfectly in the context of the example I gave. I'd like to go a little bit further on this topic using a more complex dataset structure (actually the one I want to work on). mydata PlotModelIndividualsTimeObserved Predicted 11A 1 0.05 1010.2 21A 1 0.10 2019.5 etc... 10 1B 1 0.05 10 9.8 11 1B 1 0.10 20 20.2 etc... There are p levels in mydata$Plot, m in mydata$Model, n in mydata$Individuals and t in mydata$Time (Note that I probably use the word levels improperly as all columns are not factors). Basically, this dataset summarizes the t measurements obtained in n individuals as well as the predicted values from m different modeling approaches (applied to all individuals). Therefore, the observations are repeated m times in the Observed columns, while the predictions appears only once for a given model an a given individual. What I want to write is a R batch file creating a Trellis graph, where each panel corresponds to one individual and contains the observations (as scatterplot) plus the predicted values for all models (as lines of different colors)... $Plot is just a token: it might be used to not overload graphs in case there are too many tested models. The fun part is that the values of p, m, n and t might vary from one dataset to the other, so everything has to be coded dynamically. For the plotting part I was thinking about having a loop in my code containing something like that: for (i in 1:nlevels(mydata$Model)) { subdata-subset(mydata,mydata$Model=level(mydata$Model)[i]) xyplot(subset(Observed + Predicted ~ Time | Individuals, data = subdata) #plus additionnal formatting code } Unfortunately, this code simply creates a new Trellis plot instead of adding the model one by one on the panels. Any idea or link to a useful command will wellcome. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Retrieve part of (top right corner) of a plot.data.frame plot?
Hi, That is maybe not the most elegant way but you can hide some plots regions by add a white polygon, eg: polygon(x=c(1,1,0,...),y=c(0,1,0,...),col=0,xpd=xpd) Just a personnal question, can you modify the content of the title boxes without changing the names of the variables, e.g. myParameter instead of a? Dan Bolser a écrit : Hi, I believe this question has been asked before, but I cant find and don't remember the answer. The problem is simple, calling 'plot.data.frame(x)' gives a nice 'matrix of scatterplots' for each pair of columns in x. for example; x - data.frame(a=jitter(01:20), b=jitter(20:01), c=jitter(21:40), d=jitter(rep(01,20)), e=jitter(rep(10,20)), f=jitter(rep(20,20)) ) plot(x) gives a 6 by 6 grid of scatter plots, two (upper right and lower left) for each pair of columns in x. (I am going over these basics so that you can understand what I mean next). I would like to see just part of the above result, namely the nine plots in the top right of the given plot, or; a vs. d | a vs. e | a vs. f b vs. d | b vs. e | b vs. f c vs. d | c vs. e | c vs. f I tried a number of ways to do this, but I can't find either the right formula or the right function to get what I want. Any suggestions you can give (especially any not involving the source code of 'pairs') are most welcome. Dan. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Overlaying lattice graphs (continued)
Dear R Users, I recently posted an email on this list about the use of data.frame and overlaying multiple plots. Deepayan kindly indicated to me the panel.superposition command which worked perfectly in the context of the example I gave. I'd like to go a little bit further on this topic using a more complex dataset structure (actually the one I want to work on). mydata PlotModelIndividualsTimeObserved Predicted 11A 1 0.05 1010.2 21A 1 0.10 2019.5 etc... 10 1B 1 0.05 10 9.8 11 1B 1 0.10 20 20.2 etc... There are p levels in mydata$Plot, m in mydata$Model, n in mydata$Individuals and t in mydata$Time (Note that I probably use the word levels improperly as all columns are not factors). Basically, this dataset summarizes the t measurements obtained in n individuals as well as the predicted values from m different modeling approaches (applied to all individuals). Therefore, the observations are repeated m times in the Observed columns, while the predictions appears only once for a given model an a given individual. What I want to write is a R batch file creating a Trellis graph, where each panel corresponds to one individual and contains the observations (as scatterplot) plus the predicted values for all models (as lines of different colors)... $Plot is just a token: it might be used to not overload graphs in case there are too many tested models. The fun part is that the values of p, m, n and t might vary from one dataset to the other, so everything has to be coded dynamically. For the plotting part I was thinking about having a loop in my code containing something like that: for (i in 1:nlevels(mydata$Model)) { subdata-subset(mydata,mydata$Model=level(mydata$Model)[i]) xyplot(subset(Observed + Predicted ~ Time | Individuals, data = subdata) #plus additionnal formatting code } Unfortunately, this code simply creates a new Trellis plot instead of adding the model one by one on the panels. Any idea or link to a useful command will wellcome. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Define tick mark width
Hello, Is there a way to define the width of the axis tick marks on traditionnal plots? I did not find anything specific on this topic in the help and par(lwd=...) does not affect the tick marks. I guess that using axes=FALSE and recreating the axis with the axis() command could do the trick but I wonder if there is no easier way. Thanks in advance Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Define tick mark width
Not really, but it is a good way to get major and minor tick marks (thanks it might be useful later in my project). What I to modify is the width of the segments which correspond to the marks? I know that I can do that with a lwd command in axis()... but there might be an option to set directly in the plot commands without having to recreate the axis. jim holtman a écrit : does this do what you want? plot(0, xlim=c(0,10), xaxt='n') axis(1, at=c(0,5,10)) axis(1,at=0:10,tcl=-.2, labels=FALSE) On 6/14/07, *Sébastien* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Hello, Is there a way to define the width of the axis tick marks on traditionnal plots? I did not find anything specific on this topic in the help and par(lwd=...) does not affect the tick marks. I guess that using axes=FALSE and recreating the axis with the axis() command could do the trick but I wonder if there is no easier way. Thanks in advance Sebastien __ R-help@stat.math.ethz.ch mailto:R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Overlaying lattice graphs
Thanks for the information. These commands work perfectly fine and the ?panel.superpose help was most informative. If you don't mind, I will certainly come back to you as there will certainly be additionnal complexities in my datasets that I will be unable to handle (e.g. data in more than one data.frame, complex conditions...). Sebastien Deepayan Sarkar a écrit : On 6/11/07, Seb [EMAIL PROTECTED] wrote: Hello I apologize in advance if this question has already be posted on the list, although I could not find a relevant thread in the archives. I would like to overlay xyplots using different datasets for each plot. I typically work on the following data.frame (mydata) structure mydata DrugTimeObserved Predicted 1 A0.05 10 10.2 2 A0.10 20 19.5 etc... 100 B0.05 11 12.7 101 B0.10 35 36 etc... I want to plot the observed data as points and the predicted values as lines. If I use the following commands, I don't have the possibility to switch the y values from Observed for the scatterplot to Predicted for the line. xyplot(Observed ~ Time | Drug, data = mydata, panel = function(x,y, ...){ +panel.xyplot(x,y,...) +panel.xyplot(x,y,type=l,...)}) I wonder if this problem can be solved using the trellis.focus family commands but I have a hard time to understand how they work. Please, let me know if a thread have already addressed this question. Otherwise, I would grateful for any hint, comments or info you can provide. There are several possible solutions. In your case, the simplest one would be something like (see ?panel.superpose for explanation): xyplot(Observed + Predicted ~ Time | Drug, data = mydata, type = c(p, l), distribute.type = TRUE) This will work best if the Time values are ordered; otherwise you could use type = c(p, a) instead, which will be a little slower. Let us know if this doesn't give you what you want, preferably with a reproducible example illustrating why. -Deepayan __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Subset and logical operator error
Hello, It looks to me as if the ! logical operator cannot be called when subsetting a data.frame. In the example below, the value column has two factor levels (but my typical datasets have more), and what I am trying to do is to exclude all lines for which the value is different from A. I have got a syntax error message everytime I try to use the subset() function. Unfortunatelly, the help on the subset function or the logical operators is not really specific on the way to implement this type of exclusion subset? Can you please point to me my syntax mistake or indicate a method to get this type of data.frame subset ? Thank you in advance ID value 1 1 1.2 2 2 1.2 3 3 1.2 4 4 1.2 5 5 A 6 6 A 7 7 A 8 8 A subset(mdat,value!A) Error: syntax error, unexpected '!', expecting ',' in subset(mdat,value! Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Batch processing in Windows
Hi, I am a complete newbe to R, so the following problem will probably be trivial for most of you guys: I get an error message every time I try to run a R file directly from the DOS shell. My R file (test.R) is intended to create a basic graph and has a very simple code: x-rep(1:10,1) y-rep(1:10,1) plot(x,y) I am using the following command to call this file directly from the c:/ root: C:/R CMD BATCH e:/Documents Seb/3_/test.R And here is the error message (Translated from french to english): 'R' is not recognized as an internal or external command, an executable script or a command file My OS is a french Windows XP sp2 and I am using R version 2.5.0. I wonder if the problem comes from an installation problem... Thank you in advance for your help. Sebastien __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Non-linear minimization
hello, I have got some trouble with R functions nlm(), nls() or optim() : I would like to fit 3 parameters which must stay in a precise interval. For exemple with nlm() : fn-function(p) sum((dN-estdata(p[1],p[2],p[3]))^2) out-nlm(fn, p=c(4, 17, 5), hessian=TRUE,print.level=2) with estdata() a function which returns value to fit with dN (observed data vactor) My problem is that only optim() allows me to set parameters interval with L-BFGS-B method but this one doesn't work in my case. I have heard about nls2 package (www.inra.fr/bia) but it doesn't work on Windows. Do you know any solutions Thank's a lot for reading my post Best regards Sebastien INA P-G ecology dpt __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Can't save a graph to pdf in R for MacOS
Hi, I am using R 1.7.1 (carbon) for MacOS and I am running it on MacOS X 10.2.6. When I send a graph to the pdf device (or any other devices), I get a zero KB file name Rplots.pdf. Before sending my graph to the output, I did: dev.off() pdf() boxplot(... my graph commands...) dev.off() Is this the correct procedure? I did the same procedure on another PC running Linux (R 1.6) and it work well. Please help! Thanks, Sébastien Plante __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help