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)
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 21A 1 0.10 2019.5 etc... 10 1B 1 0.05 10 9.8 11 1
Re: [R] Overlaying lattice graphs (continued)
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) The way you have structured your data makes no sense to me. In particular, your 'Observed' data is the same set of 60 numbers repeated 3 times, and this is not reflected in the data structure at all. What would you want to happen if the numbers were not repeated? Would you always plot the first 60, or would plot all of them? If I understand what you are trying to do, this might be a more transparent approach: nPts-10 # number of time points nInd-6# number of individuals TimePts - rep(1:nPts, nInd) Coef - rep(rnorm(6,0.1,0.01), each = nPts) Obs - 10 * exp(-Coef * TimePts) colID - gl(nInd, nPts) mydata - data.frame(Time = TimePts, Observed = Obs, Individuals = colID) fmA - lm(Observed ~ Time, mydata) fmB - lm(Observed ~ poly(Time, 2), mydata) fmC - lm(Observed ~ poly(Time, 2) * Individuals, mydata) mydata$PredA - predict(fmA) mydata$PredB - predict(fmB) mydata$PredC - predict(fmC) xyplot(Observed + PredA + PredB + PredC ~ Time | Individuals, data = mydata, type = c(p, l, l, l), distribute.type = TRUE) -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.
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)
Yes - you'll need ggplot2. Hadley On 6/22/07, Sébastien [EMAIL PROTECTED] wrote: 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
Re: [R] Overlaying lattice graphs (continued)
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] 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] Overlaying lattice graphs (continued)
Sebastian, You should be able to, but I don't know how to do it with lattice. In ggplot (http://had.co.nz/ggplot2) you would do it as follows: ggplot(mydata, aes(x = time, y = value, colour=model)) + geom_point(subset(data, model != observed)) + geom_line((subset(data, model == observed)) + facet_grid(. ~ individuals) or if you only wanted the models coloured: ggplot(mydata, aes(x = time, y = value)) + geom_point(subset(data, model != observed), aes(colour=model)) + geom_line((subset(data, model == observed)) + facet_grid(. ~ individuals) Although the way the panels are arranged is probably suboptimal if you have many individuals. It's something I plan to fix in the future, so that + facet_wrap(individuals) would give you a display like lattice does. Hadley 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: 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] Overlaying lattice graphs (continued)
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). 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. __ 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.
Re: [R] Overlaying lattice graphs
On 6/12/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. Another approach would be to use ggplot, http://had.co.nz/ggplot2. Then your code might look something like: ggplot(mydata, aes(x=Time)) + geom_point(aes(y=Observed)) + geom_line(aes(y = Predicted)) + facet_grid(. ~ Drug) Hadley __ 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
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.